简体   繁体   English

如何使用 firebase 身份验证为自定义后端生成新的 ID 令牌?

[英]How to generate new id tokens for custom backend with firebase auth?

We have built a custom nodejs backend but the authentication is using firebase auth with idtoken, the idtokens expire after 1 hour and the user is automatically logged out.我们已经构建了一个自定义的 nodejs 后端,但身份验证使用带有 idtoken 的 firebase auth,idtokens 在 1 小时后过期并且用户自动注销。 When using firestore this is handled automatically, we have seen solutions that suggest a service worker but that has not worked.当使用 firestore 时,这是自动处理的,我们已经看到建议使用 service worker 但没有奏效的解决方案。

Can someone please suggest a stable solution for this may be a middleware on the backend API's that can regenerate the tokens?有人可以建议一个稳定的解决方案吗?这可能是后端 API 上可以重新生成令牌的中间件?

Thanks谢谢

The user is not logged out and that is why Firestore keeps working.用户未注销,这就是 Firestore 继续工作的原因。 You can use getIdToken() method again to get user's ID Token and then pass it in API request.您可以再次使用getIdToken()方法获取用户的 ID Token,然后将其传递到 API 请求中。

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
  // Send token to your backend via HTTPS
  // ...
}).catch(function(error) {
  // Handle error
});

The normal approach (which the Firebase services themselves use too) is to always the current token with each request to the backend service, so that the service has at least 5m to complete the request (which is a lot more than most services need).正常方法(Firebase 服务本身也使用)是始终将当前令牌与每个请求发送到后端服务,以便服务至少有 5m 来完成请求(这比大多数服务需要的多得多)。

If you need a token that can be used for longer, you can consider forcing a refresh of the token before you call the service as Dharmaraj pointed out in their answer.如果您需要一个可以使用更长时间的令牌,您可以考虑在调用服务之前强制刷新令牌,正如 Dharmaraj 在他们的回答中指出的那样。

Alternative, you can switch to using session cookies for the user, which can have an expiration of up to two weeks.或者,您可以为用户切换为使用 session cookies ,最长有效期为两周。

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

相关问题 为自定义 apollo graphql 后端处理 firebase 初始化延迟和 id 令牌 - Handling firebase initialization delay and id tokens for custom apollo graphql backend 为什么Firebase Auth既有refresh token又有ID token? - Why does Firebase Auth have both refresh tokens and ID tokens? 将使用 Firebase Auth 创建的用户同步到我的自定义后端 - Synchronize users created with Firebase Auth to my custom backend Firebase 访问权限和 id 令牌 - Firebase acces and id tokens Firebase Auth:如何使用id token登录? - Firebase Auth: How to sign in using id token? 如何在前端和后端之间持久化 Firebase Auth state? (Firebase 身份验证 + React + Express) - How to persist the Firebase Auth state between front-end and backend ? (Firebase Auth + React + Express) 如何实现 Firebase 自定义认证后端? - How to implement Firebase custom authentication backend? 使用 FireBase Auth Emulator 创建和验证 (JWT) 令牌 - Creating and validating (JWT)-tokens with FireBase Auth Emulator 关于 Firebase Auth 的 UID 与自定义后端服务器的 UID 的最佳实践 - Best practice regarding Firebase Auth's UID vs custom backend server's UID 如何通过 Firebase 设置自定义身份验证声明并识别平台 - How to set custom auth claims through Firebase and identify platform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM