首页 文章

即使在登录Angular之后,直接点击url也会再次登录页面

提问于
浏览
0

Firebase认证集成在一个角度应用程序中 . 即使在登录应用程序后,直接在新标签页或浏览器的相同标签页上点击网址或路由,例如Chrome, http://localhost:4200/landing再次进入登录页面 .

如果已经登录,是否有办法不跳过登录页面 .

重现的步骤

在角度CLI项目中

Expected :它停留在http://localhost:4200/landing

Actual :它可以追溯到http://localhost:4200/login

2 回答

  • 0

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

    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;
        }
      }
    
  • 0

    我从你的问题中理解的是你最初要导航到登录页面,一旦用户已经登录,你想要去他已经输入的路线 . 如果没有进入登录页面你应该使用localstorage,如果你想要在浏览器中保留新选项卡或新窗口中的任何数据一旦您登录在本地存储中存储一些令牌 . 像这样

    localStorage.setItem('userId', 'USERID');
    

    现在在你的appComponent上初始化检查本地存储是否不存在然后才直接登录 .

    ngOnInit() {
    if (!localStorage.getItem('userId')) {
      this.router.navigate(['/login']); // i think initially you are redirecting to login so put condition there in app component
    }
    }
    

    注意:请注意,一旦关闭浏览器,这甚至会保留

相关问题