у меня есть приложение для реагирования на нативе, у него есть кнопка регистрации с помощью кнопки google, когда я нажимаю на вход, я получаю данные в console.info, я хочу сохранить данные в firebase, я не знаю, как это сделать
const googleLogin = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
console.info(userInfo);// i am getting user data here
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (e.g. 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
}
}
};
Вы можете обратиться к этой ССЫЛКЕ для социальной аутентификации Google, это то, что вы ищете, чтобы сохранить данные аутентификации в firebase:
import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
async function onGoogleButtonPress() {
// Check if your device supports Google Play
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
// Get the users ID token
const { idToken } = await GoogleSignin.signIn();
// Create a Google credential with the token
const googleCredential = auth.GoogleAuthProvider.credential(idToken);
// Sign-in the user with the credential
return auth().signInWithCredential(googleCredential);
}
Это потому, что у вас не установлен модуль в вашем приложении, обратитесь к руководствам по ссылке, которую я разместил, чтобы правильно установить firebase и аутентификацию в вашем приложении.
необходимо указать adToken или accesstoken
@Mostfa Я понял это, но теперь я получаю сообщение об ошибке с просьбой указать токен
Это может быть полезно. Просто убедитесь, что вы интегрировали все библиотеки для аутентификации Firebase.
import {
GoogleSignin,
statusCodes,
} from '@react-native-google-signin/google-signin';
import auth, {FirebaseAuthTypes} from '@react-native-firebase/auth';
// This function will be call on tapping sign in with google button
const signInWithGoogle = async () => {
try {
// This will check whether there is Google play service or not
await GoogleSignin.hasPlayServices();
//This will give you userInformation
const userInfo = await GoogleSignin.signIn();
// This will create new credential which can help to signIn in firebase
const credential = auth.GoogleAuthProvider.credential(userInfo.idToken);
//Here we are trying to return promise so when we call function we can have promise object
return new Promise((resolve, reject) => {
auth()
.signInWithCredential(credential)
.then(response => {
console.info('response in', response);
resolve(response);
})
.catch(error => {
console.info('error in', error);
reject(error);
});
});
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (e.g. sign in) is in progress already
alert(JSON.stringify(error));
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
// play services not available or outdated
alert(JSON.stringify(error));
} else {
alert(error);
}
}
};
Теперь, когда вы вызываете эту функцию на кнопке Google при нажатии, она даст вам объект обещания, и вы можете сделать это, как показано ниже.
onPress = {() => {
SignInMethods.signInWithGoogle()
.then(response => {
console.info('user information from firebase authentication', response.user);
})
.catch(error => {
console.info('error in google sign in :', error);
});
}}
я получаю эту ошибку