简体   繁体   English

如何在 Ionic 4 中使用 Firebase 处理刷新令牌?

[英]How to handle refresh token with Firebase in Ionic 4?

I'm trying to implement firebase as Auth mechanism in my Ionic 4 app.我正在尝试在我的 Ionic 4 应用程序中实现 firebase 作为身份验证机制。 I'm using this code to let the user signIn with email and password:我正在使用此代码让用户使用电子邮件和密码登录:

    loginUser(value){
   return new Promise<any>((resolve, reject) => {
     firebase.auth().signInWithEmailAndPassword(value.email, value.password)
     .then(
       res => resolve(res),
       err => reject(err))
   })
  }

Since Firebase works with accessToken that expires after 1 hour of the creation and with refreshToken to refresh the first one, I'm not sure how to move on;由于 Firebase 使用在创建 1 小时后过期的 accessToken 并使用 refreshToken 刷新第一个,我不知道如何继续; I mean should I save both accessToken and refreshToken in my database?我的意思是我应该在我的数据库中保存 accessToken 和 refreshToken 吗? How to refresh the token?如何刷新令牌? Is this operation done automatically or should I do something?这个操作是自动完成的还是我应该做些什么? I'm using also a JavaEE server, which detect if the user is correctly logged with Firebase, by passing the accessToken.我还使用了一个 JavaEE 服务器,它通过传递 accessToken 来检测用户是否使用 Firebase 正确登录。

should I save both accessToken and refreshToken in my database?我应该在我的数据库中保存 accessToken 和 refreshToken 吗?

Instead of persisting the tokens yourself, tell Firebase where to persist the authentication information with Auth.setPersistence() .不要自己保留令牌,而是使用Auth.setPersistence()告诉 Firebase 在哪里保留身份验证信息。 For example:例如:

firebase.auth().setPersistence(fire.localPersistence())

Be sure to test if this is even needed though, as on most environments Firebase will automatically determine where to persist the authentication state.不过,请务必测试是否需要这样做,因为在大多数环境中,Firebase 会自动确定在何处保留身份验证状态。


How to refresh the token?如何刷新令牌? Is this operation done automatically or should I do something?这个操作是自动完成的还是我应该做些什么?

The ID token is automatically refreshed by the Firebase SDK, about 55 minutes after it was created. ID 令牌会在创建后约 55 分钟由 Firebase SDK 自动刷新。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM