Enhance JWT Token Management and Authentication Flow (#720)

- Implement extended token lifetime for "Remember Me" functionality
- Add token expiration details to authentication responses
- Update client-side token handling to support dynamic expiration
- Modify authentication middleware to handle token initialization more robustly
- Configure JWT configuration to support longer token lifetimes
This commit is contained in:
Julien Nahum
2025-03-10 17:32:17 +08:00
committed by GitHub
parent 06328a47ab
commit a5162192b1
10 changed files with 63 additions and 20 deletions

28
client/stores/auth.js vendored
View File

@@ -21,12 +21,22 @@ export const useAuthStore = defineStore("auth", {
},
// Stop admin impersonation
stopImpersonating() {
this.setToken(this.admin_token)
// When stopping impersonation, we don't have expiration info for the admin token
// Use a default long expiration (24 hours) to ensure the admin can continue working
this.setToken(this.admin_token, 60 * 60 * 24)
this.setAdminToken(null)
},
setToken(token) {
this.setCookie("token", token)
setToken(token, expiresIn) {
// Set cookie with expiration if provided
const cookieOptions = {}
if (expiresIn) {
// expiresIn is in seconds, maxAge also needs to be in seconds
cookieOptions.maxAge = expiresIn
}
this.setCookie("token", token, cookieOptions)
this.token = token
},
@@ -35,9 +45,9 @@ export const useAuthStore = defineStore("auth", {
this.admin_token = token
},
setCookie(name, value) {
setCookie(name, value, options = {}) {
if (import.meta.client) {
useCookie(name).value = value
useCookie(name, options).value = value
}
},
@@ -49,7 +59,8 @@ export const useAuthStore = defineStore("auth", {
setUser(user) {
if (!user) {
console.error("No user, logging out.")
this.setToken(null)
// When logging out due to no user, clear the token with maxAge 0
this.setToken(null, 0)
}
this.user = user
@@ -73,7 +84,10 @@ export const useAuthStore = defineStore("auth", {
opnFetch("logout", { method: "POST" }).catch(() => {})
this.user = null
this.setToken(null)
// Clear the token cookie by setting maxAge to 0
this.setCookie("token", null, { maxAge: 0 })
this.token = null
},
// async fetchOauthUrl() {