X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fauth%2Fauth.service.ts;h=a30c79c8685edafb59aa2bbdc6ba2d64afd17789;hb=a6375e69668ea42e19531c6bc68dcd37f3f7cbd7;hp=8eea0c4bfed4f812c9cc97e85618243c0e06d610;hpb=7da18e4420c4b71a8ecfda07f39324fbfec081c3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 8eea0c4bf..a30c79c86 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts @@ -1,10 +1,12 @@ import { Injectable } from '@angular/core'; import { Headers, Http, Response, URLSearchParams } from '@angular/http'; +import { Router } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject'; import { AuthStatus } from './auth-status.model'; import { AuthUser } from './auth-user.model'; +import { RestExtractor } from '../rest'; @Injectable() export class AuthService { @@ -19,15 +21,19 @@ export class AuthService { private loginChanged: Subject; private user: AuthUser = null; - constructor(private http: Http) { + constructor( + private http: Http, + private restExtractor: RestExtractor, + private router: Router + ) { this.loginChanged = new Subject(); this.loginChangedSource = this.loginChanged.asObservable(); // Fetch the client_id/client_secret // FIXME: save in local storage? this.http.get(AuthService.BASE_CLIENT_URL) - .map(res => res.json()) - .catch(this.handleError) + .map(this.restExtractor.extractDataGet) + .catch((res) => this.restExtractor.handleError(res)) .subscribe( result => { this.clientId = result.client_id; @@ -35,7 +41,10 @@ export class AuthService { console.log('Client credentials loaded.'); }, error => { - alert(error); + alert( + `Cannot retrieve OAuth Client credentials: ${error.text}. \n` + + 'Ensure you have correctly configured PeerTube (config/ directory), in particular the "webserver" section.' + ); } ); @@ -101,19 +110,20 @@ export class AuthService { }; return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) - .map(res => res.json()) + .map(this.restExtractor.extractDataGet) .map(res => { res.username = username; return res; }) .flatMap(res => this.fetchUserInformations(res)) .map(res => this.handleLogin(res)) - .catch(this.handleError); + .catch((res) => this.restExtractor.handleError(res)); } logout() { // TODO: make an HTTP request to revoke the tokens this.user = null; + AuthUser.flush(); this.setStatus(AuthStatus.LoggedOut); @@ -139,9 +149,23 @@ export class AuthService { }; return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) - .map(res => res.json()) + .map(this.restExtractor.extractDataGet) .map(res => this.handleRefreshToken(res)) - .catch(this.handleError); + .catch((res: Response) => { + // The refresh token is invalid? + if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { + console.error('Cannot refresh token -> logout...'); + this.logout(); + this.router.navigate(['/login']); + + return Observable.throw({ + json: '', + text: 'You need to reconnect.' + }); + } + + return this.restExtractor.handleError(res); + }); } private fetchUserInformations (obj: any) { @@ -160,11 +184,6 @@ export class AuthService { ); } - private handleError (error: Response) { - console.error(error); - return Observable.throw(error.json() || { error: 'Server error' }); - } - private handleLogin (obj: any) { const id = obj.id; const username = obj.username;