blob: a6d614cb7b3419501cfbf0edeb8f8fdd47e22169 (
plain) (
blame)
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
|
import { UserTokenLocalStorageKeys } from './user-local-storage-keys'
export class UserTokens {
accessToken: string
refreshToken: string
tokenType: string
constructor (hash?: Partial<UserTokens>) {
if (hash) {
this.accessToken = hash.accessToken
this.refreshToken = hash.refreshToken
if (hash.tokenType === 'bearer') {
this.tokenType = 'Bearer'
} else {
this.tokenType = hash.tokenType
}
}
}
static getUserTokens (localStorage: Pick<Storage, 'getItem'>) {
const accessTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)
const refreshTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)
const tokenTypeLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.TOKEN_TYPE)
if (!accessTokenLocalStorage || !refreshTokenLocalStorage || !tokenTypeLocalStorage) return null
return new UserTokens({
accessToken: accessTokenLocalStorage,
refreshToken: refreshTokenLocalStorage,
tokenType: tokenTypeLocalStorage
})
}
static saveToLocalStorage (localStorage: Pick<Storage, 'setItem'>, tokens: UserTokens) {
localStorage.setItem(UserTokenLocalStorageKeys.ACCESS_TOKEN, tokens.accessToken)
localStorage.setItem(UserTokenLocalStorageKeys.REFRESH_TOKEN, tokens.refreshToken)
localStorage.setItem(UserTokenLocalStorageKeys.TOKEN_TYPE, tokens.tokenType)
}
static flushLocalStorage (localStorage: Pick<Storage, 'removeItem'>) {
localStorage.removeItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)
localStorage.removeItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)
localStorage.removeItem(UserTokenLocalStorageKeys.TOKEN_TYPE)
}
}
|