]>
Commit | Line | Data |
---|---|---|
1 | // Do not use the barrel (dependency loop) | |
2 | import { UserRole } from '../../../../../shared/models/users/user-role.type' | |
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 | } | |
64 | ||
65 | export class AuthUser extends User { | |
66 | private static KEYS = { | |
67 | ID: 'id', | |
68 | ROLE: 'role', | |
69 | EMAIL: 'email', | |
70 | USERNAME: 'username', | |
71 | DISPLAY_NSFW: 'display_nsfw' | |
72 | } | |
73 | ||
74 | tokens: Tokens | |
75 | ||
76 | static load () { | |
77 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) | |
78 | if (usernameLocalStorage) { | |
79 | return new AuthUser( | |
80 | { | |
81 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), | |
82 | username: localStorage.getItem(this.KEYS.USERNAME), | |
83 | email: localStorage.getItem(this.KEYS.EMAIL), | |
84 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, | |
85 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' | |
86 | }, | |
87 | Tokens.load() | |
88 | ) | |
89 | } | |
90 | ||
91 | return null | |
92 | } | |
93 | ||
94 | static flush () { | |
95 | localStorage.removeItem(this.KEYS.USERNAME) | |
96 | localStorage.removeItem(this.KEYS.ID) | |
97 | localStorage.removeItem(this.KEYS.ROLE) | |
98 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW) | |
99 | localStorage.removeItem(this.KEYS.EMAIL) | |
100 | Tokens.flush() | |
101 | } | |
102 | ||
103 | constructor (userHash: { | |
104 | id: number, | |
105 | username: string, | |
106 | role: UserRole, | |
107 | email: string, | |
108 | displayNSFW: boolean | |
109 | }, hashTokens: TokenOptions) { | |
110 | super(userHash) | |
111 | this.tokens = new Tokens(hashTokens) | |
112 | } | |
113 | ||
114 | getAccessToken () { | |
115 | return this.tokens.accessToken | |
116 | } | |
117 | ||
118 | getRefreshToken () { | |
119 | return this.tokens.refreshToken | |
120 | } | |
121 | ||
122 | getTokenType () { | |
123 | return this.tokens.tokenType | |
124 | } | |
125 | ||
126 | refreshTokens (accessToken: string, refreshToken: string) { | |
127 | this.tokens.accessToken = accessToken | |
128 | this.tokens.refreshToken = refreshToken | |
129 | } | |
130 | ||
131 | save () { | |
132 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | |
133 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
134 | localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
135 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role) | |
136 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) | |
137 | this.tokens.save() | |
138 | } | |
139 | } |