diff options
Diffstat (limited to 'client/src/app/core/auth/auth-user.model.ts')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 63 |
1 files changed, 2 insertions, 61 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 88b730938..ee61ff881 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,7 +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 { peertubeLocalStorage } from '@app/helpers/peertube-web-storage' | 4 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
5 | import { | 5 | import { |
6 | hasUserRight, | 6 | hasUserRight, |
7 | MyUser as ServerMyUserModel, | 7 | MyUser as ServerMyUserModel, |
@@ -12,66 +12,7 @@ import { | |||
12 | UserRole, | 12 | UserRole, |
13 | UserVideoQuota | 13 | UserVideoQuota |
14 | } from '@shared/models' | 14 | } from '@shared/models' |
15 | 15 | import { TokenOptions, Tokens } from '../../../root-helpers/pure-auth-user.model' | |
16 | export type TokenOptions = { | ||
17 | accessToken: string | ||
18 | refreshToken: string | ||
19 | tokenType: string | ||
20 | } | ||
21 | |||
22 | // Private class only used by User | ||
23 | class Tokens { | ||
24 | private static KEYS = { | ||
25 | ACCESS_TOKEN: 'access_token', | ||
26 | REFRESH_TOKEN: 'refresh_token', | ||
27 | TOKEN_TYPE: 'token_type' | ||
28 | } | ||
29 | |||
30 | accessToken: string | ||
31 | refreshToken: string | ||
32 | tokenType: string | ||
33 | |||
34 | static load () { | ||
35 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) | ||
36 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) | ||
37 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) | ||
38 | |||
39 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
40 | return new Tokens({ | ||
41 | accessToken: accessTokenLocalStorage, | ||
42 | refreshToken: refreshTokenLocalStorage, | ||
43 | tokenType: tokenTypeLocalStorage | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | return null | ||
48 | } | ||
49 | |||
50 | static flush () { | ||
51 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) | ||
52 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | ||
53 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | ||
54 | } | ||
55 | |||
56 | constructor (hash?: TokenOptions) { | ||
57 | if (hash) { | ||
58 | this.accessToken = hash.accessToken | ||
59 | this.refreshToken = hash.refreshToken | ||
60 | |||
61 | if (hash.tokenType === 'bearer') { | ||
62 | this.tokenType = 'Bearer' | ||
63 | } else { | ||
64 | this.tokenType = hash.tokenType | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | save () { | ||
70 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | ||
71 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | ||
72 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | ||
73 | } | ||
74 | } | ||
75 | 16 | ||
76 | export class AuthUser extends User implements ServerMyUserModel { | 17 | export class AuthUser extends User implements ServerMyUserModel { |
77 | tokens: Tokens | 18 | tokens: Tokens |