All files / src/utils TokenUtils.ts

97.61% Statements 41/42
87.5% Branches 7/8
90% Functions 9/10
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            71x 71x 71x 71x 71x 71x 71x 71x   1373x 71x 71x 71x 71x 71x 71x 71x   71x 16x 16x 16x 16x 16x 16x 16x 16x     71x 10x 10x 10x 10x 10x 10x 10x 10x                              
import { LoginResultProps } from "@utils/FetchUtilsProps";
import { OrganizationInfo } from "@props/RecordProps";
 
/**
 * localStorage 中存放的 tokens 的 keys
 */
export const AccessTokenLocalStorageKey = 'tech.muyan.access_token';
export const RefreshTokenLocalStorageKey = 'tech.muyan.refresh_token';
export const UsernameLocalStorageKey = 'tech.muyan.username';
export const NameLocalStorageKey = 'tech.muyan.name';
export const OrganizationLocalStorageKey = 'tech.muyan.organization';
export const UserAvatarLocalStorageKey = 'tech.muyan.avatar';
export const UserIdLocalStorageKey = 'tech.muyan.user_id';
export const DefaultPageLocalStorageKey = 'tech.muyan.defaultPage';
 
const getAccessToken = (): string | null => localStorage.getItem(AccessTokenLocalStorageKey);
const getRefreshToken = (): string | null => localStorage.getItem(RefreshTokenLocalStorageKey);
const getUsername = (): string | null => localStorage.getItem(UsernameLocalStorageKey);
const getName = (): string | null => localStorage.getItem(NameLocalStorageKey);
const getAvatar = (): string | null => localStorage.getItem(UserAvatarLocalStorageKey);
const getOrganization = (): OrganizationInfo => JSON.parse(localStorage.getItem(OrganizationLocalStorageKey) ?? "{}");
const getUserId = (): string | null => localStorage.getItem(UserIdLocalStorageKey);
const getDefaultPage = (): string | null => localStorage.getItem(DefaultPageLocalStorageKey);
 
const saveTokens = (jwt: LoginResultProps): void => {
    localStorage.setItem(AccessTokenLocalStorageKey, jwt.access_token);
    localStorage.setItem(RefreshTokenLocalStorageKey, jwt.refresh_token);
    localStorage.setItem(UsernameLocalStorageKey, jwt.username);
    localStorage.setItem(NameLocalStorageKey, jwt.name ?? '');
    localStorage.setItem(OrganizationLocalStorageKey, JSON.stringify(jwt.organization));
    localStorage.setItem(UserAvatarLocalStorageKey, jwt.avatar ?? '');
    localStorage.setItem(UserIdLocalStorageKey, jwt.id.toString());
    localStorage.setItem(DefaultPageLocalStorageKey, jwt.defaultPage ?? '');
};
 
const clearTokens = (): void => {
    localStorage.removeItem(AccessTokenLocalStorageKey);
    localStorage.removeItem(RefreshTokenLocalStorageKey);
    localStorage.removeItem(UsernameLocalStorageKey);
    localStorage.removeItem(NameLocalStorageKey);
    localStorage.removeItem(OrganizationLocalStorageKey);
    localStorage.removeItem(UserAvatarLocalStorageKey);
    localStorage.removeItem(UserIdLocalStorageKey);
    localStorage.removeItem(DefaultPageLocalStorageKey);
};
 
export {
    saveTokens,
    getAccessToken,
    getRefreshToken,
    clearTokens,
    getUsername,
    getName,
    getOrganization,
    getAvatar,
    getUserId,
    getDefaultPage,
};