[英]Firebase + Flutter : get platform user used to sign in
I have an app with 3 sign in methods: Google, Facebook & mail.我有一个具有 3 种登录方法的应用程序:Google、Facebook 和邮件。 I want to show the users that are signed in with mail a different screen.我想向使用邮件登录的用户显示不同的屏幕。 Is it possible to get the sign in method form the package firebase authentication?是否可以从包 firebase 身份验证中获取登录方法?
I know I can fix this by using firestore & checking if a statement is true or false.我知道我可以通过使用 firestore 来解决这个问题并检查语句是真是假。 But that will cost me a read every time a user opens the app...但是,每次用户打开应用程序时,我都会花时间阅读......
Firebase has a special property providerId
. Firebase 有一个特殊的属性providerId
。 But, as mentioned @GazihanAlankus it always returns firebase
.但是,正如@GazihanAlankus 提到的,它总是返回firebase
。
And, the property firebaseUser.providerData[1].providerId
sometimes not exists (for example when user used anonymous login).而且,属性firebaseUser.providerData[1].providerId
有时不存在(例如,当用户使用匿名登录时)。
So, we should use appropriate approaches, for example:因此,我们应该使用适当的方法,例如:
FirebaseUser user = await _firebaseAuth.currentUser();
if (user.providerData.length < 2) {
// do something
}
else {
print(res.providerId);
}
The list of values, that are returned by property providerId
:由属性providerId
返回的值列表:
EmailAuthProviderID: password
PhoneAuthProviderID: phone
GoogleAuthProviderID: google.com
FacebookAuthProviderID: facebook.com
TwitterAuthProviderID: twitter.com
GitHubAuthProviderID: github.com
AppleAuthProviderID: apple.com
YahooAuthProviderID: yahoo.com
MicrosoftAuthProviderID: hotmail.com
I got this list from the cool research here What is the full list of provider id's for firebase.UserInfo.providerId?我从这里的酷研究中得到了这个列表Firebase.UserInfo.providerId 的提供者 ID 的完整列表是什么?
This seems to be what you want: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#getProviderData()这似乎是你想要的: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#getProviderData()
In my app where I use Google logins only, I have firebaseUser.providerData[1].providerId == 'google.com'
.在我仅使用 Google 登录的应用中,我有firebaseUser.providerData[1].providerId == 'google.com'
。
Btw, firebaseUser.providerData[0].providerId == 'firebase'
.顺便说一句, firebaseUser.providerData[0].providerId == 'firebase'
。
I guess you could check them all and look for what providers you get for different kinds of users.我想您可以检查所有这些并寻找您为不同类型的用户获得的提供商。
Edit: here's what I get when logging in with e-mail: https://postimg.cc/BXWGGN6h编辑:这是我使用电子邮件登录时得到的信息: https ://postimg.cc/BXWGGN6h
In my app I used FirebaseAuth.instance.currentUser.providerData[0].providerId == 'google.com'.
在我的应用中,我使用了FirebaseAuth.instance.currentUser.providerData[0].providerId == 'google.com'.
, cause providerData[1] doesn't contain any value ,导致 providerData[1] 不包含任何值
But that will cost me a read every time a user opens the app.但每次用户打开应用程序时,我都会花时间阅读。 THIS IS TRUTH!这是事实!
Alternatively, you can create your own app DB using SQFLite , and create only one table (user)
in that, having a field of signUpMethod
having possible values are google, facebook and mail
.或者,您可以使用SQFLite创建自己的应用程序数据库,并在其中仅创建一个表(user)
,其中的signUpMethod
字段可能值为google, facebook and mail
。 Whenever you opens the app, first check that in your db, if this is mail
, redirect to another screen which you want, else call firebase service
每当您打开应用程序时,首先在您的数据库中检查,如果这是mail
,则重定向到您想要的另一个屏幕,否则调用firebase service
Cheers!干杯!
For anyone reading this in 2022, Firebase now has nice new docs for Flutter and in it they have this, which I personally found super useful:对于任何在 2022 年阅读本文的人,Firebase 现在有很好的 Flutter 新文档,其中有这个,我个人认为它非常有用:
if (user != null) {
for (final providerProfile in user.providerData) {
// ID of the provider (google.com, apple.cpm, etc.)
final provider = providerProfile.providerId;
// UID specific to the provider
final uid = providerProfile.uid;
// Name, email address, and profile photo URL
final name = providerProfile.displayName;
final emailAddress = providerProfile.email;
final profilePhoto = providerProfile.photoURL;
}
}
Source: Firebase documentation资料来源: Firebase 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.