[英]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.