aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/auth/auth.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/core/auth/auth.service.ts')
-rw-r--r--client/src/app/core/auth/auth.service.ts111
1 files changed, 55 insertions, 56 deletions
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts
index 6223cde6d..4b388d7be 100644
--- a/client/src/app/core/auth/auth.service.ts
+++ b/client/src/app/core/auth/auth.service.ts
@@ -1,14 +1,9 @@
1import { Observable, ReplaySubject, Subject, throwError as observableThrowError } from 'rxjs'
2import { catchError, map, mergeMap, tap } from 'rxjs/operators'
1import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' 3import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
2import { Injectable } from '@angular/core' 4import { Injectable } from '@angular/core'
3import { Router } from '@angular/router' 5import { Router } from '@angular/router'
4import { NotificationsService } from 'angular2-notifications' 6import { NotificationsService } from 'angular2-notifications'
5import 'rxjs/add/observable/throw'
6import 'rxjs/add/operator/do'
7import 'rxjs/add/operator/map'
8import 'rxjs/add/operator/mergeMap'
9import { Observable } from 'rxjs/Observable'
10import { ReplaySubject } from 'rxjs/ReplaySubject'
11import { Subject } from 'rxjs/Subject'
12import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared' 7import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared'
13import { User } from '../../../../../shared/models/users' 8import { User } from '../../../../../shared/models/users'
14import { UserLogin } from '../../../../../shared/models/users/user-login.model' 9import { UserLogin } from '../../../../../shared/models/users/user-login.model'
@@ -46,7 +41,7 @@ export class AuthService {
46 private notificationsService: NotificationsService, 41 private notificationsService: NotificationsService,
47 private restExtractor: RestExtractor, 42 private restExtractor: RestExtractor,
48 private router: Router 43 private router: Router
49 ) { 44 ) {
50 this.loginChanged = new Subject<AuthStatus>() 45 this.loginChanged = new Subject<AuthStatus>()
51 this.loginChangedSource = this.loginChanged.asObservable() 46 this.loginChangedSource = this.loginChanged.asObservable()
52 47
@@ -58,28 +53,28 @@ export class AuthService {
58 // Fetch the client_id/client_secret 53 // Fetch the client_id/client_secret
59 // FIXME: save in local storage? 54 // FIXME: save in local storage?
60 this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) 55 this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL)
61 .catch(res => this.restExtractor.handleError(res)) 56 .pipe(catchError(res => this.restExtractor.handleError(res)))
62 .subscribe( 57 .subscribe(
63 res => { 58 res => {
64 this.clientId = res.client_id 59 this.clientId = res.client_id
65 this.clientSecret = res.client_secret 60 this.clientSecret = res.client_secret
66 console.log('Client credentials loaded.') 61 console.log('Client credentials loaded.')
67 }, 62 },
68 63
69 error => { 64 error => {
70 let errorMessage = error.message 65 let errorMessage = error.message
71 66
72 if (error.status === 403) { 67 if (error.status === 403) {
73 errorMessage = `Cannot retrieve OAuth Client credentials: ${error.text}. \n` 68 errorMessage = `Cannot retrieve OAuth Client credentials: ${error.text}. \n`
74 errorMessage += 'Ensure you have correctly configured PeerTube (config/ directory), ' + 69 errorMessage += 'Ensure you have correctly configured PeerTube (config/ directory), ' +
75 'in particular the "webserver" section.' 70 'in particular the "webserver" section.'
76 } 71 }
77 72
78 // We put a bigger timeout 73 // We put a bigger timeout
79 // This is an important message 74 // This is an important message
80 this.notificationsService.error('Error', errorMessage, { timeOut: 7000 }) 75 this.notificationsService.error('Error', errorMessage, { timeOut: 7000 })
81 } 76 }
82 ) 77 )
83 } 78 }
84 79
85 getRefreshToken () { 80 getRefreshToken () {
@@ -129,10 +124,12 @@ export class AuthService {
129 124
130 const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') 125 const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
131 return this.http.post<UserLogin>(AuthService.BASE_TOKEN_URL, body.toString(), { headers }) 126 return this.http.post<UserLogin>(AuthService.BASE_TOKEN_URL, body.toString(), { headers })
132 .map(res => Object.assign(res, { username })) 127 .pipe(
133 .flatMap(res => this.mergeUserInformation(res)) 128 map(res => Object.assign(res, { username })),
134 .map(res => this.handleLogin(res)) 129 mergeMap(res => this.mergeUserInformation(res)),
135 .catch(res => this.restExtractor.handleError(res)) 130 map(res => this.handleLogin(res)),
131 catchError(res => this.restExtractor.handleError(res))
132 )
136 } 133 }
137 134
138 logout () { 135 logout () {
@@ -161,20 +158,22 @@ export class AuthService {
161 const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') 158 const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
162 159
163 this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers }) 160 this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers })
164 .map(res => this.handleRefreshToken(res)) 161 .pipe(
165 .do(() => this.refreshingTokenObservable = null) 162 map(res => this.handleRefreshToken(res)),
166 .catch(err => { 163 tap(() => this.refreshingTokenObservable = null),
167 this.refreshingTokenObservable = null 164 catchError(err => {
168 165 this.refreshingTokenObservable = null
169 console.error(err) 166
170 console.log('Cannot refresh token -> logout...') 167 console.error(err)
171 this.logout() 168 console.log('Cannot refresh token -> logout...')
172 this.router.navigate([ '/login' ]) 169 this.logout()
173 170 this.router.navigate([ '/login' ])
174 return Observable.throw({ 171
175 error: 'You need to reconnect.' 172 return observableThrowError({
176 }) 173 error: 'You need to reconnect.'
177 }) 174 })
175 })
176 )
178 177
179 return this.refreshingTokenObservable 178 return this.refreshingTokenObservable
180 } 179 }
@@ -188,14 +187,14 @@ export class AuthService {
188 } 187 }
189 188
190 this.mergeUserInformation(obj) 189 this.mergeUserInformation(obj)
191 .subscribe( 190 .subscribe(
192 res => { 191 res => {
193 this.user.patch(res) 192 this.user.patch(res)
194 this.user.save() 193 this.user.save()
195 194
196 this.userInformationLoaded.next(true) 195 this.userInformationLoaded.next(true)
197 } 196 }
198 ) 197 )
199 } 198 }
200 199
201 private mergeUserInformation (obj: UserLoginWithUsername): Observable<UserLoginWithUserInformation> { 200 private mergeUserInformation (obj: UserLoginWithUsername): Observable<UserLoginWithUserInformation> {
@@ -203,7 +202,7 @@ export class AuthService {
203 const headers = new HttpHeaders().set('Authorization', `${obj.token_type} ${obj.access_token}`) 202 const headers = new HttpHeaders().set('Authorization', `${obj.token_type} ${obj.access_token}`)
204 203
205 return this.http.get<UserServerModel>(AuthService.BASE_USER_INFORMATION_URL, { headers }) 204 return this.http.get<UserServerModel>(AuthService.BASE_USER_INFORMATION_URL, { headers })
206 .map(res => Object.assign(obj, res)) 205 .pipe(map(res => Object.assign(obj, res)))
207 } 206 }
208 207
209 private handleLogin (obj: UserLoginWithUserInformation) { 208 private handleLogin (obj: UserLoginWithUserInformation) {