diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 14:32:15 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 14:32:15 +0200 |
commit | df98563e2104b82b119c00a3cd83cd0dc1242d25 (patch) | |
tree | a9720bf01bac9ad5646bd3d3c9bc7653617afdad /client/src/app/core/auth | |
parent | 46757b477c1adb5f98060d15998a3852e18902a6 (diff) | |
download | PeerTube-df98563e2104b82b119c00a3cd83cd0dc1242d25.tar.gz PeerTube-df98563e2104b82b119c00a3cd83cd0dc1242d25.tar.zst PeerTube-df98563e2104b82b119c00a3cd83cd0dc1242d25.zip |
Use typescript standard and lint all files
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 180 | ||||
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 236 | ||||
-rw-r--r-- | client/src/app/core/auth/index.ts | 4 |
3 files changed, 213 insertions, 207 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 1a31a7834..65c37bcfa 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,6 +1,66 @@ | |||
1 | // Do not use the barrel (dependency loop) | 1 | // Do not use the barrel (dependency loop) |
2 | import { UserRole } from '../../../../../shared/models/user.model' | 2 | import { UserRole } from '../../../../../shared/models/user.model' |
3 | import { User } from '../../shared/users/user.model'; | 3 | import { User } from '../../shared/users/user.model' |
4 | |||
5 | export type TokenOptions = { | ||
6 | accessToken: string | ||
7 | refreshToken: string | ||
8 | tokenType: string | ||
9 | } | ||
10 | |||
11 | // Private class only used by User | ||
12 | class Tokens { | ||
13 | private static KEYS = { | ||
14 | ACCESS_TOKEN: 'access_token', | ||
15 | REFRESH_TOKEN: 'refresh_token', | ||
16 | TOKEN_TYPE: 'token_type' | ||
17 | } | ||
18 | |||
19 | accessToken: string | ||
20 | refreshToken: string | ||
21 | tokenType: string | ||
22 | |||
23 | static load () { | ||
24 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN) | ||
25 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN) | ||
26 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE) | ||
27 | |||
28 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
29 | return new Tokens({ | ||
30 | accessToken: accessTokenLocalStorage, | ||
31 | refreshToken: refreshTokenLocalStorage, | ||
32 | tokenType: tokenTypeLocalStorage | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | return null | ||
37 | } | ||
38 | |||
39 | static flush () { | ||
40 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN) | ||
41 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN) | ||
42 | localStorage.removeItem(this.KEYS.TOKEN_TYPE) | ||
43 | } | ||
44 | |||
45 | constructor (hash?: TokenOptions) { | ||
46 | if (hash) { | ||
47 | this.accessToken = hash.accessToken | ||
48 | this.refreshToken = hash.refreshToken | ||
49 | |||
50 | if (hash.tokenType === 'bearer') { | ||
51 | this.tokenType = 'Bearer' | ||
52 | } else { | ||
53 | this.tokenType = hash.tokenType | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | save () { | ||
59 | localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | ||
60 | localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | ||
61 | localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | ||
62 | } | ||
63 | } | ||
4 | 64 | ||
5 | export class AuthUser extends User { | 65 | export class AuthUser extends User { |
6 | private static KEYS = { | 66 | private static KEYS = { |
@@ -9,123 +69,69 @@ export class AuthUser extends User { | |||
9 | EMAIL: 'email', | 69 | EMAIL: 'email', |
10 | USERNAME: 'username', | 70 | USERNAME: 'username', |
11 | DISPLAY_NSFW: 'display_nsfw' | 71 | DISPLAY_NSFW: 'display_nsfw' |
12 | }; | 72 | } |
13 | 73 | ||
14 | tokens: Tokens; | 74 | tokens: Tokens |
15 | 75 | ||
16 | static load() { | 76 | static load () { |
17 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | 77 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME) |
18 | if (usernameLocalStorage) { | 78 | if (usernameLocalStorage) { |
19 | return new AuthUser( | 79 | return new AuthUser( |
20 | { | 80 | { |
21 | id: parseInt(localStorage.getItem(this.KEYS.ID)), | 81 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), |
22 | username: localStorage.getItem(this.KEYS.USERNAME), | 82 | username: localStorage.getItem(this.KEYS.USERNAME), |
23 | email: localStorage.getItem(this.KEYS.EMAIL), | 83 | email: localStorage.getItem(this.KEYS.EMAIL), |
24 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, | 84 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, |
25 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' | 85 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' |
26 | }, | 86 | }, |
27 | Tokens.load() | 87 | Tokens.load() |
28 | ); | 88 | ) |
29 | } | 89 | } |
30 | 90 | ||
31 | return null; | 91 | return null |
32 | } | 92 | } |
33 | 93 | ||
34 | static flush() { | 94 | static flush () { |
35 | localStorage.removeItem(this.KEYS.USERNAME); | 95 | localStorage.removeItem(this.KEYS.USERNAME) |
36 | localStorage.removeItem(this.KEYS.ID); | 96 | localStorage.removeItem(this.KEYS.ID) |
37 | localStorage.removeItem(this.KEYS.ROLE); | 97 | localStorage.removeItem(this.KEYS.ROLE) |
38 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW); | 98 | localStorage.removeItem(this.KEYS.DISPLAY_NSFW) |
39 | Tokens.flush(); | 99 | Tokens.flush() |
40 | } | 100 | } |
41 | 101 | ||
42 | constructor(userHash: { | 102 | constructor (userHash: { |
43 | id: number, | 103 | id: number, |
44 | username: string, | 104 | username: string, |
45 | role: UserRole, | 105 | role: UserRole, |
46 | email: string, | 106 | email: string, |
47 | displayNSFW: boolean | 107 | displayNSFW: boolean |
48 | }, hashTokens: any) { | 108 | }, hashTokens: TokenOptions) { |
49 | super(userHash); | 109 | super(userHash) |
50 | this.tokens = new Tokens(hashTokens); | 110 | this.tokens = new Tokens(hashTokens) |
51 | } | ||
52 | |||
53 | getAccessToken() { | ||
54 | return this.tokens.access_token; | ||
55 | } | ||
56 | |||
57 | getRefreshToken() { | ||
58 | return this.tokens.refresh_token; | ||
59 | } | ||
60 | |||
61 | getTokenType() { | ||
62 | return this.tokens.token_type; | ||
63 | } | ||
64 | |||
65 | refreshTokens(access_token: string, refresh_token: string) { | ||
66 | this.tokens.access_token = access_token; | ||
67 | this.tokens.refresh_token = refresh_token; | ||
68 | } | 111 | } |
69 | 112 | ||
70 | save() { | 113 | getAccessToken () { |
71 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); | 114 | return this.tokens.accessToken |
72 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); | ||
73 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role); | ||
74 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)); | ||
75 | this.tokens.save(); | ||
76 | } | 115 | } |
77 | } | ||
78 | |||
79 | // Private class only used by User | ||
80 | class Tokens { | ||
81 | private static KEYS = { | ||
82 | ACCESS_TOKEN: 'access_token', | ||
83 | REFRESH_TOKEN: 'refresh_token', | ||
84 | TOKEN_TYPE: 'token_type', | ||
85 | }; | ||
86 | |||
87 | access_token: string; | ||
88 | refresh_token: string; | ||
89 | token_type: string; | ||
90 | |||
91 | static load() { | ||
92 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); | ||
93 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); | ||
94 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); | ||
95 | |||
96 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
97 | return new Tokens({ | ||
98 | access_token: accessTokenLocalStorage, | ||
99 | refresh_token: refreshTokenLocalStorage, | ||
100 | token_type: tokenTypeLocalStorage | ||
101 | }); | ||
102 | } | ||
103 | 116 | ||
104 | return null; | 117 | getRefreshToken () { |
118 | return this.tokens.refreshToken | ||
105 | } | 119 | } |
106 | 120 | ||
107 | static flush() { | 121 | getTokenType () { |
108 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN); | 122 | return this.tokens.tokenType |
109 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN); | ||
110 | localStorage.removeItem(this.KEYS.TOKEN_TYPE); | ||
111 | } | 123 | } |
112 | 124 | ||
113 | constructor(hash?: any) { | 125 | refreshTokens (accessToken: string, refreshToken: string) { |
114 | if (hash) { | 126 | this.tokens.accessToken = accessToken |
115 | this.access_token = hash.access_token; | 127 | this.tokens.refreshToken = refreshToken |
116 | this.refresh_token = hash.refresh_token; | ||
117 | |||
118 | if (hash.token_type === 'bearer') { | ||
119 | this.token_type = 'Bearer'; | ||
120 | } else { | ||
121 | this.token_type = hash.token_type; | ||
122 | } | ||
123 | } | ||
124 | } | 128 | } |
125 | 129 | ||
126 | save() { | 130 | save () { |
127 | localStorage.setItem('access_token', this.access_token); | 131 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
128 | localStorage.setItem('refresh_token', this.refresh_token); | 132 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) |
129 | localStorage.setItem('token_type', this.token_type); | 133 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role) |
134 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) | ||
135 | this.tokens.save() | ||
130 | } | 136 | } |
131 | } | 137 | } |
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 4c75df1ca..32f7a5503 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -1,40 +1,40 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core' |
2 | import { Headers, Http, Response, URLSearchParams } from '@angular/http'; | 2 | import { Headers, Http, Response, URLSearchParams } from '@angular/http' |
3 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router' |
4 | import { Observable } from 'rxjs/Observable'; | 4 | import { Observable } from 'rxjs/Observable' |
5 | import { Subject } from 'rxjs/Subject'; | 5 | import { Subject } from 'rxjs/Subject' |
6 | import 'rxjs/add/operator/map'; | 6 | import 'rxjs/add/operator/map' |
7 | import 'rxjs/add/operator/mergeMap'; | 7 | import 'rxjs/add/operator/mergeMap' |
8 | import 'rxjs/add/observable/throw'; | 8 | 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'; | 12 | import { AuthStatus } from './auth-status.model' |
13 | import { AuthUser } from './auth-user.model'; | 13 | import { AuthUser } from './auth-user.model' |
14 | // Do not use the barrel (dependency loop) | 14 | // Do not use the barrel (dependency loop) |
15 | import { RestExtractor } from '../../shared/rest'; | 15 | import { RestExtractor } from '../../shared/rest' |
16 | 16 | ||
17 | @Injectable() | 17 | @Injectable() |
18 | export class AuthService { | 18 | export class AuthService { |
19 | private static BASE_CLIENT_URL = API_URL + '/api/v1/clients/local'; | 19 | private static BASE_CLIENT_URL = API_URL + '/api/v1/clients/local' |
20 | private static BASE_TOKEN_URL = API_URL + '/api/v1/users/token'; | 20 | private static BASE_TOKEN_URL = API_URL + '/api/v1/users/token' |
21 | private static BASE_USER_INFORMATIONS_URL = API_URL + '/api/v1/users/me'; | 21 | private static BASE_USER_INFORMATIONS_URL = API_URL + '/api/v1/users/me' |
22 | 22 | ||
23 | loginChangedSource: Observable<AuthStatus>; | 23 | loginChangedSource: Observable<AuthStatus> |
24 | 24 | ||
25 | private clientId: string; | 25 | private clientId: string |
26 | private clientSecret: string; | 26 | private clientSecret: string |
27 | private loginChanged: Subject<AuthStatus>; | 27 | private loginChanged: Subject<AuthStatus> |
28 | private user: AuthUser = null; | 28 | private user: AuthUser = null |
29 | 29 | ||
30 | constructor( | 30 | constructor ( |
31 | private http: Http, | 31 | private http: Http, |
32 | private notificationsService: NotificationsService, | 32 | private notificationsService: NotificationsService, |
33 | private restExtractor: RestExtractor, | 33 | private restExtractor: RestExtractor, |
34 | private router: Router | 34 | private router: Router |
35 | ) { | 35 | ) { |
36 | this.loginChanged = new Subject<AuthStatus>(); | 36 | this.loginChanged = new Subject<AuthStatus>() |
37 | this.loginChangedSource = this.loginChanged.asObservable(); | 37 | this.loginChangedSource = this.loginChanged.asObservable() |
38 | 38 | ||
39 | // Fetch the client_id/client_secret | 39 | // Fetch the client_id/client_secret |
40 | // FIXME: save in local storage? | 40 | // FIXME: save in local storage? |
@@ -43,120 +43,120 @@ export class AuthService { | |||
43 | .catch((res) => this.restExtractor.handleError(res)) | 43 | .catch((res) => this.restExtractor.handleError(res)) |
44 | .subscribe( | 44 | .subscribe( |
45 | result => { | 45 | result => { |
46 | this.clientId = result.client_id; | 46 | this.clientId = result.client_id |
47 | this.clientSecret = result.client_secret; | 47 | this.clientSecret = result.client_secret |
48 | console.log('Client credentials loaded.'); | 48 | console.log('Client credentials loaded.') |
49 | }, | 49 | }, |
50 | 50 | ||
51 | error => { | 51 | error => { |
52 | let errorMessage = `Cannot retrieve OAuth Client credentials: ${error.text}. \n`; | 52 | let errorMessage = `Cannot retrieve OAuth Client credentials: ${error.text}. \n` |
53 | errorMessage += 'Ensure you have correctly configured PeerTube (config/ directory), in particular the "webserver" section.'; | 53 | errorMessage += 'Ensure you have correctly configured PeerTube (config/ directory), in particular the "webserver" section.' |
54 | 54 | ||
55 | // We put a bigger timeout | 55 | // We put a bigger timeout |
56 | // This is an important message | 56 | // This is an important message |
57 | this.notificationsService.error('Error', errorMessage, { timeOut: 7000 }); | 57 | this.notificationsService.error('Error', errorMessage, { timeOut: 7000 }) |
58 | } | 58 | } |
59 | ); | 59 | ) |
60 | 60 | ||
61 | // Return null if there is nothing to load | 61 | // Return null if there is nothing to load |
62 | this.user = AuthUser.load(); | 62 | this.user = AuthUser.load() |
63 | } | 63 | } |
64 | 64 | ||
65 | getRefreshToken() { | 65 | getRefreshToken () { |
66 | if (this.user === null) return null; | 66 | if (this.user === null) return null |
67 | 67 | ||
68 | return this.user.getRefreshToken(); | 68 | return this.user.getRefreshToken() |
69 | } | 69 | } |
70 | 70 | ||
71 | getRequestHeaderValue() { | 71 | getRequestHeaderValue () { |
72 | return `${this.getTokenType()} ${this.getAccessToken()}`; | 72 | return `${this.getTokenType()} ${this.getAccessToken()}` |
73 | } | 73 | } |
74 | 74 | ||
75 | getAccessToken() { | 75 | getAccessToken () { |
76 | if (this.user === null) return null; | 76 | if (this.user === null) return null |
77 | 77 | ||
78 | return this.user.getAccessToken(); | 78 | return this.user.getAccessToken() |
79 | } | 79 | } |
80 | 80 | ||
81 | getTokenType() { | 81 | getTokenType () { |
82 | if (this.user === null) return null; | 82 | if (this.user === null) return null |
83 | 83 | ||
84 | return this.user.getTokenType(); | 84 | return this.user.getTokenType() |
85 | } | 85 | } |
86 | 86 | ||
87 | getUser(): AuthUser { | 87 | getUser () { |
88 | return this.user; | 88 | return this.user |
89 | } | 89 | } |
90 | 90 | ||
91 | isAdmin() { | 91 | isAdmin () { |
92 | if (this.user === null) return false; | 92 | if (this.user === null) return false |
93 | 93 | ||
94 | return this.user.isAdmin(); | 94 | return this.user.isAdmin() |
95 | } | 95 | } |
96 | 96 | ||
97 | isLoggedIn() { | 97 | isLoggedIn () { |
98 | if (this.getAccessToken()) { | 98 | if (this.getAccessToken()) { |
99 | return true; | 99 | return true |
100 | } else { | 100 | } else { |
101 | return false; | 101 | return false |
102 | } | 102 | } |
103 | } | 103 | } |
104 | 104 | ||
105 | login(username: string, password: string) { | 105 | login (username: string, password: string) { |
106 | let body = new URLSearchParams(); | 106 | let body = new URLSearchParams() |
107 | body.set('client_id', this.clientId); | 107 | body.set('client_id', this.clientId) |
108 | body.set('client_secret', this.clientSecret); | 108 | body.set('client_secret', this.clientSecret) |
109 | body.set('response_type', 'code'); | 109 | body.set('response_type', 'code') |
110 | body.set('grant_type', 'password'); | 110 | body.set('grant_type', 'password') |
111 | body.set('scope', 'upload'); | 111 | body.set('scope', 'upload') |
112 | body.set('username', username); | 112 | body.set('username', username) |
113 | body.set('password', password); | 113 | body.set('password', password) |
114 | 114 | ||
115 | let headers = new Headers(); | 115 | let headers = new Headers() |
116 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | 116 | headers.append('Content-Type', 'application/x-www-form-urlencoded') |
117 | 117 | ||
118 | let options = { | 118 | let options = { |
119 | headers: headers | 119 | headers: headers |
120 | }; | 120 | } |
121 | 121 | ||
122 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 122 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
123 | .map(this.restExtractor.extractDataGet) | 123 | .map(this.restExtractor.extractDataGet) |
124 | .map(res => { | 124 | .map(res => { |
125 | res.username = username; | 125 | res.username = username |
126 | return res; | 126 | return res |
127 | }) | 127 | }) |
128 | .flatMap(res => this.mergeUserInformations(res)) | 128 | .flatMap(res => this.mergeUserInformations(res)) |
129 | .map(res => this.handleLogin(res)) | 129 | .map(res => this.handleLogin(res)) |
130 | .catch((res) => this.restExtractor.handleError(res)); | 130 | .catch((res) => this.restExtractor.handleError(res)) |
131 | } | 131 | } |
132 | 132 | ||
133 | logout() { | 133 | logout () { |
134 | // TODO: make an HTTP request to revoke the tokens | 134 | // TODO: make an HTTP request to revoke the tokens |
135 | this.user = null; | 135 | this.user = null |
136 | 136 | ||
137 | AuthUser.flush(); | 137 | AuthUser.flush() |
138 | 138 | ||
139 | this.setStatus(AuthStatus.LoggedOut); | 139 | this.setStatus(AuthStatus.LoggedOut) |
140 | } | 140 | } |
141 | 141 | ||
142 | refreshAccessToken() { | 142 | refreshAccessToken () { |
143 | console.log('Refreshing token...'); | 143 | console.log('Refreshing token...') |
144 | 144 | ||
145 | const refreshToken = this.getRefreshToken(); | 145 | const refreshToken = this.getRefreshToken() |
146 | 146 | ||
147 | let body = new URLSearchParams(); | 147 | let body = new URLSearchParams() |
148 | body.set('refresh_token', refreshToken); | 148 | body.set('refresh_token', refreshToken) |
149 | body.set('client_id', this.clientId); | 149 | body.set('client_id', this.clientId) |
150 | body.set('client_secret', this.clientSecret); | 150 | body.set('client_secret', this.clientSecret) |
151 | body.set('response_type', 'code'); | 151 | body.set('response_type', 'code') |
152 | body.set('grant_type', 'refresh_token'); | 152 | body.set('grant_type', 'refresh_token') |
153 | 153 | ||
154 | let headers = new Headers(); | 154 | let headers = new Headers() |
155 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | 155 | headers.append('Content-Type', 'application/x-www-form-urlencoded') |
156 | 156 | ||
157 | let options = { | 157 | let options = { |
158 | headers: headers | 158 | headers: headers |
159 | }; | 159 | } |
160 | 160 | ||
161 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 161 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
162 | .map(this.restExtractor.extractDataGet) | 162 | .map(this.restExtractor.extractDataGet) |
@@ -164,41 +164,41 @@ export class AuthService { | |||
164 | .catch((res: Response) => { | 164 | .catch((res: Response) => { |
165 | // The refresh token is invalid? | 165 | // The refresh token is invalid? |
166 | if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { | 166 | if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { |
167 | console.error('Cannot refresh token -> logout...'); | 167 | console.error('Cannot refresh token -> logout...') |
168 | this.logout(); | 168 | this.logout() |
169 | this.router.navigate(['/login']); | 169 | this.router.navigate(['/login']) |
170 | 170 | ||
171 | return Observable.throw({ | 171 | return Observable.throw({ |
172 | json: () => '', | 172 | json: () => '', |
173 | text: () => 'You need to reconnect.' | 173 | text: () => 'You need to reconnect.' |
174 | }); | 174 | }) |
175 | } | 175 | } |
176 | 176 | ||
177 | return this.restExtractor.handleError(res); | 177 | return this.restExtractor.handleError(res) |
178 | }); | 178 | }) |
179 | } | 179 | } |
180 | 180 | ||
181 | refreshUserInformations() { | 181 | refreshUserInformations () { |
182 | const obj = { | 182 | const obj = { |
183 | access_token: this.user.getAccessToken() | 183 | access_token: this.user.getAccessToken() |
184 | }; | 184 | } |
185 | 185 | ||
186 | this.mergeUserInformations(obj) | 186 | this.mergeUserInformations (obj) |
187 | .subscribe( | 187 | .subscribe( |
188 | res => { | 188 | res => { |
189 | this.user.displayNSFW = res.displayNSFW; | 189 | this.user.displayNSFW = res.displayNSFW |
190 | this.user.role = res.role; | 190 | this.user.role = res.role |
191 | 191 | ||
192 | this.user.save(); | 192 | this.user.save() |
193 | } | 193 | } |
194 | ); | 194 | ) |
195 | } | 195 | } |
196 | 196 | ||
197 | private mergeUserInformations(obj: { access_token: string }) { | 197 | private mergeUserInformations (obj: { access_token: string }) { |
198 | // Do not call authHttp here to avoid circular dependencies headaches | 198 | // Do not call authHttp here to avoid circular dependencies headaches |
199 | 199 | ||
200 | const headers = new Headers(); | 200 | const headers = new Headers() |
201 | headers.set('Authorization', `Bearer ${obj.access_token}`); | 201 | headers.set('Authorization', `Bearer ${obj.access_token}`) |
202 | 202 | ||
203 | return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers }) | 203 | return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers }) |
204 | .map(res => res.json()) | 204 | .map(res => res.json()) |
@@ -207,38 +207,38 @@ export class AuthService { | |||
207 | id: res.id, | 207 | id: res.id, |
208 | role: res.role, | 208 | role: res.role, |
209 | displayNSFW: res.displayNSFW | 209 | displayNSFW: res.displayNSFW |
210 | }; | 210 | } |
211 | 211 | ||
212 | return Object.assign(obj, newProperties); | 212 | return Object.assign(obj, newProperties) |
213 | } | 213 | } |
214 | ); | 214 | ) |
215 | } | 215 | } |
216 | 216 | ||
217 | private handleLogin (obj: any) { | 217 | private handleLogin (obj: any) { |
218 | const id = obj.id; | 218 | const id = obj.id |
219 | const username = obj.username; | 219 | const username = obj.username |
220 | const role = obj.role; | 220 | const role = obj.role |
221 | const email = obj.email; | 221 | const email = obj.email |
222 | const displayNSFW = obj.displayNSFW; | 222 | const displayNSFW = obj.displayNSFW |
223 | const hashTokens = { | 223 | const hashTokens = { |
224 | access_token: obj.access_token, | 224 | accessToken: obj.access_token, |
225 | token_type: obj.token_type, | 225 | tokenType: obj.token_type, |
226 | refresh_token: obj.refresh_token | 226 | refreshToken: obj.refresh_token |
227 | }; | 227 | } |
228 | 228 | ||
229 | this.user = new AuthUser({ id, username, role, displayNSFW, email }, hashTokens); | 229 | this.user = new AuthUser({ id, username, role, displayNSFW, email }, hashTokens) |
230 | this.user.save(); | 230 | this.user.save() |
231 | 231 | ||
232 | this.setStatus(AuthStatus.LoggedIn); | 232 | this.setStatus(AuthStatus.LoggedIn) |
233 | } | 233 | } |
234 | 234 | ||
235 | private handleRefreshToken (obj: any) { | 235 | private handleRefreshToken (obj: any) { |
236 | this.user.refreshTokens(obj.access_token, obj.refresh_token); | 236 | this.user.refreshTokens(obj.access_token, obj.refresh_token) |
237 | this.user.save(); | 237 | this.user.save() |
238 | } | 238 | } |
239 | 239 | ||
240 | private setStatus(status: AuthStatus) { | 240 | private setStatus (status: AuthStatus) { |
241 | this.loginChanged.next(status); | 241 | this.loginChanged.next(status) |
242 | } | 242 | } |
243 | 243 | ||
244 | } | 244 | } |
diff --git a/client/src/app/core/auth/index.ts b/client/src/app/core/auth/index.ts index 67a18cfbb..8e5caa7ed 100644 --- a/client/src/app/core/auth/index.ts +++ b/client/src/app/core/auth/index.ts | |||
@@ -1,3 +1,3 @@ | |||
1 | export * from './auth-status.model'; | 1 | export * from './auth-status.model' |
2 | export * from './auth-user.model'; | 2 | export * from './auth-user.model' |
3 | export * from './auth.service' | 3 | export * from './auth.service' |