react-native-firebase/codorials/authentication-with-firebase/google-login.md

49 lines
1.5 KiB
Markdown
Raw Normal View History

2018-04-16 12:51:19 +00:00
# Google Login
Much like Facebook loginin, Firebase provides the ability to accept and sign in with a Google credential.
## Installing `react-native-google-signin`
To sign in with Google, we recommend using `react-native-google-signin`. This provides a native way of obtaining the users
Google accounts and their required `accessToken`.
```
npm install react-native-google-signin --save
react-native link react-native-google-signin
```
## Creating a credential
To generate a new credential for the user, simply call the asynchronous `signIn` method `react-native-google-signin` provides and create a new
credential from the `firebase.auth.GoogleAuthProvider`:
```js
import { GoogleSignin } from 'react-native-google-signin';
// Calling this function will open Google for login.
export const googleLogin = async () => {
try {
// Add any configuration settings here:
await GoogleSignin.configure();
const data = await GoogleSignin.signIn();
// create a new firebase credential with the token
2018-04-21 14:55:13 +00:00
const credential = firebase.auth.GoogleAuthProvider.credential(
data.idToken,
data.accessToken
);
2018-04-16 12:51:19 +00:00
// login with credential
2018-04-21 14:55:13 +00:00
const currentUser = await firebase
.auth()
.signInAndRetrieveDataWithCredential(credential);
2018-04-16 12:51:19 +00:00
} catch (e) {
console.error(e);
}
2018-04-21 14:55:13 +00:00
};
2018-04-16 12:51:19 +00:00
```
The flow here is exactly the same as the rest of our app; logging in with the newly created credential will trigger our `onAuthStateChanged` lister to fire with
the new user, logging us into the application.