How to login with Google in React Native for frontend and NodeJS MonggoDB for backend?
You can use this "react-native-google-signin" Library on frontend side.
This is the usage
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
}
}
};
For Backend, you need to verify the user access token that will send by frontend.
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With