feature: auto refresh user token when token exp in 7 days (#644)

This commit is contained in:
Dream Hunter
2025-04-26 21:22:26 +08:00
committed by GitHub
parent 327962432a
commit 101a561894
7 changed files with 32 additions and 4 deletions

View File

@@ -116,7 +116,7 @@ export default {
user_email: email,
user_id: user_id,
// 90 days expire in seconds
exp: Math.floor(Date.now() / 1000) + 90 * 24 * 60 * 60,
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256")
return c.json({

View File

@@ -194,7 +194,7 @@ export default {
user_email: user_email,
user_id: user_id,
// 90 days expire in seconds
exp: Math.floor(Date.now() / 1000) + 90 * 24 * 60 * 60,
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256")
return c.json({

View File

@@ -55,10 +55,21 @@ export default {
// 1 hour
exp: Math.floor(Date.now() / 1000) + 3600,
}, c.env.JWT_SECRET, "HS256") : null;
// create new if expired in 7 days
const new_user_token = user.exp > (
Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60
) ? null : await Jwt.sign({
user_email: user.user_email,
user_id: user.user_id,
// 30 days expire in seconds
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256");
return c.json({
...user,
is_admin: is_admin,
access_token: access_token,
new_user_token: new_user_token,
user_role: user_role
});
},

View File

@@ -173,7 +173,7 @@ export default {
user_email: email,
user_id: user_id,
// 90 days expire in seconds
exp: Math.floor(Date.now() / 1000) + 90 * 24 * 60 * 60,
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256")
return c.json({