使用 create-react-native-app 和 firebase 使用 google 登录(使用指数)
Login with google with create-react-native-app and firebase (using exponent)
通过这个代码。
googleAuthenticate = (token) => {
const provider = firebase.auth.GoogleAuthProvider();
const credential = provider.credential(token)
return firebase.auth().signInWithCredential(credential) }
gglogin = async () => {
const LogInResult = await Google.logInAsync({
androidClientId: '<my-androidClientId>',
iosClientId: '<my-IOSClientId>',
scopes: ['profile', 'email']
});
if(LogInResult.type === 'success'){
Alert.alert(
'Logged in!',
`Hi ${user.name}!`,
);
this.googleAuthenticate(LogInResult.accessToken)
}
}
它可以获得用户名警报,但无法通过收到此错误将令牌发送到 firebase。
Possible Unhandled Promise Rejection (id: 0):
TypeError: this.addScope is not a function. (In
'this.addScope("profile")', 'this.addScope' is undefined)`
请问有什么问题
您必须将范围添加到提供程序,如下所示:
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
firebase.auth().signInWithRedirect(provider);
请再次检查此文档:
https://firebase.google.com/docs/reference/js/firebase.auth.GoogleAuthProvider
您需要如下更改代码:
googleAuthenticate = (token) => {
const provider = firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
const credential = provider.credential(token)
return firebase.auth().signInWithCredential(credential) }
我注意到两个问题,credential 是一个静态方法,第一个参数是一个 id 令牌,第二个参数是一个访问令牌(您似乎传递的是访问令牌):
const credential = firebase.auth.GoogleAuthProvider.credential(null, token);
return firebase.auth().signInWithCredential(credential);
通过这个代码。
googleAuthenticate = (token) => {
const provider = firebase.auth.GoogleAuthProvider();
const credential = provider.credential(token)
return firebase.auth().signInWithCredential(credential) }
gglogin = async () => {
const LogInResult = await Google.logInAsync({
androidClientId: '<my-androidClientId>',
iosClientId: '<my-IOSClientId>',
scopes: ['profile', 'email']
});
if(LogInResult.type === 'success'){
Alert.alert(
'Logged in!',
`Hi ${user.name}!`,
);
this.googleAuthenticate(LogInResult.accessToken)
}
}
它可以获得用户名警报,但无法通过收到此错误将令牌发送到 firebase。
Possible Unhandled Promise Rejection (id: 0):
TypeError: this.addScope is not a function. (In
'this.addScope("profile")', 'this.addScope' is undefined)`
请问有什么问题
您必须将范围添加到提供程序,如下所示:
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
firebase.auth().signInWithRedirect(provider);
请再次检查此文档: https://firebase.google.com/docs/reference/js/firebase.auth.GoogleAuthProvider
您需要如下更改代码:
googleAuthenticate = (token) => {
const provider = firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
const credential = provider.credential(token)
return firebase.auth().signInWithCredential(credential) }
我注意到两个问题,credential 是一个静态方法,第一个参数是一个 id 令牌,第二个参数是一个访问令牌(您似乎传递的是访问令牌):
const credential = firebase.auth.GoogleAuthProvider.credential(null, token);
return firebase.auth().signInWithCredential(credential);