firebase / firebase/firebase-admin-node

Session cookie refresh

未关闭
#2,349 4 条评论 7 个 reaction 已指派 0 人 在 GitHub 查看
needs-triage type: feature request
主要语言
TypeScript
星标
1.7k
派生
419
平均合并
3 天 10 小时
30 天内合并 PR
16

描述

Hello I'm using Firebase Auth (actually gcp identity platform) with the session cookie in a NextJS app, everything is working fine but I'm looking a way to refresh the cookie to avoid forcing the user to login again when the session expire. There isn't an official way to do that but I might have found a solution, I'm just not 100% sure this is secure enough or if there are issues I'm missing, the idea is to have a refresh endpoint with this code:

```
const idToken = req.cookies.session || req.body.sessionCookie;
const session = await auth().verifySessionCookie(idToken, true);

if (session) {
const customToken = await auth().createCustomToken(session.uid);

const response = await fetch(
`${GOOGLE_API}:signInWithCustomToken?key=${process.env.NEXT_PUBLIC_API_KEY}`,
{
method: "POST",
cache: "no-cache",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: customToken, returnSecureToken: true }),
},
);

const result = await response.json();
const sessionCookie = await auth().createSessionCookie(result.idToken, {
expiresIn,
});

setCookie(res, "session", sessionCookie, {
...cookieOptions,
maxAge: expiresIn,
});

return res.json({ session, customToken, sessionCookie });
}
```

then every time I want I can call this refresh endpoint and the session will be updated...it seems to work but I'd like to have some feedback on this.

贡献指南

打开贡献指南

调研方向

描述的唯一入口点是提议的刷新端点,它读取会话 cookie 或请求体中的值,并创建新的会话 cookie。请先审查 Firebase Auth 的会话 cookie 流程以及该端点的安全影响;只有在确定了一种受支持且明确安全的刷新方案及其预期行为后,该 issue 才算完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
firebase, google-cloud, javascript, next.js
领域
authentication, backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。