diff options
Diffstat (limited to 'client/src/app/shared/auth')
-rw-r--r-- | client/src/app/shared/auth/auth-http.service.ts | 6 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 37 | ||||
-rw-r--r-- | client/src/app/shared/auth/user.model.ts | 23 |
3 files changed, 52 insertions, 14 deletions
diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts index 9c7ef4389..55bb501e6 100644 --- a/client/src/app/shared/auth/auth-http.service.ts +++ b/client/src/app/shared/auth/auth-http.service.ts | |||
@@ -49,16 +49,18 @@ export class AuthHttp extends Http { | |||
49 | return this.request(url, options); | 49 | return this.request(url, options); |
50 | } | 50 | } |
51 | 51 | ||
52 | post(url: string, options?: RequestOptionsArgs): Observable<Response> { | 52 | post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { |
53 | if (!options) options = {}; | 53 | if (!options) options = {}; |
54 | options.method = RequestMethod.Post; | 54 | options.method = RequestMethod.Post; |
55 | options.body = body; | ||
55 | 56 | ||
56 | return this.request(url, options); | 57 | return this.request(url, options); |
57 | } | 58 | } |
58 | 59 | ||
59 | put(url: string, options?: RequestOptionsArgs): Observable<Response> { | 60 | put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { |
60 | if (!options) options = {}; | 61 | if (!options) options = {}; |
61 | options.method = RequestMethod.Put; | 62 | options.method = RequestMethod.Put; |
63 | options.body = body; | ||
62 | 64 | ||
63 | return this.request(url, options); | 65 | return this.request(url, options); |
64 | } | 66 | } |
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 6a5b19ffe..24d1a4fa2 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -10,6 +10,7 @@ import { User } from './user.model'; | |||
10 | export class AuthService { | 10 | export class AuthService { |
11 | private static BASE_CLIENT_URL = '/api/v1/clients/local'; | 11 | private static BASE_CLIENT_URL = '/api/v1/clients/local'; |
12 | private static BASE_TOKEN_URL = '/api/v1/users/token'; | 12 | private static BASE_TOKEN_URL = '/api/v1/users/token'; |
13 | private static BASE_USER_INFORMATIONS_URL = '/api/v1/users/me'; | ||
13 | 14 | ||
14 | loginChangedSource: Observable<AuthStatus>; | 15 | loginChangedSource: Observable<AuthStatus>; |
15 | 16 | ||
@@ -99,6 +100,7 @@ export class AuthService { | |||
99 | res.username = username; | 100 | res.username = username; |
100 | return res; | 101 | return res; |
101 | }) | 102 | }) |
103 | .flatMap(res => this.fetchUserInformations(res)) | ||
102 | .map(res => this.handleLogin(res)) | 104 | .map(res => this.handleLogin(res)) |
103 | .catch(this.handleError); | 105 | .catch(this.handleError); |
104 | } | 106 | } |
@@ -136,31 +138,50 @@ export class AuthService { | |||
136 | .catch(this.handleError); | 138 | .catch(this.handleError); |
137 | } | 139 | } |
138 | 140 | ||
139 | private setStatus(status: AuthStatus) { | 141 | private fetchUserInformations (obj: any) { |
140 | this.loginChanged.next(status); | 142 | // Do not call authHttp here to avoid circular dependencies headaches |
143 | |||
144 | const headers = new Headers(); | ||
145 | headers.set('Authorization', `Bearer ${obj.access_token}`); | ||
146 | |||
147 | return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers }) | ||
148 | .map(res => res.json()) | ||
149 | .map(res => { | ||
150 | obj.id = res.id; | ||
151 | obj.role = res.role; | ||
152 | return obj; | ||
153 | } | ||
154 | ); | ||
155 | } | ||
156 | |||
157 | private handleError (error: Response) { | ||
158 | console.error(error); | ||
159 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
141 | } | 160 | } |
142 | 161 | ||
143 | private handleLogin (obj: any) { | 162 | private handleLogin (obj: any) { |
163 | const id = obj.id; | ||
144 | const username = obj.username; | 164 | const username = obj.username; |
165 | const role = obj.role; | ||
145 | const hash_tokens = { | 166 | const hash_tokens = { |
146 | access_token: obj.access_token, | 167 | access_token: obj.access_token, |
147 | token_type: obj.token_type, | 168 | token_type: obj.token_type, |
148 | refresh_token: obj.refresh_token | 169 | refresh_token: obj.refresh_token |
149 | }; | 170 | }; |
150 | 171 | ||
151 | this.user = new User(username, hash_tokens); | 172 | this.user = new User(id, username, role, hash_tokens); |
152 | this.user.save(); | 173 | this.user.save(); |
153 | 174 | ||
154 | this.setStatus(AuthStatus.LoggedIn); | 175 | this.setStatus(AuthStatus.LoggedIn); |
155 | } | 176 | } |
156 | 177 | ||
157 | private handleError (error: Response) { | ||
158 | console.error(error); | ||
159 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
160 | } | ||
161 | |||
162 | private handleRefreshToken (obj: any) { | 178 | private handleRefreshToken (obj: any) { |
163 | this.user.refreshTokens(obj.access_token, obj.refresh_token); | 179 | this.user.refreshTokens(obj.access_token, obj.refresh_token); |
164 | this.user.save(); | 180 | this.user.save(); |
165 | } | 181 | } |
182 | |||
183 | private setStatus(status: AuthStatus) { | ||
184 | this.loginChanged.next(status); | ||
185 | } | ||
186 | |||
166 | } | 187 | } |
diff --git a/client/src/app/shared/auth/user.model.ts b/client/src/app/shared/auth/user.model.ts index 98852f835..e486873ab 100644 --- a/client/src/app/shared/auth/user.model.ts +++ b/client/src/app/shared/auth/user.model.ts | |||
@@ -1,15 +1,24 @@ | |||
1 | export class User { | 1 | export class User { |
2 | private static KEYS = { | 2 | private static KEYS = { |
3 | ID: 'id', | ||
4 | ROLE: 'role', | ||
3 | USERNAME: 'username' | 5 | USERNAME: 'username' |
4 | }; | 6 | }; |
5 | 7 | ||
8 | id: string; | ||
9 | role: string; | ||
6 | username: string; | 10 | username: string; |
7 | tokens: Tokens; | 11 | tokens: Tokens; |
8 | 12 | ||
9 | static load() { | 13 | static load() { |
10 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | 14 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); |
11 | if (usernameLocalStorage) { | 15 | if (usernameLocalStorage) { |
12 | return new User(localStorage.getItem(this.KEYS.USERNAME), Tokens.load()); | 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 | ); | ||
13 | } | 22 | } |
14 | 23 | ||
15 | return null; | 24 | return null; |
@@ -17,11 +26,15 @@ export class User { | |||
17 | 26 | ||
18 | static flush() { | 27 | static flush() { |
19 | localStorage.removeItem(this.KEYS.USERNAME); | 28 | localStorage.removeItem(this.KEYS.USERNAME); |
29 | localStorage.removeItem(this.KEYS.ID); | ||
30 | localStorage.removeItem(this.KEYS.ROLE); | ||
20 | Tokens.flush(); | 31 | Tokens.flush(); |
21 | } | 32 | } |
22 | 33 | ||
23 | constructor(username: string, hash_tokens: any) { | 34 | constructor(id: string, username: string, role: string, hash_tokens: any) { |
35 | this.id = id; | ||
24 | this.username = username; | 36 | this.username = username; |
37 | this.role = role; | ||
25 | this.tokens = new Tokens(hash_tokens); | 38 | this.tokens = new Tokens(hash_tokens); |
26 | } | 39 | } |
27 | 40 | ||
@@ -43,12 +56,14 @@ export class User { | |||
43 | } | 56 | } |
44 | 57 | ||
45 | save() { | 58 | save() { |
46 | localStorage.setItem('username', this.username); | 59 | localStorage.setItem(User.KEYS.ID, this.id); |
60 | localStorage.setItem(User.KEYS.USERNAME, this.username); | ||
61 | localStorage.setItem(User.KEYS.ROLE, this.role); | ||
47 | this.tokens.save(); | 62 | this.tokens.save(); |
48 | } | 63 | } |
49 | } | 64 | } |
50 | 65 | ||
51 | // Private class used only by User | 66 | // Private class only used by User |
52 | class Tokens { | 67 | class Tokens { |
53 | private static KEYS = { | 68 | private static KEYS = { |
54 | ACCESS_TOKEN: 'access_token', | 69 | ACCESS_TOKEN: 'access_token', |