I'm trying to connect new users through Facebook authentication and Firebase SDK with createContext, but I'm getting: new FacebookAuthProvider()
is not supported on the native Firebase SDKs.
I'm using the firebase doc here : https://firebase.google.com/docs/auth/web/facebook-login and here : https://github.com/firebase/snippets-web/blob/1452a031ee1b7904a361b23391af8533237eab82/auth/facebook.js#L9-L9
My code :
import React, {createContext, useState} from 'react';
import auth from '@react-native-firebase/auth';
import firebase from '@react-native-firebase/app';
export const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
const [user, setUser] = useState(null);
return (
<AuthContext.Provider
value={{
user,
setUser,
login: async (email, password) => {
try {
await auth().signInWithEmailAndPassword(email, password);
} catch (e) {
return handleError(e);
}
},
register: async (email, password) => {
try {
await auth().createUserWithEmailAndPassword(email, password);
} catch (e) {
return handleError(e);
}
},
logout: async () => {
try {
await auth().signOut();
} catch (e) {
return handleError(e);
}
},
loginWithFacebook: async (processRequest) => {
try {
let provider = new firebase.auth.FacebookAuthProvider();
provider.addScope('user_birthday');
provider.setCustomParameters({
display: 'popup',
});
await firebase.auth().signInWithPopup(provider);
} catch (e) {
console.log(e);
}
},
}}>
{children}
</AuthContext.Provider>
);
};
If someone have another idea for using facebook connection with firebase SDK and useContext I'm okay too.
Okay so as the doc says here :
All Authentication features, except phone authentication and popup/redirect OAuth operations, are supported.
And the solution is here
My AuthProvider now with onFaceBookButtonPress function from the above doc :
import React, {createContext, useState} from 'react';
import auth from '@react-native-firebase/auth';
import {LoginManager, AccessToken} from 'react-native-fbsdk';
export const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
const [user, setUser] = useState(null);
return (
<AuthContext.Provider
value={{
user,
setUser,
login: async (email, password) => {
try {
await auth().signInWithEmailAndPassword(email, password);
} catch (e) {
return handleError(e);
}
},
register: async (email, password) => {
try {
await auth().createUserWithEmailAndPassword(email, password);
} catch (e) {
return handleError(e);
}
},
logout: async () => {
try {
await auth().signOut();
} catch (e) {
return handleError(e);
}
},
loginWithFacebook: async (processRequest) => {
try {
await onFacebookButtonPress();
} catch (e) {
throw new Error(e);
}
},
}}>
{children}
</AuthContext.Provider>
);
async function onFacebookButtonPress() {
// Attempt login with permissions
const result = await LoginManager.logInWithPermissions([
'public_profile',
'email',
]);
if (result.isCancelled) {
throw 'User cancelled the login process';
}
// Once signed in, get the users AccesToken
const data = await AccessToken.getCurrentAccessToken();
if (!data) {
throw 'Something went wrong obtaining access token';
}
// Create a Firebase credential with the AccessToken
const facebookCredential = auth.FacebookAuthProvider.credential(
data.accessToken,
);
// Sign-in the user with the credential
return auth().signInWithCredential(facebookCredential);
}
And I call it like this:
import {AuthContext} from '../../../navigation/auth/AuthProvider';
const {loginWithFacebook} = useContext(AuthContext);
<Button onPress={loginWithFacebook}