diff options
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 44 | ||||
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 16 |
2 files changed, 11 insertions, 49 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index f10b37e5a..cd9665e37 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,13 +1,7 @@ | |||
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 { | 4 | import { UserTokens } from '@root-helpers/users' |
5 | flushUserInfoFromLocalStorage, | ||
6 | getUserInfoFromLocalStorage, | ||
7 | saveUserInfoIntoLocalStorage, | ||
8 | TokenOptions, | ||
9 | Tokens | ||
10 | } from '@root-helpers/users' | ||
11 | import { hasUserRight } from '@shared/core-utils/users' | 5 | import { hasUserRight } from '@shared/core-utils/users' |
12 | import { | 6 | import { |
13 | MyUser as ServerMyUserModel, | 7 | MyUser as ServerMyUserModel, |
@@ -19,31 +13,15 @@ import { | |||
19 | } from '@shared/models' | 13 | } from '@shared/models' |
20 | 14 | ||
21 | export class AuthUser extends User implements ServerMyUserModel { | 15 | export class AuthUser extends User implements ServerMyUserModel { |
22 | tokens: Tokens | 16 | tokens: UserTokens |
23 | specialPlaylists: MyUserSpecialPlaylist[] | 17 | specialPlaylists: MyUserSpecialPlaylist[] |
24 | 18 | ||
25 | canSeeVideosLink = true | 19 | canSeeVideosLink = true |
26 | 20 | ||
27 | static load () { | 21 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) { |
28 | const tokens = Tokens.load() | ||
29 | if (!tokens) return null | ||
30 | |||
31 | const userInfo = getUserInfoFromLocalStorage() | ||
32 | if (!userInfo) return null | ||
33 | |||
34 | return new AuthUser(userInfo, tokens) | ||
35 | } | ||
36 | |||
37 | static flush () { | ||
38 | flushUserInfoFromLocalStorage() | ||
39 | |||
40 | Tokens.flush() | ||
41 | } | ||
42 | |||
43 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { | ||
44 | super(userHash) | 22 | super(userHash) |
45 | 23 | ||
46 | this.tokens = new Tokens(hashTokens) | 24 | this.tokens = new UserTokens(hashTokens) |
47 | this.specialPlaylists = userHash.specialPlaylists | 25 | this.specialPlaylists = userHash.specialPlaylists |
48 | } | 26 | } |
49 | 27 | ||
@@ -77,20 +55,6 @@ export class AuthUser extends User implements ServerMyUserModel { | |||
77 | return user.role === UserRole.USER | 55 | return user.role === UserRole.USER |
78 | } | 56 | } |
79 | 57 | ||
80 | save () { | ||
81 | saveUserInfoIntoLocalStorage({ | ||
82 | id: this.id, | ||
83 | username: this.username, | ||
84 | email: this.email, | ||
85 | role: this.role, | ||
86 | nsfwPolicy: this.nsfwPolicy, | ||
87 | webTorrentEnabled: this.webTorrentEnabled, | ||
88 | autoPlayVideo: this.autoPlayVideo | ||
89 | }) | ||
90 | |||
91 | this.tokens.save() | ||
92 | } | ||
93 | |||
94 | computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> { | 58 | computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> { |
95 | if (!this.isUploadDisabled()) { | 59 | if (!this.isUploadDisabled()) { |
96 | this.canSeeVideosLink = true | 60 | this.canSeeVideosLink = true |
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 79239a17a..2ac88c185 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -5,7 +5,7 @@ import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' | |||
5 | import { Injectable } from '@angular/core' | 5 | import { Injectable } from '@angular/core' |
6 | import { Router } from '@angular/router' | 6 | import { Router } from '@angular/router' |
7 | import { Notifier } from '@app/core/notification/notifier.service' | 7 | import { Notifier } from '@app/core/notification/notifier.service' |
8 | import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' | 8 | import { objectToUrlEncoded, peertubeLocalStorage, UserTokens } from '@root-helpers/index' |
9 | import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models' | 9 | import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models' |
10 | import { environment } from '../../../environments/environment' | 10 | import { environment } from '../../../environments/environment' |
11 | import { RestExtractor } from '../rest/rest-extractor.service' | 11 | import { RestExtractor } from '../rest/rest-extractor.service' |
@@ -34,6 +34,7 @@ export class AuthService { | |||
34 | 34 | ||
35 | loginChangedSource: Observable<AuthStatus> | 35 | loginChangedSource: Observable<AuthStatus> |
36 | userInformationLoaded = new ReplaySubject<boolean>(1) | 36 | userInformationLoaded = new ReplaySubject<boolean>(1) |
37 | tokensRefreshed = new ReplaySubject<void>(1) | ||
37 | hotkeys: Hotkey[] | 38 | hotkeys: Hotkey[] |
38 | 39 | ||
39 | private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) | 40 | private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) |
@@ -52,9 +53,6 @@ export class AuthService { | |||
52 | this.loginChanged = new Subject<AuthStatus>() | 53 | this.loginChanged = new Subject<AuthStatus>() |
53 | this.loginChangedSource = this.loginChanged.asObservable() | 54 | this.loginChangedSource = this.loginChanged.asObservable() |
54 | 55 | ||
55 | // Return null if there is nothing to load | ||
56 | this.user = AuthUser.load() | ||
57 | |||
58 | // Set HotKeys | 56 | // Set HotKeys |
59 | this.hotkeys = [ | 57 | this.hotkeys = [ |
60 | new Hotkey('m s', (event: KeyboardEvent): boolean => { | 58 | new Hotkey('m s', (event: KeyboardEvent): boolean => { |
@@ -76,6 +74,10 @@ export class AuthService { | |||
76 | ] | 74 | ] |
77 | } | 75 | } |
78 | 76 | ||
77 | buildAuthUser (userInfo: Partial<User>, tokens: UserTokens) { | ||
78 | this.user = new AuthUser(userInfo, tokens) | ||
79 | } | ||
80 | |||
79 | loadClientCredentials () { | 81 | loadClientCredentials () { |
80 | // Fetch the client_id/client_secret | 82 | // Fetch the client_id/client_secret |
81 | this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) | 83 | this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) |
@@ -180,8 +182,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular | |||
180 | 182 | ||
181 | this.user = null | 183 | this.user = null |
182 | 184 | ||
183 | AuthUser.flush() | ||
184 | |||
185 | this.setStatus(AuthStatus.LoggedOut) | 185 | this.setStatus(AuthStatus.LoggedOut) |
186 | 186 | ||
187 | this.hotkeysService.remove(this.hotkeys) | 187 | this.hotkeysService.remove(this.hotkeys) |
@@ -239,7 +239,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular | |||
239 | .subscribe({ | 239 | .subscribe({ |
240 | next: res => { | 240 | next: res => { |
241 | this.user.patch(res) | 241 | this.user.patch(res) |
242 | this.user.save() | ||
243 | 242 | ||
244 | this.userInformationLoaded.next(true) | 243 | this.userInformationLoaded.next(true) |
245 | } | 244 | } |
@@ -262,7 +261,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular | |||
262 | } | 261 | } |
263 | 262 | ||
264 | this.user = new AuthUser(obj, hashTokens) | 263 | this.user = new AuthUser(obj, hashTokens) |
265 | this.user.save() | ||
266 | 264 | ||
267 | this.setStatus(AuthStatus.LoggedIn) | 265 | this.setStatus(AuthStatus.LoggedIn) |
268 | this.userInformationLoaded.next(true) | 266 | this.userInformationLoaded.next(true) |
@@ -272,7 +270,7 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular | |||
272 | 270 | ||
273 | private handleRefreshToken (obj: UserRefreshToken) { | 271 | private handleRefreshToken (obj: UserRefreshToken) { |
274 | this.user.refreshTokens(obj.access_token, obj.refresh_token) | 272 | this.user.refreshTokens(obj.access_token, obj.refresh_token) |
275 | this.user.save() | 273 | this.tokensRefreshed.next() |
276 | } | 274 | } |
277 | 275 | ||
278 | private setStatus (status: AuthStatus) { | 276 | private setStatus (status: AuthStatus) { |