aboutsummaryrefslogtreecommitdiffhomepage
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts12
-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
-rw-r--r--client/src/root-helpers/index.ts3
-rw-r--r--client/src/root-helpers/pure-auth-user.model.ts123
-rw-r--r--client/src/root-helpers/users/index.ts3
-rw-r--r--client/src/root-helpers/users/user-local-storage-keys.ts (renamed from client/src/root-helpers/user-keys.ts)2
-rw-r--r--client/src/root-helpers/users/user-local-storage-manager.ts55
-rw-r--r--client/src/root-helpers/users/user-tokens.ts61
-rw-r--r--client/src/standalone/videos/embed.ts26
12 files changed, 199 insertions, 214 deletions
diff --git a/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts b/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts
index 016975341..d2372023f 100644
--- a/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts
+++ b/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts
@@ -7,6 +7,7 @@ import { VideoPlaylist } from '@app/shared/shared-video-playlist'
7import { I18n } from '@ngx-translate/i18n-polyfill' 7import { I18n } from '@ngx-translate/i18n-polyfill'
8import { RecommendationInfo } from './recommendation-info.model' 8import { RecommendationInfo } from './recommendation-info.model'
9import { RecommendedVideosStore } from './recommended-videos.store' 9import { RecommendedVideosStore } from './recommended-videos.store'
10import { UserLocalStorageKeys } from '@root-helpers/users'
10 11
11@Component({ 12@Component({
12 selector: 'my-recommended-videos', 13 selector: 'my-recommended-videos',
@@ -48,9 +49,12 @@ export class RecommendedVideosComponent implements OnInit, OnChanges {
48 if (this.authService.isLoggedIn()) { 49 if (this.authService.isLoggedIn()) {
49 this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo 50 this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo
50 } else { 51 } else {
51 this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false 52 this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
52 this.sessionStorageService.watch([User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe( 53
53 () => this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' 54 this.sessionStorageService.watch([UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe(
55 () => {
56 this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
57 }
54 ) 58 )
55 } 59 }
56 60
@@ -73,7 +77,7 @@ export class RecommendedVideosComponent implements OnInit, OnChanges {
73 } 77 }
74 78
75 switchAutoPlayNextVideo () { 79 switchAutoPlayNextVideo () {
76 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) 80 this.sessionStorageService.setItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
77 81
78 if (this.authService.isLoggedIn()) { 82 if (this.authService.isLoggedIn()) {
79 const details = { 83 const details = {
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
diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts
index 5ed4933f1..59468b31c 100644
--- a/client/src/root-helpers/index.ts
+++ b/client/src/root-helpers/index.ts
@@ -1,4 +1,3 @@
1export * from './users'
1export * from './peertube-web-storage' 2export * from './peertube-web-storage'
2export * from './utils' 3export * from './utils'
3export * from './user-keys'
4export * from './pure-auth-user.model'
diff --git a/client/src/root-helpers/pure-auth-user.model.ts b/client/src/root-helpers/pure-auth-user.model.ts
deleted file mode 100644
index 81226da01..000000000
--- a/client/src/root-helpers/pure-auth-user.model.ts
+++ /dev/null
@@ -1,123 +0,0 @@
1// pure version of auth-user, that doesn't import app packages
2import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
3import {
4 MyUser as ServerMyUserModel,
5 MyUserSpecialPlaylist,
6 NSFWPolicyType,
7 UserRole
8} from '@shared/models'
9import { UserKeys } from '@root-helpers/user-keys'
10
11export type TokenOptions = {
12 accessToken: string
13 refreshToken: string
14 tokenType: string
15}
16
17// Private class only used by User
18export class Tokens {
19 private static KEYS = {
20 ACCESS_TOKEN: 'access_token',
21 REFRESH_TOKEN: 'refresh_token',
22 TOKEN_TYPE: 'token_type'
23 }
24
25 accessToken: string
26 refreshToken: string
27 tokenType: string
28
29 static load () {
30 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
31 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
32 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
33
34 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
35 return new Tokens({
36 accessToken: accessTokenLocalStorage,
37 refreshToken: refreshTokenLocalStorage,
38 tokenType: tokenTypeLocalStorage
39 })
40 }
41
42 return null
43 }
44
45 static flush () {
46 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
47 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
48 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
49 }
50
51 constructor (hash?: TokenOptions) {
52 if (hash) {
53 this.accessToken = hash.accessToken
54 this.refreshToken = hash.refreshToken
55
56 if (hash.tokenType === 'bearer') {
57 this.tokenType = 'Bearer'
58 } else {
59 this.tokenType = hash.tokenType
60 }
61 }
62 }
63
64 save () {
65 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
66 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
67 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
68 }
69}
70
71export class PureAuthUser {
72 tokens: Tokens
73 specialPlaylists: MyUserSpecialPlaylist[]
74
75 canSeeVideosLink = true
76
77 static load () {
78 const usernameLocalStorage = peertubeLocalStorage.getItem(UserKeys.USERNAME)
79 if (usernameLocalStorage) {
80 return new PureAuthUser(
81 {
82 id: parseInt(peertubeLocalStorage.getItem(UserKeys.ID), 10),
83 username: peertubeLocalStorage.getItem(UserKeys.USERNAME),
84 email: peertubeLocalStorage.getItem(UserKeys.EMAIL),
85 role: parseInt(peertubeLocalStorage.getItem(UserKeys.ROLE), 10) as UserRole,
86 nsfwPolicy: peertubeLocalStorage.getItem(UserKeys.NSFW_POLICY) as NSFWPolicyType,
87 webTorrentEnabled: peertubeLocalStorage.getItem(UserKeys.WEBTORRENT_ENABLED) === 'true',
88 autoPlayVideo: peertubeLocalStorage.getItem(UserKeys.AUTO_PLAY_VIDEO) === 'true',
89 videosHistoryEnabled: peertubeLocalStorage.getItem(UserKeys.VIDEOS_HISTORY_ENABLED) === 'true'
90 },
91 Tokens.load()
92 )
93 }
94
95 return null
96 }
97
98 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
99 this.tokens = new Tokens(hashTokens)
100 this.specialPlaylists = userHash.specialPlaylists
101 }
102
103 getAccessToken () {
104 return this.tokens.accessToken
105 }
106
107 getRefreshToken () {
108 return this.tokens.refreshToken
109 }
110
111 getTokenType () {
112 return this.tokens.tokenType
113 }
114
115 refreshTokens (accessToken: string, refreshToken: string) {
116 this.tokens.accessToken = accessToken
117 this.tokens.refreshToken = refreshToken
118 }
119
120 save () {
121 this.tokens.save()
122 }
123}
diff --git a/client/src/root-helpers/users/index.ts b/client/src/root-helpers/users/index.ts
new file mode 100644
index 000000000..8fbaca9e3
--- /dev/null
+++ b/client/src/root-helpers/users/index.ts
@@ -0,0 +1,3 @@
1export * from './user-local-storage-keys'
2export * from './user-local-storage-manager'
3export * from './user-tokens'
diff --git a/client/src/root-helpers/user-keys.ts b/client/src/root-helpers/users/user-local-storage-keys.ts
index 897be8c43..5f915899c 100644
--- a/client/src/root-helpers/user-keys.ts
+++ b/client/src/root-helpers/users/user-local-storage-keys.ts
@@ -1,4 +1,4 @@
1export const UserKeys = { 1export const UserLocalStorageKeys = {
2 ID: 'id', 2 ID: 'id',
3 ROLE: 'role', 3 ROLE: 'role',
4 EMAIL: 'email', 4 EMAIL: 'email',
diff --git a/client/src/root-helpers/users/user-local-storage-manager.ts b/client/src/root-helpers/users/user-local-storage-manager.ts
new file mode 100644
index 000000000..c75cea127
--- /dev/null
+++ b/client/src/root-helpers/users/user-local-storage-manager.ts
@@ -0,0 +1,55 @@
1import { NSFWPolicyType, UserRole } from '@shared/models'
2import { peertubeLocalStorage } from '../peertube-web-storage'
3import { UserLocalStorageKeys } from './user-local-storage-keys'
4
5function getUserInfoFromLocalStorage () {
6 const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)
7
8 if (!usernameLocalStorage) return undefined
9
10 return {
11 id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),
12 username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),
13 email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),
14 role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
15 nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
16 webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',
17 autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
18 videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'
19 }
20}
21
22function flushUserInfoFromLocalStorage () {
23 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)
24 peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)
25 peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)
26 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)
27 peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)
28 peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)
29 peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
30 peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)
31}
32
33function saveUserInfoIntoLocalStorage (info: {
34 id: number
35 username: string
36 email: string
37 role: UserRole
38 nsfwPolicy: NSFWPolicyType
39 webTorrentEnabled: boolean
40 autoPlayVideo: boolean
41}) {
42 peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())
43 peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)
44 peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)
45 peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())
46 peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())
47 peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))
48 peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))
49}
50
51export {
52 getUserInfoFromLocalStorage,
53 saveUserInfoIntoLocalStorage,
54 flushUserInfoFromLocalStorage
55}
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}
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts
index 89903aa35..8b00be790 100644
--- a/client/src/standalone/videos/embed.ts
+++ b/client/src/standalone/videos/embed.ts
@@ -1,6 +1,7 @@
1import './embed.scss' 1import './embed.scss'
2import videojs from 'video.js' 2import videojs from 'video.js'
3import { objectToUrlEncoded, peertubeLocalStorage, PureAuthUser } from '@root-helpers/index' 3import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index'
4import { Tokens } from '@root-helpers/users'
4import { peertubeTranslate } from '../../../../shared/core-utils/i18n' 5import { peertubeTranslate } from '../../../../shared/core-utils/i18n'
5import { 6import {
6 ResultList, 7 ResultList,
@@ -39,7 +40,7 @@ export class PeerTubeEmbed {
39 mode: PlayerMode 40 mode: PlayerMode
40 scope = 'peertube' 41 scope = 'peertube'
41 42
42 user: PureAuthUser 43 userTokens: Tokens
43 headers = new Headers() 44 headers = new Headers()
44 LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { 45 LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
45 CLIENT_ID: 'client_id', 46 CLIENT_ID: 'client_id',
@@ -74,7 +75,7 @@ export class PeerTubeEmbed {
74 const headers = new Headers() 75 const headers = new Headers()
75 headers.set('Content-Type', 'application/x-www-form-urlencoded') 76 headers.set('Content-Type', 'application/x-www-form-urlencoded')
76 const data = { 77 const data = {
77 refresh_token: this.user.getRefreshToken(), 78 refresh_token: this.userTokens.refreshToken,
78 client_id: clientId, 79 client_id: clientId,
79 client_secret: clientSecret, 80 client_secret: clientSecret,
80 response_type: 'code', 81 response_type: 'code',
@@ -88,9 +89,12 @@ export class PeerTubeEmbed {
88 }) 89 })
89 .then(res => res.json()) 90 .then(res => res.json())
90 .then((obj: UserRefreshToken) => { 91 .then((obj: UserRefreshToken) => {
91 this.user.refreshTokens(obj.access_token, obj.refresh_token) 92 this.userTokens.accessToken = obj.access_token
92 this.user.save() 93 this.userTokens.refreshToken = obj.refresh_token
93 this.headers.set('Authorization', `${this.user.getTokenType()} ${this.user.getAccessToken()}`) 94 this.userTokens.save()
95
96 this.setHeadersFromTokens()
97
94 resolve() 98 resolve()
95 }) 99 })
96 .catch((refreshTokenError: any) => { 100 .catch((refreshTokenError: any) => {
@@ -165,7 +169,7 @@ export class PeerTubeEmbed {
165 169
166 async init () { 170 async init () {
167 try { 171 try {
168 this.user = PureAuthUser.load() 172 this.userTokens = Tokens.load()
169 await this.initCore() 173 await this.initCore()
170 } catch (e) { 174 } catch (e) {
171 console.error(e) 175 console.error(e)
@@ -218,9 +222,7 @@ export class PeerTubeEmbed {
218 const urlParts = window.location.pathname.split('/') 222 const urlParts = window.location.pathname.split('/')
219 const videoId = urlParts[ urlParts.length - 1 ] 223 const videoId = urlParts[ urlParts.length - 1 ]
220 224
221 if (this.user) { 225 if (this.userTokens) this.setHeadersFromTokens()
222 this.headers.set('Authorization', `${this.user.getTokenType()} ${this.user.getAccessToken()}`)
223 }
224 226
225 const videoPromise = this.loadVideoInfo(videoId) 227 const videoPromise = this.loadVideoInfo(videoId)
226 const captionsPromise = this.loadVideoCaptions(videoId) 228 const captionsPromise = this.loadVideoCaptions(videoId)
@@ -381,6 +383,10 @@ export class PeerTubeEmbed {
381 private getPlaceholderElement () { 383 private getPlaceholderElement () {
382 return document.getElementById('placeholder-preview') 384 return document.getElementById('placeholder-preview')
383 } 385 }
386
387 private setHeadersFromTokens () {
388 this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`)
389 }
384} 390}
385 391
386PeerTubeEmbed.main() 392PeerTubeEmbed.main()