diff options
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth-status.model.ts | 4 | ||||
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 118 | ||||
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 4 | ||||
-rw-r--r-- | client/src/app/core/auth/index.ts | 2 |
4 files changed, 126 insertions, 2 deletions
diff --git a/client/src/app/core/auth/auth-status.model.ts b/client/src/app/core/auth/auth-status.model.ts new file mode 100644 index 000000000..f646bd4cf --- /dev/null +++ b/client/src/app/core/auth/auth-status.model.ts | |||
@@ -0,0 +1,4 @@ | |||
1 | export enum AuthStatus { | ||
2 | LoggedIn, | ||
3 | LoggedOut | ||
4 | } | ||
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts new file mode 100644 index 000000000..5d61954d6 --- /dev/null +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -0,0 +1,118 @@ | |||
1 | // Do not use the barrel (dependency loop) | ||
2 | import { User } from '../../shared/users/user.model'; | ||
3 | |||
4 | export class AuthUser extends User { | ||
5 | private static KEYS = { | ||
6 | ID: 'id', | ||
7 | ROLE: 'role', | ||
8 | USERNAME: 'username' | ||
9 | }; | ||
10 | |||
11 | tokens: Tokens; | ||
12 | |||
13 | static load() { | ||
14 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | ||
15 | if (usernameLocalStorage) { | ||
16 | return new AuthUser( | ||
17 | { | ||
18 | id: parseInt(localStorage.getItem(this.KEYS.ID)), | ||
19 | username: localStorage.getItem(this.KEYS.USERNAME), | ||
20 | role: localStorage.getItem(this.KEYS.ROLE) | ||
21 | }, | ||
22 | Tokens.load() | ||
23 | ); | ||
24 | } | ||
25 | |||
26 | return null; | ||
27 | } | ||
28 | |||
29 | static flush() { | ||
30 | localStorage.removeItem(this.KEYS.USERNAME); | ||
31 | localStorage.removeItem(this.KEYS.ID); | ||
32 | localStorage.removeItem(this.KEYS.ROLE); | ||
33 | Tokens.flush(); | ||
34 | } | ||
35 | |||
36 | constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) { | ||
37 | super(userHash); | ||
38 | this.tokens = new Tokens(hashTokens); | ||
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() { | ||
59 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); | ||
60 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); | ||
61 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role); | ||
62 | this.tokens.save(); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | // Private class only used by User | ||
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 | } | ||
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index a56adbbad..2e7328197 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -9,9 +9,9 @@ import 'rxjs/add/observable/throw'; | |||
9 | 9 | ||
10 | import { NotificationsService } from 'angular2-notifications'; | 10 | import { NotificationsService } from 'angular2-notifications'; |
11 | 11 | ||
12 | import { AuthStatus } from './auth-status.model'; | ||
13 | import { AuthUser } from './auth-user.model'; | ||
12 | // Do not use the barrel (dependency loop) | 14 | // Do not use the barrel (dependency loop) |
13 | import { AuthStatus } from '../../shared/auth/auth-status.model'; | ||
14 | import { AuthUser } from '../../shared/auth/auth-user.model'; | ||
15 | import { RestExtractor } from '../../shared/rest'; | 15 | import { RestExtractor } from '../../shared/rest'; |
16 | 16 | ||
17 | @Injectable() | 17 | @Injectable() |
diff --git a/client/src/app/core/auth/index.ts b/client/src/app/core/auth/index.ts index cf52c9c7c..67a18cfbb 100644 --- a/client/src/app/core/auth/index.ts +++ b/client/src/app/core/auth/index.ts | |||
@@ -1 +1,3 @@ | |||
1 | export * from './auth-status.model'; | ||
2 | export * from './auth-user.model'; | ||
1 | export * from './auth.service' | 3 | export * from './auth.service' |