首页 > 解决方案 > 即使在登录 Angular 之后,直接点击 url 也会再次进入登录页面

问题描述

Firebase 身份验证集成在 Angular 应用程序中。即使在登录到应用程序后,直接在新选项卡或浏览器的同一选项卡上点击 url 或路由,chrome,例如http://localhost:4200/landing 会再次进入登录页面。

如果已经登录,有没有办法不跳过登录页面。

重现步骤

  1. ng serve在 Angular CLI 项目中将我带到http://localhost:4200/login
  2. 登录后需要http://localhost:4200/landing
  3. 打开一个新选项卡或在现有选项卡类型中,http://localhost:4200/landing回车

预期:它停留在http://localhost:4200/landing

实际:它回到http://localhost:4200/login

标签: angularangular2-routing

解决方案


我认为需要进行以下更改。

 constructor(private _firebaseAuth: AngularFireAuth, private router: Router) {
    this.user = _firebaseAuth.authState;
    this.user.subscribe(
      (user) => {
        if (user) {
          this.userDetails = user;              
// Set the UserId
           localStorage.setItem('userId', user.uid);
        } else {
          this.userDetails = null;
        }
      }
    );
  }


  isLoggedIn() {

    if (this.userDetails == null) {
// Check the UserId
      if (localStorage.getItem('userId')) {
              return true;
      }
      return false;

    } else {
      return true;
    }
  }

推荐阅读