简体   繁体   English

React Native 中的 GoogleSignIn 身份验证

[英]GoogleSignIn Authentication in React Native

I'm confused related to the GoogleSignIn Logout first I've managed to SignIn my google account, the problem is when I logout my google account, and SignIn again the previous account that you login before it didn't ask the password like the image below when I click one of user account it will go to my home screen directly instead asking password for that google account since I've already logout the account in my app.我对GoogleSignIn Logout感到困惑首先我已经设法SignIn我的谷歌帐户,问题是当我注销我的谷歌帐户时,再次登录你之前登录的上一个帐户,然后它没有像图片一样询问密码下面,当我单击其中一个用户帐户时,它将 go 直接显示到我的主屏幕,而不是询问该 google 帐户的密码,因为我已经在我的应用程序中注销了该帐户。

在此处输入图像描述

This is my logout function I tried the code below it works in some other way but my concern is since I've already logout the account it should not directly go to homepage when SignIn again ask password to the user or remove user when logout?这是我的注销 function我尝试了下面的代码以其他方式工作,但我担心的是,因为我已经注销了帐户,所以当登录再次询问用户密码或注销时删除用户时,它不应该直接将SignIn直接登录到homepage What is the best way should I do, do I correcly implement it?我应该做的最好的方法是什么,我是否正确地实施它?

   const [user, setUser] = useState(null);
   logout: async () => {
      try {
        await GoogleSignin.revokeAccess();
        await GoogleSignin.signOut();
        setUser({ user: null });
        await auth().signOut();
        
      } catch (e) {
        console.log(e);
      }
    },

GoogleSign function谷歌签名 function

  return (
    <AuthContext.Provider
      value={{
        user,
        setUser,
        googleLogin: async () => {
          try {
            // Get the users ID token
            const Token  = await GoogleSignin.signIn();
            const {idToken} =  Token;
            
            // Create a Google credential with the token
            const googleCredential = auth.GoogleAuthProvider.credential(idToken);
        
          
            // Sign-in the user with the credential
            await auth().signInWithCredential(googleCredential)            
            .catch(error => {
                console.log('Something went wrong with sign up: ', error);
            });
          } catch(error) {
            console.log({error});
          }
        },
      }}>
      {children}
    </AuthContext.Provider>
  );

Short Answer: Your code is completely fine!简短回答:您的代码完全没问题!

Long Answer: This is completely normal behaviour and is intended to be.长答案:这是完全正常的行为,并且是有意的。 If you want to Sign Out completely you would have to go into the settings of the corresponding google account and sign out from that device.如果您想完全退出,则必须将 go 进入相应谷歌帐户的设置并从该设备退出。 (Manage Google Account => Security => My Devices => Logout) (管理 Google 帐户 => 安全 => 我的设备 => 注销)

try removing the await GoogleSignin.revokeAccess();尝试删除 await GoogleSignin.revokeAccess(); this worked for me after removing revoke access删除撤销访问权限后,这对我有用

useEffect(() => {
    GoogleSignin.configure({
      webClientId:
        'nmgqmhuak76u9evobvkdm8oil764a9e3.apps.googleusercontent.com',
    });
  }, []);

const signInWithGoogleAsync = async () => {
    try {
      const {idToken} = await GoogleSignin.signIn();
      const googleCredential = auth.GoogleAuthProvider.credential(idToken);
      const user_sign_in = auth().signInWithCredential(googleCredential);
      user_sign_in
        .then(user => {
          console.log(user);
        })
        .catch(error => {
          console.log(error);
          if (error.code === 'auth/network-request-failed') {
            ToastAndroid.showWithGravity(
              'A network error (such as timeout, interrupted connection or unreachable host) has occurred.]',
              ToastAndroid.SHORT,
              ToastAndroid.CENTER,
            );
          }
        });
    } catch (error) {
      console.log(error);
    }
  };

const logout = async () => {
    try {
      await GoogleSignin.signOut();
      await auth().signOut();
      ToastAndroid.showWithGravity(
        'Signing Off',
        ToastAndroid.SHORT,
        ToastAndroid.CENTER,
      );
      navigation.replace('login');
} catch (error) {
  console.log(error);
}

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM