ASH*_*lah 4 node.js reactjs react-native
如何在前端的 React Native 和后端的 NodeJS MonggoDB 中使用 Google 登录?
小智 8
您可以在前端使用这个“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
}
}
};
Run Code Online (Sandbox Code Playgroud)
对于后端,您需要验证前端将发送的用户访问令牌。
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');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3525 次 |
| 最近记录: |