diff options
author | Chocobozzz <me@florianbigard.com> | 2018-03-23 14:26:20 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-03-23 14:26:20 +0100 |
commit | 0bd78bf30b2ae159791bdc90d17ed18e0327f621 (patch) | |
tree | e8f69cef499a99c51deeea39f7197b7ff93a80a2 /client/src/app/core/auth/auth-user.model.ts | |
parent | 9c673970f66fe91c665e1e3905fa768f57e11a18 (diff) | |
download | PeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.tar.gz PeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.tar.zst PeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.zip |
Proxify local storage and handle if it is unavailable
Diffstat (limited to 'client/src/app/core/auth/auth-user.model.ts')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 9ad275392..366eea110 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' | ||
2 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | ||
1 | // Do not use the barrel (dependency loop) | 3 | // Do not use the barrel (dependency loop) |
2 | import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' | 4 | import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' |
3 | import { User, UserConstructorHash } from '../../shared/users/user.model' | 5 | import { User, UserConstructorHash } from '../../shared/users/user.model' |
4 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | ||
5 | 6 | ||
6 | export type TokenOptions = { | 7 | export type TokenOptions = { |
7 | accessToken: string | 8 | accessToken: string |
@@ -22,9 +23,9 @@ class Tokens { | |||
22 | tokenType: string | 23 | tokenType: string |
23 | 24 | ||
24 | static load () { | 25 | static load () { |
25 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN) | 26 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) |
26 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN) | 27 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) |
27 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE) | 28 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) |
28 | 29 | ||
29 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | 30 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { |
30 | return new Tokens({ | 31 | return new Tokens({ |
@@ -38,9 +39,9 @@ class Tokens { | |||
38 | } | 39 | } |
39 | 40 | ||
40 | static flush () { | 41 | static flush () { |
41 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN) | 42 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) |
42 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN) | 43 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) |
43 | localStorage.removeItem(this.KEYS.TOKEN_TYPE) | 44 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) |
44 | } | 45 | } |
45 | 46 | ||
46 | constructor (hash?: TokenOptions) { | 47 | constructor (hash?: TokenOptions) { |
@@ -57,9 +58,9 @@ class Tokens { | |||
57 | } | 58 | } |
58 | 59 | ||
59 | save () { | 60 | save () { |
60 | localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | 61 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) |
61 | localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | 62 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) |
62 | localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | 63 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) |
63 | } | 64 | } |
64 | } | 65 | } |
65 | 66 | ||
@@ -76,16 +77,16 @@ export class AuthUser extends User { | |||
76 | tokens: Tokens | 77 | tokens: Tokens |
77 | 78 | ||
78 | static load () { | 79 | static load () { |
79 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) | 80 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) |
80 | if (usernameLocalStorage) { | 81 | if (usernameLocalStorage) { |
81 | return new AuthUser( | 82 | return new AuthUser( |
82 | { | 83 | { |
83 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), | 84 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), |
84 | username: localStorage.getItem(this.KEYS.USERNAME), | 85 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), |
85 | email: localStorage.getItem(this.KEYS.EMAIL), | 86 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), |
86 | role: parseInt(localStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | 87 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, |
87 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true', | 88 | displayNSFW: peertubeLocalStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true', |
88 | autoPlayVideo: localStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' | 89 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' |
89 | }, | 90 | }, |
90 | Tokens.load() | 91 | Tokens.load() |
91 | ) | 92 | ) |
@@ -95,12 +96,12 @@ export class AuthUser extends User { | |||
95 | } | 96 | } |
96 | 97 | ||
97 | static flush () { | 98 | static flush () { |
98 | localStorage.removeItem(this.KEYS.USERNAME) | 99 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) |
99 | localStorage.removeItem(this.KEYS.ID) | 100 | peertubeLocalStorage.removeItem(this.KEYS.ID) |
100 | localStorage.removeItem(this.KEYS.ROLE) | 101 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) |
101 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW) | 102 | peertubeLocalStorage.removeItem(this.KEYS.DISPLAY_NSFW) |
102 | localStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) | 103 | peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) |
103 | localStorage.removeItem(this.KEYS.EMAIL) | 104 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) |
104 | Tokens.flush() | 105 | Tokens.flush() |
105 | } | 106 | } |
106 | 107 | ||
@@ -131,12 +132,12 @@ export class AuthUser extends User { | |||
131 | } | 132 | } |
132 | 133 | ||
133 | save () { | 134 | save () { |
134 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | 135 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
135 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | 136 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) |
136 | localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | 137 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) |
137 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | 138 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) |
138 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) | 139 | peertubeLocalStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) |
139 | localStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) | 140 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) |
140 | this.tokens.save() | 141 | this.tokens.save() |
141 | } | 142 | } |
142 | } | 143 | } |