简体   繁体   English

如何保留 Firebase Auth 并在以后使用它登录?

[英]How to persist Firebase Auth and use it to login later?

I want the feature in my app, that a user can login with multiple accounts and switch between these accounts without having to enter the email and password again.我想要我的应用程序中的功能,即用户可以使用多个帐户登录并在这些帐户之间切换,而无需再次输入 email 和密码。

One obvious approach that works is to persist the email-password-pairs.一种明显有效的方法是保留电子邮件密码对。 But that is highly insecure.但这是非常不安全的。

So instead I would prefer to persist the session / token of that account.因此,我宁愿保留该帐户的 session / 令牌。 And when switching the account, we logout from that account, get the session / token from the other account and use that to login.切换帐户时,我们从该帐户注销,从另一个帐户获取 session / 令牌并使用它登录。 Unfortunately I just cannot find where to get that session / token.不幸的是,我就是找不到从哪里获得 session / 令牌。 AFAIK Firebase does what I described above internally (they persist the session / token to the SharedPreferences, and use that to authenticate the user on app start). AFAIK Firebase 在内部执行我上面描述的操作(他们将 session / 令牌保存到 SharedPreferences,并使用它在应用程序启动时对用户进行身份验证)。

So how can I do that myself?那么我自己该怎么做呢? (My platform is Android) (我的平台是安卓)

From the documentation on Authenticate with Firebase Using a Custom Authentication System来自使用自定义身份验证系统使用 Firebase进行身份验证的文档

You can integrate Firebase Authentication with a custom authentication system by modifying your authentication server to produce custom signed tokens when a user successfully signs in. Your app receives this token and uses it to authenticate with Firebase.您可以将 Firebase 身份验证与自定义身份验证系统集成,方法是修改您的身份验证服务器以在用户成功登录时生成自定义签名令牌。您的应用程序接收此令牌并使用它与 Firebase 进行身份验证。

When users sign in to your app, send their sign-in credentials (for example, their username and password) to your authentication server.当用户登录您的应用程序时,将他们的登录凭据(例如,他们的用户名和密码)发送到您的身份验证服务器。 Your server checks the credentials and returns a custom token if they are valid.Refer this document on create custom tokens您的服务器检查凭据并返回自定义令牌(如果有效)。请参阅此文档以创建自定义令牌

After you receive the custom token from your authentication server, pass it to signInWithCustomToken to sign in the user:从身份验证服务器收到自定义令牌后,将其传递给 signInWithCustomToken 以登录用户:

 import { getAuth, signInWithCustomToken } from "firebase/auth"; const auth = getAuth(); signInWithCustomToken(auth, token).then((userCredential) => { // Signed in const user = userCredential.user; //... }).catch((error) => { const errorCode = error.code; const errorMessage = error.message; //... });

You can also check this stackoverflow thread您还可以查看此 stackoverflow线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 持久化用户Auth Flutter Firebase - Persist user Auth Flutter Firebase 如何在前端和后端之间持久化 Firebase Auth state? (Firebase 身份验证 + React + Express) - How to persist the Firebase Auth state between front-end and backend ? (Firebase Auth + React + Express) 如何正确使用firebase auth - How to use firebase auth correctly 如何在 React Native EXPO 中使用 FireBase 正确保留登录 state? - How to properly persist login state using FireBase in React Native EXPO? Flutter:如何使用邮箱验证FIrebase Auth - Flutter : How to use Email Verification FIrebase Auth 如何在反应中使用 firebase auth object? - How to use firebase auth object in react? 苹果登录 android FireBase Auth 有问题 - Apple login in android with FireBase Auth has problem Angular Firebase auth,如何检查用户是否登录以隐藏导航中的登录链接? - Angular Firebase auth, how to check if user is logged in to hide login link from navigation? firebase.auth.Auth.signInWithCredential 已弃用。 请改用 firebase.auth.Auth.signInAndRetrieveDataWithCredential - firebase.auth.Auth.signInWithCredential is deprecated. Please use firebase.auth.Auth.signInAndRetrieveDataWithCredential instead 如何在 react(+reactfire) 中使用 firebase 持久化用户身份验证 - How to persist the user authentication with firebase in react(+reactfire)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM