aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/users/user-tokens.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers/users/user-tokens.ts')
-rw-r--r--client/src/root-helpers/users/user-tokens.ts69
1 files changed, 27 insertions, 42 deletions
diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.ts
index d42e1c8f3..a6d614cb7 100644
--- a/client/src/root-helpers/users/user-tokens.ts
+++ b/client/src/root-helpers/users/user-tokens.ts
@@ -1,46 +1,11 @@
1import { peertubeLocalStorage } from '../peertube-web-storage' 1import { UserTokenLocalStorageKeys } from './user-local-storage-keys'
2
3export type TokenOptions = {
4 accessToken: string
5 refreshToken: string
6 tokenType: string
7}
8
9// Private class only used by User
10export class Tokens {
11 private static KEYS = {
12 ACCESS_TOKEN: 'access_token',
13 REFRESH_TOKEN: 'refresh_token',
14 TOKEN_TYPE: 'token_type'
15 }
16 2
3export class UserTokens {
17 accessToken: string 4 accessToken: string
18 refreshToken: string 5 refreshToken: string
19 tokenType: string 6 tokenType: string
20 7
21 static load () { 8 constructor (hash?: Partial<UserTokens>) {
22 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
23 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
24 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
25
26 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
27 return new Tokens({
28 accessToken: accessTokenLocalStorage,
29 refreshToken: refreshTokenLocalStorage,
30 tokenType: tokenTypeLocalStorage
31 })
32 }
33
34 return null
35 }
36
37 static flush () {
38 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
39 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
40 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
41 }
42
43 constructor (hash?: TokenOptions) {
44 if (hash) { 9 if (hash) {
45 this.accessToken = hash.accessToken 10 this.accessToken = hash.accessToken
46 this.refreshToken = hash.refreshToken 11 this.refreshToken = hash.refreshToken
@@ -53,9 +18,29 @@ export class Tokens {
53 } 18 }
54 } 19 }
55 20
56 save () { 21 static getUserTokens (localStorage: Pick<Storage, 'getItem'>) {
57 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) 22 const accessTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)
58 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) 23 const refreshTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)
59 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) 24 const tokenTypeLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.TOKEN_TYPE)
25
26 if (!accessTokenLocalStorage || !refreshTokenLocalStorage || !tokenTypeLocalStorage) return null
27
28 return new UserTokens({
29 accessToken: accessTokenLocalStorage,
30 refreshToken: refreshTokenLocalStorage,
31 tokenType: tokenTypeLocalStorage
32 })
33 }
34
35 static saveToLocalStorage (localStorage: Pick<Storage, 'setItem'>, tokens: UserTokens) {
36 localStorage.setItem(UserTokenLocalStorageKeys.ACCESS_TOKEN, tokens.accessToken)
37 localStorage.setItem(UserTokenLocalStorageKeys.REFRESH_TOKEN, tokens.refreshToken)
38 localStorage.setItem(UserTokenLocalStorageKeys.TOKEN_TYPE, tokens.tokenType)
39 }
40
41 static flushLocalStorage (localStorage: Pick<Storage, 'removeItem'>) {
42 localStorage.removeItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)
43 localStorage.removeItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)
44 localStorage.removeItem(UserTokenLocalStorageKeys.TOKEN_TYPE)
60 } 45 }
61} 46}