aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/users/user-tokens.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-06 15:25:19 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-08-07 08:55:02 +0200
commita4ff3100d36f2fe9a4dfc00e8487c28a94433c4f (patch)
tree1744c9db32b765b1511d4a69a7ba7b525dc0f619 /client/src/root-helpers/users/user-tokens.ts
parenta02b93ce756d646a59cef57b5e4ff53c2bb30bec (diff)
downloadPeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.gz
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.zst
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.zip
Cleanup tokens logic in embed
Diffstat (limited to 'client/src/root-helpers/users/user-tokens.ts')
-rw-r--r--client/src/root-helpers/users/user-tokens.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.ts
new file mode 100644
index 000000000..d42e1c8f3
--- /dev/null
+++ b/client/src/root-helpers/users/user-tokens.ts
@@ -0,0 +1,61 @@
1import { peertubeLocalStorage } from '../peertube-web-storage'
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
17 accessToken: string
18 refreshToken: string
19 tokenType: string
20
21 static load () {
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) {
45 this.accessToken = hash.accessToken
46 this.refreshToken = hash.refreshToken
47
48 if (hash.tokenType === 'bearer') {
49 this.tokenType = 'Bearer'
50 } else {
51 this.tokenType = hash.tokenType
52 }
53 }
54 }
55
56 save () {
57 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
58 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
59 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
60 }
61}