gpt4 book ai didi

node.js - 在 React Native 和 NodeJS 后端使用 Google 登录

转载 作者:行者123 更新时间:2023-12-02 02:00:17 25 4
gpt4 key购买 nike

前端如何使用 React Native 和后端使用 NodeJS MongoDB 登录 Google?

最佳答案

您可以在前端使用这个“react-native-google-signin”库。

这是用法

import {GoogleSignin, statusCodes} from 'react-native-google-signin';
useEffect(() => {
configureGoogleSign();
}, []);

function configureGoogleSign() {
GoogleSignin.configure({
webClientId: WEB_CLIENT_ID,
offlineAccess: false,
});
}
const signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
setgmail(userInfo);

//Navigate user where you want and store information
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (f.e. sign in) is in progress already
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
// play services not available or outdated
} else {
// some other error happened
}
}
};

对于后端,您需要验证将由前端发送的用户访问 token 。

await axios({
method: 'get',
url: 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' + access_token,
withCredentials: true
})
.then(function (response) {
console.log('response==>', response.data);
flag = true;
id = response.data.kid
})
.catch(function (response) {
console.log('error');
});

关于node.js - 在 React Native 和 NodeJS 后端使用 Google 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69071261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com