google sign in expo react native firebase code example
Example 1: google sign in in firebase react
const googleProvider = new firebase.auth.GoogleAuthProvider();
export { firebase, db, auth, functions, googleProvider }
import { firebase, googleProvider, db } from '../components/firebase/firebase'
import React, {useEffect} from "react";
import { useHistory, Redirect } from 'react-router-dom'
export const SignInWithGoogle = () => {
const history = useHistory();
useEffect(() => {
setTimeout(() => {
history.push('/dashboard');
}, 10000);
},[]);
return () => {
return firebase.auth().signInWithPopup(googleProvider)
.then(async result =>{
console.log(result.credential.accessToken)
const user = result.user
console.log(user)
localStorage.setItem('userid', user.uid)
localStorage.setItem('photoURL', user.photoURL)
await db.collection('users').doc(user.uid).update({
name: user.displayName,
email: user.email,
phone: user.phoneNumber,
photoURL: user.photoURL
})
})
.then(() => {
history.push('/dashboard');
})
.catch( err => {
console.log(err)
})
}
}
export const startLogout = () => {
return () => {
return firebase.auth().signOut()
}
}
Example 2: react native firebase login with facebook
async function logIn() {
try {
await Facebook.initializeAsync({
appId: "APP ID HERE",
});
const {
type,
token,
expirationDate,
permissions,
declinedPermissions,
} = await Facebook.logInWithReadPermissionsAsync({
permissions: ["public_profile"],
});
if (type === "success") {
alert(`HELLO`);
console.log("SUCCESS");
const response = await fetch(
`https://graph.facebook.com/me?access_token=${token}`
);
Alert.alert("Logged in!", `Hi ${(await response.json()).name}!`);
} else {
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}