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 | 180 |
1 files changed, 93 insertions, 87 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 1a31a7834..65c37bcfa 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,6 +1,66 @@ | |||
1 | // Do not use the barrel (dependency loop) | 1 | // Do not use the barrel (dependency loop) |
2 | import { UserRole } from '../../../../../shared/models/user.model' | 2 | import { UserRole } from '../../../../../shared/models/user.model' |
3 | import { User } from '../../shared/users/user.model'; | 3 | import { User } from '../../shared/users/user.model' |
4 | |||
5 | export type TokenOptions = { | ||
6 | accessToken: string | ||
7 | refreshToken: string | ||
8 | tokenType: string | ||
9 | } | ||
10 | |||
11 | // Private class only used by User | ||
12 | class Tokens { | ||
13 | private static KEYS = { | ||
14 | ACCESS_TOKEN: 'access_token', | ||
15 | REFRESH_TOKEN: 'refresh_token', | ||
16 | TOKEN_TYPE: 'token_type' | ||
17 | } | ||
18 | |||
19 | accessToken: string | ||
20 | refreshToken: string | ||
21 | tokenType: string | ||
22 | |||
23 | static load () { | ||
24 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN) | ||
25 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN) | ||
26 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE) | ||
27 | |||
28 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
29 | return new Tokens({ | ||
30 | accessToken: accessTokenLocalStorage, | ||
31 | refreshToken: refreshTokenLocalStorage, | ||
32 | tokenType: tokenTypeLocalStorage | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | return null | ||
37 | } | ||
38 | |||
39 | static flush () { | ||
40 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN) | ||
41 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN) | ||
42 | localStorage.removeItem(this.KEYS.TOKEN_TYPE) | ||
43 | } | ||
44 | |||
45 | constructor (hash?: TokenOptions) { | ||
46 | if (hash) { | ||
47 | this.accessToken = hash.accessToken | ||
48 | this.refreshToken = hash.refreshToken | ||
49 | |||
50 | if (hash.tokenType === 'bearer') { | ||
51 | this.tokenType = 'Bearer' | ||
52 | } else { | ||
53 | this.tokenType = hash.tokenType | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | save () { | ||
59 | localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | ||
60 | localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | ||
61 | localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | ||
62 | } | ||
63 | } | ||
4 | 64 | ||
5 | export class AuthUser extends User { | 65 | export class AuthUser extends User { |
6 | private static KEYS = { | 66 | private static KEYS = { |
@@ -9,123 +69,69 @@ export class AuthUser extends User { | |||
9 | EMAIL: 'email', | 69 | EMAIL: 'email', |
10 | USERNAME: 'username', | 70 | USERNAME: 'username', |
11 | DISPLAY_NSFW: 'display_nsfw' | 71 | DISPLAY_NSFW: 'display_nsfw' |
12 | }; | 72 | } |
13 | 73 | ||
14 | tokens: Tokens; | 74 | tokens: Tokens |
15 | 75 | ||
16 | static load() { | 76 | static load () { |
17 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | 77 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) |
18 | if (usernameLocalStorage) { | 78 | if (usernameLocalStorage) { |
19 | return new AuthUser( | 79 | return new AuthUser( |
20 | { | 80 | { |
21 | id: parseInt(localStorage.getItem(this.KEYS.ID)), | 81 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), |
22 | username: localStorage.getItem(this.KEYS.USERNAME), | 82 | username: localStorage.getItem(this.KEYS.USERNAME), |
23 | email: localStorage.getItem(this.KEYS.EMAIL), | 83 | email: localStorage.getItem(this.KEYS.EMAIL), |
24 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, | 84 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, |
25 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' | 85 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' |
26 | }, | 86 | }, |
27 | Tokens.load() | 87 | Tokens.load() |
28 | ); | 88 | ) |
29 | } | 89 | } |
30 | 90 | ||
31 | return null; | 91 | return null |
32 | } | 92 | } |
33 | 93 | ||
34 | static flush() { | 94 | static flush () { |
35 | localStorage.removeItem(this.KEYS.USERNAME); | 95 | localStorage.removeItem(this.KEYS.USERNAME) |
36 | localStorage.removeItem(this.KEYS.ID); | 96 | localStorage.removeItem(this.KEYS.ID) |
37 | localStorage.removeItem(this.KEYS.ROLE); | 97 | localStorage.removeItem(this.KEYS.ROLE) |
38 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW); | 98 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW) |
39 | Tokens.flush(); | 99 | Tokens.flush() |
40 | } | 100 | } |
41 | 101 | ||
42 | constructor(userHash: { | 102 | constructor (userHash: { |
43 | id: number, | 103 | id: number, |
44 | username: string, | 104 | username: string, |
45 | role: UserRole, | 105 | role: UserRole, |
46 | email: string, | 106 | email: string, |
47 | displayNSFW: boolean | 107 | displayNSFW: boolean |
48 | }, hashTokens: any) { | 108 | }, hashTokens: TokenOptions) { |
49 | super(userHash); | 109 | super(userHash) |
50 | this.tokens = new Tokens(hashTokens); | 110 | this.tokens = new Tokens(hashTokens) |
51 | } | ||
52 | |||
53 | getAccessToken() { | ||
54 | return this.tokens.access_token; | ||
55 | } | ||
56 | |||
57 | getRefreshToken() { | ||
58 | return this.tokens.refresh_token; | ||
59 | } | ||
60 | |||
61 | getTokenType() { | ||
62 | return this.tokens.token_type; | ||
63 | } | ||
64 | |||
65 | refreshTokens(access_token: string, refresh_token: string) { | ||
66 | this.tokens.access_token = access_token; | ||
67 | this.tokens.refresh_token = refresh_token; | ||
68 | } | 111 | } |
69 | 112 | ||
70 | save() { | 113 | getAccessToken () { |
71 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); | 114 | return this.tokens.accessToken |
72 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); | ||
73 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role); | ||
74 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)); | ||
75 | this.tokens.save(); | ||
76 | } | 115 | } |
77 | } | ||
78 | |||
79 | // Private class only used by User | ||
80 | class Tokens { | ||
81 | private static KEYS = { | ||
82 | ACCESS_TOKEN: 'access_token', | ||
83 | REFRESH_TOKEN: 'refresh_token', | ||
84 | TOKEN_TYPE: 'token_type', | ||
85 | }; | ||
86 | |||
87 | access_token: string; | ||
88 | refresh_token: string; | ||
89 | token_type: string; | ||
90 | |||
91 | static load() { | ||
92 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); | ||
93 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); | ||
94 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); | ||
95 | |||
96 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
97 | return new Tokens({ | ||
98 | access_token: accessTokenLocalStorage, | ||
99 | refresh_token: refreshTokenLocalStorage, | ||
100 | token_type: tokenTypeLocalStorage | ||
101 | }); | ||
102 | } | ||
103 | 116 | ||
104 | return null; | 117 | getRefreshToken () { |
118 | return this.tokens.refreshToken | ||
105 | } | 119 | } |
106 | 120 | ||
107 | static flush() { | 121 | getTokenType () { |
108 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN); | 122 | return this.tokens.tokenType |
109 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN); | ||
110 | localStorage.removeItem(this.KEYS.TOKEN_TYPE); | ||
111 | } | 123 | } |
112 | 124 | ||
113 | constructor(hash?: any) { | 125 | refreshTokens (accessToken: string, refreshToken: string) { |
114 | if (hash) { | 126 | this.tokens.accessToken = accessToken |
115 | this.access_token = hash.access_token; | 127 | this.tokens.refreshToken = refreshToken |
116 | this.refresh_token = hash.refresh_token; | ||
117 | |||
118 | if (hash.token_type === 'bearer') { | ||
119 | this.token_type = 'Bearer'; | ||
120 | } else { | ||
121 | this.token_type = hash.token_type; | ||
122 | } | ||
123 | } | ||
124 | } | 128 | } |
125 | 129 | ||
126 | save() { | 130 | save () { |
127 | localStorage.setItem('access_token', this.access_token); | 131 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
128 | localStorage.setItem('refresh_token', this.refresh_token); | 132 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) |
129 | localStorage.setItem('token_type', this.token_type); | 133 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role) |
134 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) | ||
135 | this.tokens.save() | ||
130 | } | 136 | } |
131 | } | 137 | } |