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