diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-06 15:25:19 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-07 08:55:02 +0200 |
commit | a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f (patch) | |
tree | 1744c9db32b765b1511d4a69a7ba7b525dc0f619 /client/src/app/core/auth | |
parent | a02b93ce756d646a59cef57b5e4ff53c2bb30bec (diff) | |
download | PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.gz PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.zst PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.zip |
Cleanup tokens logic in embed
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 5679d28a9..34efa24fc 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,18 +1,22 @@ | |||
1 | import { Observable, of } from 'rxjs' | 1 | import { Observable, of } from 'rxjs' |
2 | import { map } from 'rxjs/operators' | 2 | import { map } from 'rxjs/operators' |
3 | import { User } from '@app/core/users/user.model' | 3 | import { User } from '@app/core/users/user.model' |
4 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | 4 | import { |
5 | flushUserInfoFromLocalStorage, | ||
6 | getUserInfoFromLocalStorage, | ||
7 | saveUserInfoIntoLocalStorage, | ||
8 | TokenOptions, | ||
9 | Tokens | ||
10 | } from '@root-helpers/users' | ||
5 | import { hasUserRight } from '@shared/core-utils/users' | 11 | import { hasUserRight } from '@shared/core-utils/users' |
6 | import { | 12 | import { |
7 | MyUser as ServerMyUserModel, | 13 | MyUser as ServerMyUserModel, |
8 | MyUserSpecialPlaylist, | 14 | MyUserSpecialPlaylist, |
9 | NSFWPolicyType, | ||
10 | User as ServerUserModel, | 15 | User as ServerUserModel, |
11 | UserRight, | 16 | UserRight, |
12 | UserRole, | 17 | UserRole, |
13 | UserVideoQuota | 18 | UserVideoQuota |
14 | } from '@shared/models' | 19 | } from '@shared/models' |
15 | import { TokenOptions, Tokens } from '@root-helpers/pure-auth-user.model' | ||
16 | 20 | ||
17 | export class AuthUser extends User implements ServerMyUserModel { | 21 | export class AuthUser extends User implements ServerMyUserModel { |
18 | tokens: Tokens | 22 | tokens: Tokens |
@@ -21,31 +25,16 @@ export class AuthUser extends User implements ServerMyUserModel { | |||
21 | canSeeVideosLink = true | 25 | canSeeVideosLink = true |
22 | 26 | ||
23 | static load () { | 27 | static load () { |
24 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) | 28 | const userInfo = getUserInfoFromLocalStorage() |
25 | if (usernameLocalStorage) { | 29 | |
26 | return new AuthUser( | 30 | if (!userInfo) return null |
27 | { | ||
28 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), | ||
29 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), | ||
30 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), | ||
31 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | ||
32 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, | ||
33 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', | ||
34 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', | ||
35 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | ||
36 | }, | ||
37 | Tokens.load() | ||
38 | ) | ||
39 | } | ||
40 | 31 | ||
41 | return null | 32 | return new AuthUser(userInfo, Tokens.load()) |
42 | } | 33 | } |
43 | 34 | ||
44 | static flush () { | 35 | static flush () { |
45 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) | 36 | flushUserInfoFromLocalStorage() |
46 | peertubeLocalStorage.removeItem(this.KEYS.ID) | 37 | |
47 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | ||
48 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) | ||
49 | Tokens.flush() | 38 | Tokens.flush() |
50 | } | 39 | } |
51 | 40 | ||
@@ -87,13 +76,16 @@ export class AuthUser extends User implements ServerMyUserModel { | |||
87 | } | 76 | } |
88 | 77 | ||
89 | save () { | 78 | save () { |
90 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | 79 | saveUserInfoIntoLocalStorage({ |
91 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | 80 | id: this.id, |
92 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | 81 | username: this.username, |
93 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | 82 | email: this.email, |
94 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) | 83 | role: this.role, |
95 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) | 84 | nsfwPolicy: this.nsfwPolicy, |
96 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) | 85 | webTorrentEnabled: this.webTorrentEnabled, |
86 | autoPlayVideo: this.autoPlayVideo | ||
87 | }) | ||
88 | |||
97 | this.tokens.save() | 89 | this.tokens.save() |
98 | } | 90 | } |
99 | 91 | ||