刷新页面以角度5自动注销 [英] Refresh page get log out automatically in angular 5

查看:77
本文介绍了刷新页面以角度5自动注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase进行登录和注册. 那是我的authService样子:

I'm using firebase for the signIn and signup. that is my authService look like :

token: string;
authenticated: boolean = false;
signinUser(email: string, password: string) {
        firebase
            .auth()
            .signInWithEmailAndPassword(email, password)
            .then(response => {
                this.authenticated = true;
                console.log('authService-->signinUser-->authenticated', this.authenticated);


                //Set the a wallet using a combination of the email and the name of the network e.g. Majd@gmail.com@stschain
                this.dataService.setWallet(`${email}${this.domainExtenstion}`);
                this.setEmail(email);
                this.router.navigate(['/dashboard']);
                console.log('sinign in')
                firebase
                    .auth()
                    .currentUser.getIdToken()
                    .then(
                        (token: string) => {
                            (this.token = token);
                            // localStorage.setItem('token', JSON.stringify(token));
                        }
                    );
            })
            .catch(error => {
                console.log(error);
                alert(error);
            });

    }

isAuthenticated() {
   return this.token != null;       
}

在我的authGuardService中,即时消息调用可以激活这样的方法:

and in my authGuardService im calling canactivate methode like this :

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (!this.authService.authenticated) {
      console.log('cant load' )
      this.router.navigate(['/signin']);
    }
    console.log('can load' )
    return this.authService.isAuthenticated();
  }
}

但是当我刷新页面时,此authenticated值始终为false.

but when i refresh the page this authenticated value get false always.

请任何人知道将要感谢的原因.

Please, anyone, know the cause that will appreciate.

推荐答案

在您的代码中,您将执行以下操作:

In your code you do:

firebase
    .auth()
    .signInWithEmailAndPassword(email, password)
    .then(response => {
        this.authenticated = true;

then块仅在用户显式登录时运行.重新加载页面后,它不会自动运行.

This then block only runs when the user explicitly signs in. It does not automatically run when the page gets reloaded.

但是在重新加载页面时,Firebase身份验证会自动(尝试)恢复用户的登录会话,您的代码只是不知道它.要检测身份验证状态更改,请使用身份验证状态侦听器(如

But Firebase Authentication does automatically (try to) restore the user's sign-in session when the page is reloaded, your code just isn't aware of it. To detect auth state changes, use an auth state listener (as shown in the documentation):

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

请注意,onAuthStateChanged回调会同时针对显式(例如,调用signInWith...)和隐式(例如,页面重新加载)身份验证状态更改进行调用,因此请考虑将代码(的一部分)从then()块移至此回调.

Note that the onAuthStateChanged callback gets called for both explicit (e.g. you calling signInWith...) and implicit (e.g. page reload) auth state changes, so consider moving (some of) your code from the then() block to this callback.

这篇关于刷新页面以角度5自动注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