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