aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core
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/app/core
parenta02b93ce756d646a59cef57b5e4ff53c2bb30bec (diff)
downloadPeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.gz
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.zst
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.zip
Cleanup tokens logic in embed
Diffstat (limited to 'client/src/app/core')
-rw-r--r--client/src/app/core/auth/auth-user.model.ts54
-rw-r--r--client/src/app/core/theme/theme.service.ts16
-rw-r--r--client/src/app/core/users/user.model.ts3
-rw-r--r--client/src/app/core/users/user.service.ts55
4 files changed, 54 insertions, 74 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 @@
1import { Observable, of } from 'rxjs' 1import { Observable, of } from 'rxjs'
2import { map } from 'rxjs/operators' 2import { map } from 'rxjs/operators'
3import { User } from '@app/core/users/user.model' 3import { User } from '@app/core/users/user.model'
4import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' 4import {
5 flushUserInfoFromLocalStorage,
6 getUserInfoFromLocalStorage,
7 saveUserInfoIntoLocalStorage,
8 TokenOptions,
9 Tokens
10} from '@root-helpers/users'
5import { hasUserRight } from '@shared/core-utils/users' 11import { hasUserRight } from '@shared/core-utils/users'
6import { 12import {
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'
15import { TokenOptions, Tokens } from '@root-helpers/pure-auth-user.model'
16 20
17export class AuthUser extends User implements ServerMyUserModel { 21export 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
diff --git a/client/src/app/core/theme/theme.service.ts b/client/src/app/core/theme/theme.service.ts
index 7e05fdddd..4c4611d01 100644
--- a/client/src/app/core/theme/theme.service.ts
+++ b/client/src/app/core/theme/theme.service.ts
@@ -1,13 +1,13 @@
1import { first } from 'rxjs/operators' 1import { first } from 'rxjs/operators'
2import { Injectable } from '@angular/core' 2import { Injectable } from '@angular/core'
3import { UserLocalStorageKeys } from '@root-helpers/users'
4import { ServerConfig, ServerConfigTheme } from '@shared/models'
5import { environment } from '../../../environments/environment'
3import { AuthService } from '../auth' 6import { AuthService } from '../auth'
4import { PluginService } from '../plugins/plugin.service' 7import { PluginService } from '../plugins/plugin.service'
5import { ServerService } from '../server' 8import { ServerService } from '../server'
6import { LocalStorageService } from '../wrappers/storage.service'
7import { User } from '../users/user.model'
8import { UserService } from '../users/user.service' 9import { UserService } from '../users/user.service'
9import { ServerConfig, ServerConfigTheme } from '@shared/models' 10import { LocalStorageService } from '../wrappers/storage.service'
10import { environment } from '../../../environments/environment'
11 11
12@Injectable() 12@Injectable()
13export class ThemeService { 13export class ThemeService {
@@ -111,9 +111,9 @@ export class ThemeService {
111 111
112 this.pluginService.reloadLoadedScopes() 112 this.pluginService.reloadLoadedScopes()
113 113
114 this.localStorageService.setItem(User.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme), false) 114 this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false)
115 } else { 115 } else {
116 this.localStorageService.removeItem(User.KEYS.LAST_ACTIVE_THEME, false) 116 this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false)
117 } 117 }
118 118
119 this.oldThemeName = currentTheme 119 this.oldThemeName = currentTheme
@@ -127,7 +127,7 @@ export class ThemeService {
127 if (!this.auth.isLoggedIn()) { 127 if (!this.auth.isLoggedIn()) {
128 this.updateCurrentTheme() 128 this.updateCurrentTheme()
129 129
130 this.localStorageService.watch([ User.KEYS.THEME ]).subscribe( 130 this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe(
131 () => this.updateCurrentTheme() 131 () => this.updateCurrentTheme()
132 ) 132 )
133 } 133 }
@@ -138,7 +138,7 @@ export class ThemeService {
138 } 138 }
139 139
140 private loadAndSetFromLocalStorage () { 140 private loadAndSetFromLocalStorage () {
141 const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.LAST_ACTIVE_THEME) 141 const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME)
142 if (!lastActiveThemeString) return 142 if (!lastActiveThemeString) return
143 143
144 try { 144 try {
diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.ts
index f0d3a08b8..7c9569ed4 100644
--- a/client/src/app/core/users/user.model.ts
+++ b/client/src/app/core/users/user.model.ts
@@ -10,11 +10,8 @@ import {
10 UserRole, 10 UserRole,
11 VideoChannel 11 VideoChannel
12} from '@shared/models' 12} from '@shared/models'
13import { UserKeys } from '@root-helpers/user-keys'
14 13
15export class User implements UserServerModel { 14export class User implements UserServerModel {
16 static KEYS = UserKeys
17
18 id: number 15 id: number
19 username: string 16 username: string
20 email: string 17 email: string
diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts
index c98b3844c..aac2a0206 100644
--- a/client/src/app/core/users/user.service.ts
+++ b/client/src/app/core/users/user.service.ts
@@ -1,4 +1,3 @@
1import { has } from 'lodash-es'
2import { BytesPipe } from 'ngx-pipes' 1import { BytesPipe } from 'ngx-pipes'
3import { SortMeta } from 'primeng/api' 2import { SortMeta } from 'primeng/api'
4import { from, Observable, of } from 'rxjs' 3import { from, Observable, of } from 'rxjs'
@@ -7,6 +6,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'
7import { Injectable } from '@angular/core' 6import { Injectable } from '@angular/core'
8import { AuthService } from '@app/core/auth' 7import { AuthService } from '@app/core/auth'
9import { I18n } from '@ngx-translate/i18n-polyfill' 8import { I18n } from '@ngx-translate/i18n-polyfill'
9import { UserLocalStorageKeys } from '@root-helpers/users'
10import { 10import {
11 Avatar, 11 Avatar,
12 NSFWPolicyType, 12 NSFWPolicyType,
@@ -81,37 +81,28 @@ export class UserService {
81 } 81 }
82 82
83 updateMyAnonymousProfile (profile: UserUpdateMe) { 83 updateMyAnonymousProfile (profile: UserUpdateMe) {
84 const supportedKeys = { 84 try {
85 // local storage keys 85 this.localStorageService.setItem(UserLocalStorageKeys.NSFW_POLICY, profile.nsfwPolicy)
86 nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val), 86 this.localStorageService.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, profile.webTorrentEnabled)
87 webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)),
88 autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)),
89 autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)),
90 theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val),
91 videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)),
92 87
93 // session storage keys 88 this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, profile.autoPlayNextVideo)
94 autoPlayNextVideo: (val: boolean) => 89 this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, profile.autoPlayNextVideoPlaylist)
95 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val))
96 }
97 90
98 for (const key of Object.keys(profile)) { 91 this.localStorageService.setItem(UserLocalStorageKeys.THEME, profile.theme)
99 try { 92 this.localStorageService.setItem(UserLocalStorageKeys.VIDEO_LANGUAGES, profile.videoLanguages)
100 if (has(supportedKeys, key)) supportedKeys[key](profile[key]) 93 } catch (err) {
101 } catch (err) { 94 console.error(`Cannot set item in localStorage. Likely due to a value impossible to stringify.`, err)
102 console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err)
103 }
104 } 95 }
105 } 96 }
106 97
107 listenAnonymousUpdate () { 98 listenAnonymousUpdate () {
108 return this.localStorageService.watch([ 99 return this.localStorageService.watch([
109 User.KEYS.NSFW_POLICY, 100 UserLocalStorageKeys.NSFW_POLICY,
110 User.KEYS.WEBTORRENT_ENABLED, 101 UserLocalStorageKeys.WEBTORRENT_ENABLED,
111 User.KEYS.AUTO_PLAY_VIDEO, 102 UserLocalStorageKeys.AUTO_PLAY_VIDEO,
112 User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, 103 UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
113 User.KEYS.THEME, 104 UserLocalStorageKeys.THEME,
114 User.KEYS.VIDEO_LANGUAGES 105 UserLocalStorageKeys.VIDEO_LANGUAGES
115 ]).pipe( 106 ]).pipe(
116 throttleTime(200), 107 throttleTime(200),
117 filter(() => this.authService.isLoggedIn() !== true), 108 filter(() => this.authService.isLoggedIn() !== true),
@@ -269,7 +260,7 @@ export class UserService {
269 let videoLanguages: string[] 260 let videoLanguages: string[]
270 261
271 try { 262 try {
272 videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES)) 263 videoLanguages = JSON.parse(this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES))
273 } catch (err) { 264 } catch (err) {
274 videoLanguages = null 265 videoLanguages = null
275 console.error('Cannot parse desired video languages from localStorage.', err) 266 console.error('Cannot parse desired video languages from localStorage.', err)
@@ -277,16 +268,16 @@ export class UserService {
277 268
278 return new User({ 269 return new User({
279 // local storage keys 270 // local storage keys
280 nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType, 271 nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
281 webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false', 272 webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false',
282 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default', 273 theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',
283 videoLanguages, 274 videoLanguages,
284 275
285 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', 276 autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
286 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true', 277 autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
287 278
288 // session storage keys 279 // session storage keys
289 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' 280 autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
290 }) 281 })
291 } 282 }
292 283