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