]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/root-helpers/users/user-tokens.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / client / src / root-helpers / users / user-tokens.ts
index d42e1c8f321822bb1383b760ad217b6fd2031065..a6d614cb7b3419501cfbf0edeb8f8fdd47e22169 100644 (file)
@@ -1,46 +1,11 @@
-import { peertubeLocalStorage } from '../peertube-web-storage'
-
-export type TokenOptions = {
-  accessToken: string
-  refreshToken: string
-  tokenType: string
-}
-
-// Private class only used by User
-export class Tokens {
-  private static KEYS = {
-    ACCESS_TOKEN: 'access_token',
-    REFRESH_TOKEN: 'refresh_token',
-    TOKEN_TYPE: 'token_type'
-  }
+import { UserTokenLocalStorageKeys } from './user-local-storage-keys'
 
+export class UserTokens {
   accessToken: string
   refreshToken: string
   tokenType: string
 
-  static load () {
-    const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
-    const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
-    const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
-
-    if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
-      return new Tokens({
-        accessToken: accessTokenLocalStorage,
-        refreshToken: refreshTokenLocalStorage,
-        tokenType: tokenTypeLocalStorage
-      })
-    }
-
-    return null
-  }
-
-  static flush () {
-    peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
-    peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
-    peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
-  }
-
-  constructor (hash?: TokenOptions) {
+  constructor (hash?: Partial<UserTokens>) {
     if (hash) {
       this.accessToken = hash.accessToken
       this.refreshToken = hash.refreshToken
@@ -53,9 +18,29 @@ export class Tokens {
     }
   }
 
-  save () {
-    peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
-    peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
-    peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.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)
   }
 }