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