@somesoap/react-native-google-signin
v0.2.0
Published
Google Sign in implementation
Readme
react-native-google-signin
This package provides bare minimum required for retrieving google credentials for OAuth authentication.
Under the hood this packages uses modern Credential Manager for Android and Google Sign-In for iOS.
Installation
npm install @somesoap/react-native-google-signinor
yarn add install @somesoap/react-native-google-signinUsage
Retrieve idToken from Google credentials
The most common use for this package is to receive idToken for further usage for authentication via your
backend services or Firebase Auth.
import { getGoogleSignInToken } from '@somesoap/react-native-google-signin';
export function getIdToken() {
return getGoogleSignInToken({
serverClientId: "{SERVER_CLEINT_ID}", // Usualy received from Firebase or Google Console
})
.then((credentials) => credentials.idToken)
}
Authenticate via Firebase
Purposed usage is for OAuth authentication in conjunction with Firebase.
import { Button } from 'react-native';
import { getGoogleSignInToken, ErrorCodes } from '@somesoap/react-native-google-signin';
import { GoogleAuthProvider, getAuth, signInWithCredential, onAuthStateChanged } from '@react-native-firebase/auth';
const handleSignInPress = async () => {
try {
const googleCredentials = await getGoogleSignInToken({
serverClientId: "{SET_YOUR_SERVER_CLIENT_ID}",
});
// Create a Google credential with the token
const googleCredential = GoogleAuthProvider.credential(googleCredentials.idToken);
console.log({googleCredential, googleCredentials});
// Sign-in the user with the credential
await signInWithCredential(getAuth(), googleCredential);
} catch (err: any) {
if (err.code === ErrorCodes.CANCELLATION_ERROR) {
// do nothing, user cancelled the action
return;
}
console.error(err);
}
};
const handleLogOutPress = async () => {
try {
await signOut(getAuth());
} catch (err) {
console.error(err);
}
};
const SignInTest = () => {
const [user, setUser] = React.useState<any>(null);
React.useEffect(() => {
return onAuthStateChanged(getAuth(), setUser); // unsubscribe on unmount
}, []);
if (!user) {
return (
<View>
<Button text="Sign In" onPress={handleSignInPress} />
</View>
);
}
return (
<View>
<Text>Welcome {user.email}</Text>
<Button text="Sign out" onPress={handleLogOutPress} />
</View>
);
};Contributing
This library is containing bare minimal functionality to provide authentication via Google OAuth.
For features request contact me via email.
If you are willing to contribute, check the guides below:
License
MIT
