Skip to content

Commit 077ea3c

Browse files
chore: address coderabbit
1 parent a2b349c commit 077ea3c

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/store/authStore.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ export const useAuthStore = defineStore({
3232
actions: {
3333
setToken(token: string): void {
3434
try {
35-
const payload = JSON.parse(globalThis.atob(token.split('.')[1] ?? ''))
36-
if (!payload) throw new Error('Empty payload')
35+
const part = token.split('.')[1] ?? ''
36+
// base64url -> base64 with padding
37+
const b64 = part.replace(/-/g, '+').replace(/_/g, '/')
38+
.padEnd(Math.ceil(part.length / 4) * 4, '=')
39+
const payload: any = JSON.parse(globalThis.atob(b64))
40+
if (!payload || typeof payload !== 'object') throw new Error('Empty/invalid payload')
41+
42+
// Reject expired tokens
43+
const now = Math.floor(Date.now() / 1000)
44+
if (typeof payload.exp === 'number' && payload.exp < now) {
45+
throw new Error('Token expired')
46+
}
47+
3748
this.isLoggedIn = true
38-
this.userId = payload.user_id
49+
this.userId = payload.user_id ?? payload.sub ?? ''
3950
this.username = payload.username ?? 'Guest'
40-
// TODO: validate `exp`, `iat`, etc. – sign out if expired
51+
// Optional: persist token for session restore (guarded for web/tauri)
52+
try { if (typeof localStorage !== 'undefined') localStorage.setItem('cv_token', token) } catch {}
4153
} catch (err) {
4254
console.error('[authStore] Invalid JWT:', err)
43-
this.signOut() // ensure clean state
55+
this.signOut()
4456
return
4557
}
4658
},

v0/src/store/authStore.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ export const useAuthStore = defineStore({
3232
actions: {
3333
setToken(token: string): void {
3434
try {
35-
const payload = JSON.parse(globalThis.atob(token.split('.')[1] ?? ''))
36-
if (!payload) throw new Error('Empty payload')
35+
const part = token.split('.')[1] ?? ''
36+
// base64url -> base64 with padding
37+
const b64 = part.replace(/-/g, '+').replace(/_/g, '/')
38+
.padEnd(Math.ceil(part.length / 4) * 4, '=')
39+
const payload: any = JSON.parse(globalThis.atob(b64))
40+
if (!payload || typeof payload !== 'object') throw new Error('Empty/invalid payload')
41+
42+
// Reject expired tokens
43+
const now = Math.floor(Date.now() / 1000)
44+
if (typeof payload.exp === 'number' && payload.exp < now) {
45+
throw new Error('Token expired')
46+
}
47+
3748
this.isLoggedIn = true
38-
this.userId = payload.user_id
49+
this.userId = payload.user_id ?? payload.sub ?? ''
3950
this.username = payload.username ?? 'Guest'
40-
// TODO: validate `exp`, `iat`, etc. – sign out if expired
51+
// Optional: persist token for session restore (guarded for web/tauri)
52+
try { if (typeof localStorage !== 'undefined') localStorage.setItem('cv_token', token) } catch {}
4153
} catch (err) {
4254
console.error('[authStore] Invalid JWT:', err)
43-
this.signOut() // ensure clean state
55+
this.signOut()
4456
return
4557
}
4658
},

v1/src/store/authStore.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ export const useAuthStore = defineStore({
3232
actions: {
3333
setToken(token: string): void {
3434
try {
35-
const payload = JSON.parse(globalThis.atob(token.split('.')[1] ?? ''))
36-
if (!payload) throw new Error('Empty payload')
35+
const part = token.split('.')[1] ?? ''
36+
// base64url -> base64 with padding
37+
const b64 = part.replace(/-/g, '+').replace(/_/g, '/')
38+
.padEnd(Math.ceil(part.length / 4) * 4, '=')
39+
const payload: any = JSON.parse(globalThis.atob(b64))
40+
if (!payload || typeof payload !== 'object') throw new Error('Empty/invalid payload')
41+
42+
// Reject expired tokens
43+
const now = Math.floor(Date.now() / 1000)
44+
if (typeof payload.exp === 'number' && payload.exp < now) {
45+
throw new Error('Token expired')
46+
}
47+
3748
this.isLoggedIn = true
38-
this.userId = payload.user_id
49+
this.userId = payload.user_id ?? payload.sub ?? ''
3950
this.username = payload.username ?? 'Guest'
40-
// TODO: validate `exp`, `iat`, etc. – sign out if expired
51+
// Optional: persist token for session restore (guarded for web/tauri)
52+
try { if (typeof localStorage !== 'undefined') localStorage.setItem('cv_token', token) } catch {}
4153
} catch (err) {
4254
console.error('[authStore] Invalid JWT:', err)
43-
this.signOut() // ensure clean state
55+
this.signOut()
4456
return
4557
}
4658
},

0 commit comments

Comments
 (0)