数小时后,Expo上的Firebase Auth身份验证即将过期

时间:2019-06-27 23:39:57

标签: javascript firebase react-native firebase-authentication expo

我有一个Expo应用,使用AppAuth向Google进行身份验证。 用户通过Google身份验证后,我将使用Google ID令牌构建Firebase凭据,然后使用Firebase登录。

身份验证并读取Firebase可以正常工作,在用户返回数小时后,该用户不再通过Firebase进行身份验证。

PS:刷新或关闭应用程序会使用户保持身份验证,这似乎是及时的事情。

根据文档,持久性默认情况下应处于启用状态,但我也尝试过使用.setPersistence(firebase.auth.Auth.Persistence.LOCAL)来强制执行,但没有成功。

这是我的代码:

  onSignIn = googleUser => {
    // We need to register an Observer on Firebase Auth to make sure auth is initialized.
    var unsubscribe = firebase.auth().onAuthStateChanged(
      function(firebaseUser) {
        unsubscribe();

        firebase
          .auth()
          .setPersistence(firebase.auth.Auth.Persistence.LOCAL)
          .then(() => {
            // Check if we are already signed-in Firebase with the correct user.
            if (!this.isUserEqual(googleUser, firebaseUser)) {
              // Build Firebase credential with the Google ID token.
              var credential = firebase.auth.GoogleAuthProvider.credential(
                googleUser.idToken,
                googleUser.accessToken
              );
              // Sign in with credential from the Google user.
              firebase
                .auth()
                .signInWithCredential(credential)
                .then(function(result) {
                  if (result.additionalUserInfo.isNewUser) {
                    firebase
                      .firestore()
                      .collection("users")
                      .doc(result.user.email)
                      .set({
                        email: result.user.email,
                        profile_picture:
                          result.additionalUserInfo.profile.picture,
                        first_name:
                          result.additionalUserInfo.profile.given_name,
                        last_name:
                          result.additionalUserInfo.profile.family_name,
                        full_name: result.additionalUserInfo.profile.name,
                        locale: result.additionalUserInfo.profile.locale,
                        provider_id: result.additionalUserInfo.providerId,
                        created_at: Date.now()
                      })
                      .then(function(snapshot) {});
                  } else {
                    firebase
                      .firestore()
                      .collection("users")
                      .doc(result.user.email)
                      .update({
                        last_logged_in: Date.now()
                      });
                  }
                })
                .catch(function(error) {
                  console.log(error);
                  // Handle Errors here.
                  var errorCode = error.code;
                  var errorMessage = error.message;
                  // The email of the user's account used.
                  var email = error.email;
                  // The firebase.auth.AuthCredential type that was used.
                  var credential = error.credential;
                });
          } else {
              console.log("User already signed-in Firebase.");
            }
          });
      }.bind(this)
    );
  };

signInAsync = async () => {
    const authState = await AppAuth.authAsync(config);
    await this.cacheAuthAsync(authState);
    this.onSignIn(authState);
    return authState;
  };

我希望会话和身份验证能够打开并自动刷新。

2 个答案:

答案 0 :(得分:0)

编辑

据此:When to set Firebase auth state persistence,没关系

原始答案

我认为您可能必须在setPersistence之前onAuthStateChanged

onSignIn = googleUser => {
  return firebase.auth().setPersistence()
      .then(() => {
          return new Promise((resolve, reject) => {
              const unsubscribe = firebase.auth().onAuthStateChange((result) => {
                   resolve(result)
          })
      })
      .then( /// etc
}

答案 1 :(得分:0)

您的按键没有刷新。 确保您的浏览器密钥可以访问Token Service API

https://console.cloud.google.com/apis/credentials/