FirebaseAuth GoogleAuthProvider() Вход с перенаправлением

#javascript #angularjs #promise #firebase-authentication

#javascript #angularjs #обещание #firebase-аутентификация

Вопрос:

В моем приложении есть аутентификация Google с перенаправлением, и я хотел бы просто перенаправить, когда аутентификация полностью завершена. Но обещание не работает

  function loginGoogle() {
        var provider = new firebase.auth.GoogleAuthProvider();
        firebase.auth().signInWithRedirect(provider);
        firebase.auth().getRedirectResult().then(function (result) {
            // This gives you a Google Access Token. You can use it to access the Google API.
            if (result.credential) {
                var token = result.credential.accessToken;
                console.log('token '   token);
            }
            // The signed-in user info.
            var user = result.user;
            console.log('user '   user);
            // if success redirect to
            $state.go('maps-fullwidth');

            // ...
        }).catch(function (error) {
            // Handle Errors here.
            var errorCode = error.code;
            console.log(errorCode);
            var errorMessage = error.message;
            // The email of the user's account used.
            console.log(errorMessage);
            var email = error.email;
            console.log(email);
            // The firebase.auth.AuthCredential type that was used.
            var credential = error.credential;
            console.log(credential);
            // ...
        });
    }
  

Спасибо.

Ответ №1:

Переместите getRedirectResult() вызов из вашей loginGoogle() функции. getRedirectResult() должен вызываться при загрузке страницы. Пример этого в действии можно найти здесь:

https://github.com/firebase/quickstart-js/blob/master/auth/google-redirect.html

Комментарии:

1. можем ли мы назвать это так this.afAuth.auth.signInWithRedirect(provider).then(() => { return this.afAuth.auth .getRedirectResult() .then(result => { console.log(result); const that = this; this.storage .set(TOKEN_KEY, result.user.refreshToken) .then(res => { that.authenticationState.next(true); }); }) .catch(function(error) { alert(error.message); });

2. при последующем обратном вызове в signInWithRedirect

3. Большое спасибо Ченнингу Хуангу. Теперь понимаю лучше.