为啥我必须在用户登录后使用 firebase 对用户进行身份验证?

     2023-02-23     179

关键词:

【中文标题】为啥我必须在用户登录后使用 firebase 对用户进行身份验证?【英文标题】:Why do I have to authenticate the user with firebase after he has already signed-in?为什么我必须在用户登录后使用 firebase 对用户进行身份验证? 【发布时间】:2020-04-28 21:26:03 【问题描述】:

我已经按照documentation 在我的带有firebase 身份验证的android 应用中实现了google 登录。但是,我仍在尝试理解代码及其背后的逻辑。 因此,在用户使用他的 gmail 帐户成功登录后,将调用方法 firebaseAuthWithGoogle 并将帐户信息作为参数传递:

firebaseAuthWithGoogle(account); 

这是它的定义:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) 
        // We will put the data coming from the google account in MySQL database
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() 
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) 
                    if (task.isSuccessful()) 
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mFirebaseAuth.getCurrentUser();

                     else 
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());

                    

                    // ...
                
            );

我已经尝试根据documentation向自己解释这一点:

用户成功登录后,

GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account); // Calling firebaseAuthWithGoogle

GoogleSignInAccount 对象 获取 ID 令牌, 将其换成 Firebase 凭据

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

并通过 Firebase 进行身份验证

mFirebaseAuth.signInWithCredential(credential)

使用 Firebase 凭据

对我来说,100% 清楚 不明白的是: 1. 如果用户已经登录,为什么我必须通过 Firebase 进行身份验证? 2. 如果用户已经登录,Firebase 身份验证的作用是什么? 3. 如果用户已经登录,使用 Firebase 凭据登录意味着什么?

我知道这对你们中的一些人来说可能听起来微不足道,但对我来说,整个登录流程非常模糊,尤其是使用 Firebase 身份验证。

【问题讨论】:

【参考方案1】:

当您使用 firebase 进行身份验证时,您可以通过执行以下操作访问当前登录的用户:

FirebaseUser user = mFirebaseAuth.getCurrentUser();

通过检索 user,您还可以检索可用于连接到 Firebase 数据库的 userId

您还可以在再次打开应用程序时检查用户是否仍然登录:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) 
    // User is signed in
 else 
    // No user is signed in

并在用户登录后重定向到页面

【讨论】:

谢谢。这就是使用 Firebase 身份验证的全部意义所在? Firebase 凭据登录是关于什么的?它与用户的初始登录不同吗?或者它是初始登录。这更令人困惑。

Flutter firebase 登录用户在登录后返回 NULL currentUser

】Flutterfirebase登录用户在登录后返回NULLcurrentUser【英文标题】:FlutterfirebaseloggedinuserreturnsaNULLcurrentUseraftersignin【发布时间】:2021-03-2102:35:34【问题描述】:在使用signInWithEmailAndPassword()API在firebase中进行身份验证时,我无法获得... 查看详情

使用firebase和flutter登录google后获取用户详细信息

】使用firebase和flutter登录google后获取用户详细信息【英文标题】:getuserdetailsaftersigninginwithgoogleusingfirebase&flutter【发布时间】:2021-08-2907:36:06【问题描述】:在用户使用Firebase在Flutter中使用Google签名后,我试图获取用户详细信... 查看详情

firebase身份验证登录必须允许单个设备登录(代码片段)

我正在使用Firebase后端开发应用程序,我正在使用FirebaseAuth登录我的应用程序。我做了所有集成,每件事和我的应用程序都运行良好。但是我想要单个用户的单个会话,因为单个userId我可以通过多个设备登录。所以我想限制用户... 查看详情

React/Firebase 托管:为啥在页面刷新时要求用户登录?

】React/Firebase托管:为啥在页面刷新时要求用户登录?【英文标题】:React/FirebaseHosting:Whyonapagerefreshtheuserisbeingaskedtologin?React/Firebase托管:为什么在页面刷新时要求用户登录?【发布时间】:2018-09-2103:41:37【问题描述】:我有一... 查看详情

我可以让 Firebase 使用用户名登录过程吗?

】我可以让Firebase使用用户名登录过程吗?【英文标题】:CanImakeFirebaseuseausernameloginprocess?【发布时间】:2016-09-2908:35:55【问题描述】:我想制作一个允许用户名登录的系统。此过程需要以下内容:用户必须使用电子邮件/密码注... 查看详情

如何在使用 redux 登录 facebook/expo 和 firebase 后显示用户数据

】如何在使用redux登录facebook/expo和firebase后显示用户数据【英文标题】:HowtoshowtheUserDataafterlogginginwithfacebook/expoandfirebaseusingredux【发布时间】:2017-12-1202:41:25【问题描述】:目标:1.我想将我的用户的public_profile数据保存到firebase... 查看详情

