Dav*_*ens 2 javascript firebase react-native firebase-authentication expo
我正在尝试使用 Expo 的 Google 登录功能并将该授权链接到我的 Firebase 身份验证。我无法理解如何将数据从从 Expo/google 服务接收/使用的身份验证令牌传输到我的 firebase 身份验证(即创建一个 firebase 标识符,UID)。我一直在关注有关链接身份验证和Google 登录的 Expo 文档的文档,这是我获得以下代码的地方。我也在stackoverflow 上查看了这个问题,但没有接受的答案。
对我来说,两个主要的困惑点是:
Expo 的 google login 和 firebase 的 auth 使用类似但不同的方式来验证用户身份。世博会采用Expo.Google.logInAsync(),并且token同时使用火力firebase.auth.GoogleAuthProvider(),auth.currentUser.linkWithRedirect(provider)和credential。
如果用户使用下面包含的 Expo/Google 方法登录,则 firebase在其数据库中没有该用户的记录,没有 UID,没有标识符。
如何确保如果用户使用以下方法登录,则会在 firebase auth 中创建用户?就像当一个人使用firebase.auth().createUserWithEmailAndPassword()
async logInWithGoogleAsync() {
try {
const result = await Expo.Google.logInAsync({
iosClientId: 'XXX',
scopes: ['profile', 'email'],
});
if(result.type === 'success') {
//LINK ACCOUNTS HERE?
//Use result.accessToken somehow?
return;
}
else {
return {cancelled: true};
}
}
catch(e) {
return {error: true};
}
}
Run Code Online (Sandbox Code Playgroud)
我对此的处理方法与文档所说的差不多,但我确实必须将很多点连接起来,这并非全部在一个地方 - 我可以帮助解决您的困惑(我也有)
解决你的第一点 - 基本上是通过使用
const 结果 = 等待 Expo.Google.logInAsync({
您正在做的是获取用户的 google 详细信息(Expo 正在处理 logInAsync 调用)。
我将其与 firebase 联系起来的方法是使用博览会提供的详细信息来创建 firebase google 登录凭据,然后使用此凭据登录。Firebase 提供对 GoogleAuthProvider 的访问
https://firebase.google.com/docs/reference/js/firebase.auth.GoogleAuthProvider
Expo 返回的详细信息(在您的情况下为“结果”)使您可以访问 idToken 和 accessToken,因此,通过使用:
const credential = firebase.auth.GoogleAuthProvider.credential(idToken, accessToken);
您可以创建一个 firebase google 凭据,然后您可以利用
firebase.auth().signInWithCredential(credential)
firebase 会将其注册为 google 凭据,并使用他们的 google 详细信息为用户注册/登录。(更改 AuthState)
第二点 - Firebase 允许您设置一个名为 onAuthStateChanged() 的侦听器
当您使用 signInWithCredential 时,AuthState 会发生变化并触发回调 - 此时您应该触发对 firebase 数据库的调用 - 捕获所有详细的用户(通过 google 登录访问)并将它们提交到 firebase 数据库中。我在 componentDidMount() 中设置了这个回调,以便在用户注册时 - 当他们按下提交时,我一直在监听 authStateChanges。
| 归档时间: |
|
| 查看次数: |
1470 次 |
| 最近记录: |