]>
Commit | Line | Data |
---|---|---|
bd5c83a8 C |
1 | export class User { |
2 | private static KEYS = { | |
629d8d6f C |
3 | ID: 'id', |
4 | ROLE: 'role', | |
bd5c83a8 C |
5 | USERNAME: 'username' |
6 | }; | |
7 | ||
629d8d6f C |
8 | id: string; |
9 | role: string; | |
bd5c83a8 C |
10 | username: string; |
11 | tokens: Tokens; | |
12 | ||
13 | static load() { | |
14 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | |
15 | if (usernameLocalStorage) { | |
629d8d6f C |
16 | return new User( |
17 | localStorage.getItem(this.KEYS.ID), | |
18 | localStorage.getItem(this.KEYS.USERNAME), | |
19 | localStorage.getItem(this.KEYS.ROLE), | |
20 | Tokens.load() | |
21 | ); | |
bd5c83a8 C |
22 | } |
23 | ||
24 | return null; | |
25 | } | |
26 | ||
27 | static flush() { | |
28 | localStorage.removeItem(this.KEYS.USERNAME); | |
629d8d6f C |
29 | localStorage.removeItem(this.KEYS.ID); |
30 | localStorage.removeItem(this.KEYS.ROLE); | |
bd5c83a8 C |
31 | Tokens.flush(); |
32 | } | |
33 | ||
629d8d6f C |
34 | constructor(id: string, username: string, role: string, hash_tokens: any) { |
35 | this.id = id; | |
bd5c83a8 | 36 | this.username = username; |
629d8d6f | 37 | this.role = role; |
bd5c83a8 C |
38 | this.tokens = new Tokens(hash_tokens); |
39 | } | |
40 | ||
41 | getAccessToken() { | |
42 | return this.tokens.access_token; | |
43 | } | |
44 | ||
45 | getRefreshToken() { | |
46 | return this.tokens.refresh_token; | |
47 | } | |
48 | ||
49 | getTokenType() { | |
50 | return this.tokens.token_type; | |
51 | } | |
52 | ||
53 | refreshTokens(access_token: string, refresh_token: string) { | |
54 | this.tokens.access_token = access_token; | |
55 | this.tokens.refresh_token = refresh_token; | |
56 | } | |
57 | ||
58 | save() { | |
629d8d6f C |
59 | localStorage.setItem(User.KEYS.ID, this.id); |
60 | localStorage.setItem(User.KEYS.USERNAME, this.username); | |
61 | localStorage.setItem(User.KEYS.ROLE, this.role); | |
bd5c83a8 C |
62 | this.tokens.save(); |
63 | } | |
64 | } | |
65 | ||
629d8d6f | 66 | // Private class only used by User |
bd5c83a8 C |
67 | class Tokens { |
68 | private static KEYS = { | |
69 | ACCESS_TOKEN: 'access_token', | |
70 | REFRESH_TOKEN: 'refresh_token', | |
71 | TOKEN_TYPE: 'token_type', | |
72 | }; | |
73 | ||
74 | access_token: string; | |
75 | refresh_token: string; | |
76 | token_type: string; | |
77 | ||
78 | static load() { | |
79 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); | |
80 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); | |
81 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); | |
82 | ||
83 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | |
84 | return new Tokens({ | |
85 | access_token: accessTokenLocalStorage, | |
86 | refresh_token: refreshTokenLocalStorage, | |
87 | token_type: tokenTypeLocalStorage | |
88 | }); | |
89 | } | |
90 | ||
91 | return null; | |
92 | } | |
93 | ||
94 | static flush() { | |
95 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN); | |
96 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN); | |
97 | localStorage.removeItem(this.KEYS.TOKEN_TYPE); | |
98 | } | |
99 | ||
100 | constructor(hash?: any) { | |
101 | if (hash) { | |
102 | this.access_token = hash.access_token; | |
103 | this.refresh_token = hash.refresh_token; | |
104 | ||
105 | if (hash.token_type === 'bearer') { | |
106 | this.token_type = 'Bearer'; | |
107 | } else { | |
108 | this.token_type = hash.token_type; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | save() { | |
114 | localStorage.setItem('access_token', this.access_token); | |
115 | localStorage.setItem('refresh_token', this.refresh_token); | |
116 | localStorage.setItem('token_type', this.token_type); | |
117 | } | |
118 | } |