File size: 1,745 Bytes
b3e4ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d73f1da
b3e4ba6
 
 
 
 
 
 
 
 
 
d73f1da
b3e4ba6
 
d73f1da
b3e4ba6
 
d73f1da
b3e4ba6
 
 
 
 
 
 
 
d73f1da
b3e4ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * tokenStore.js
 * ─────────────
 * Access token persistence layer.
 *
 * Strategy:
 *   • sessionStorage  → token survives same-tab page reloads
 *   • httpOnly cookie → refresh token is sent automatically by the browser
 *     on /api/auth/refresh calls (set by backend, never readable from JS)
 *
 * On a fresh tab / after tab close:
 *   sessionStorage is empty → auth store calls refreshSession() → backend
 *   validates the httpOnly refresh cookie → issues a new access token.
 */

const SESSION_KEY = 'at'  // short key, value is already opaque JWT

/** Return stored access token, or null if absent. */
export function getAccessToken() {
  try {
    return sessionStorage.getItem(SESSION_KEY) || localStorage.getItem(SESSION_KEY) || null
  } catch {
    return null
  }
}

/** Persist access token for the current tab session. */
export function setTokens(accessToken) {
  try {
    if (accessToken) {
      sessionStorage.setItem(SESSION_KEY, accessToken)
      localStorage.setItem(SESSION_KEY, accessToken)
    } else {
      sessionStorage.removeItem(SESSION_KEY)
      localStorage.removeItem(SESSION_KEY)
    }
  } catch {
    /* sessionStorage unavailable */
  }
  _clearLegacy()
}

/** Remove access token from storage (called on logout). */
export function clearTokens() {
  try {
    sessionStorage.removeItem(SESSION_KEY)
    localStorage.removeItem(SESSION_KEY)
  } catch {
    /* ignore */
  }
  _clearLegacy()
}

/** Remove old localStorage keys left by previous implementation. */
function _clearLegacy() {
  try {
    localStorage.removeItem('access_token')
    localStorage.removeItem('refresh_token')
    localStorage.removeItem('token')
  } catch {
    /* ignore */
  }
}