使用 firebase google 退出后,我再次登录。我保留在数据库中的所有数据都被删除

】使用firebasegoogle退出后,我再次登录。我保留在数据库中的所有数据都被删除【英文标题】:Aftersigningoutwithfirebasegoogle,Isigninagain.AllthedataIkeepinthedatabaseisdeleted【发布时间】:2021-10-1816:33:49【问题描述】:我希望它使用google登录... 查看详情

使用 Angular 和 Firebase 登录后获取用户配置文件

】使用Angular和Firebase登录后获取用户配置文件【英文标题】:GettingauserprofileafterloginwithAngularandFirebase【发布时间】:2021-10-3123:47:03【问题描述】:我了解Firebase身份验证仅用于对用户进行身份验证,而不是存储任何用户数据/配置... 查看详情

登录 Firebase React Native Expo 后无法获取用户数据

】登录FirebaseReactNativeExpo后无法获取用户数据【英文标题】:Can\'tgetuserdataafterlogginginFirebaseReactNativeExpo【发布时间】:2021-09-2202:23:12【问题描述】:我正在尝试使用ReactNative/Expo和Firebase创建一个社交媒体应用程序,并且我已经成... 查看详情

我正在尝试使用 Framework-7 Vuejs 创建一个应用程序。即使在使用 Firebase 退出应用程序后,我也想让我的用户保持登录状态

...在尝试使用Framework-7Vuejs创建一个应用程序。即使在使用Firebase退出应用程序后,我也想让我的用户保持登录状态【英文标题】:I\'mtryingtocreateanappusingFramework-7Vuejs.IwanttomakemyuserstayloggedinevenafterexitingtheappusingFirebase【发布时间】:20... 查看详情

如何确保用户使用 Firebase 和 Flutter 登录

】如何确保用户使用Firebase和Flutter登录【英文标题】:HowtomakesureuserisloggedinusingFirebaseandFlutter【发布时间】:2020-11-2513:57:44【问题描述】:我想检查用户是否登录然后转到主页,如果没有登录则转到登录页面。我面临的问题是,... 查看详情

用户在 Vue.js 和 Firebase 中注册后更改导航栏的状态

】用户在Vue.js和Firebase中注册后更改导航栏的状态【英文标题】:ChangingstateofnavbarafterusersignsupinVue.jsandFirebase【发布时间】:2021-10-1922:03:52【问题描述】:我目前正在开发一个简单的Vue.js应用程序,用户可以在其中注册和登录。... 查看详情

装的ubuntu系统,开机后不能用root用户进入,这是为啥?

用非root用户就可以进,这是为什么?那么想在开机后可以用root进系统,在什么机子上可以呢?请高手指点!多谢!Ubuntu的root用户限制不能在图形界面登陆,只能在命令行模式登陆(启动菜单里选有recoverymode的项)。普通用户也... 查看详情

如何使用 Firebase 进行“使用 Spotify 登录”身份验证系统?

】如何使用Firebase进行“使用Spotify登录”身份验证系统?【英文标题】:Howtomakea\'loginwithSpotify\'authenticationsystemusingFirebase?【发布时间】:2016-08-2814:29:19【问题描述】:我有一个网页,我想让它仅在用户使用他们的Spotify帐户登录... 查看详情

Firebase 身份验证 - 登录用户为空

】Firebase身份验证-登录用户为空【英文标题】:FirebaseAuthentication-LoggedInUserIsNull【发布时间】:2021-04-0519:41:33【问题描述】:我已经设法使用Firebase设置了一个自定义登录系统。用户输入电子邮件/密码并被重定向到主页(这是私... 查看详情

在 Flutter 中使用 Firebase 然后(Instagram)进行正确的登录架构

】在Flutter中使用Firebase然后(Instagram)进行正确的登录架构【英文标题】:ProperloginarchitecturewithFirebasethen(Instagram)inFlutter【发布时间】:2020-09-1110:53:03【问题描述】:让我解释一下我要做什么。我有一个使用Firebase对用户进行身... 查看详情

Facebook 登录 - Firebase V3

】Facebook登录-FirebaseV3【英文标题】:FacebookLogin-FirebaseV3【发布时间】:2016-05-2112:35:32【问题描述】:我正在使用新版本的FirebaseSDK,只是想添加Facebook登录。我可以通过Facebook登录用户,但这不会在实时数据库中创建记录。在以... 查看详情

在 React.JS 中刷新后,如何让用户保持登录到 firebase?

】在React.JS中刷新后,如何让用户保持登录到firebase?【英文标题】:HowdoIkeepauserloggedintofirebaseafterrefreshinReact.JS?【发布时间】:2018-07-2901:54:08【问题描述】:安装组件时,我想渲染登录屏幕或应用程序,具体取决于用户是否登录... 查看详情