Blame view

美国版/Food Labeling Management Platform/src/lib/authStorage.ts 813 Bytes
699ea6e8   杨鑫   完善打印逻辑
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
  export const ACCESS_TOKEN_KEY = "access_token";
  export const REFRESH_TOKEN_KEY = "refresh_token";
  
  export function getAccessToken(): string | null {
    try {
      return localStorage.getItem(ACCESS_TOKEN_KEY) ?? localStorage.getItem("token") ?? null;
    } catch {
      return null;
    }
  }
  
  export function setTokens(input: { token: string; refreshToken?: string | null }) {
    try {
      localStorage.setItem(ACCESS_TOKEN_KEY, input.token);
      if (input.refreshToken) localStorage.setItem(REFRESH_TOKEN_KEY, input.refreshToken);
    } catch {
      // ignore storage failures (private mode / disabled storage)
    }
  }
  
  export function clearTokens() {
    try {
      localStorage.removeItem(ACCESS_TOKEN_KEY);
      localStorage.removeItem("token");
      localStorage.removeItem(REFRESH_TOKEN_KEY);
    } catch {
      // ignore
    }
  }