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