From 1553e15d82b8a1ec4967a90d43b33274f8215c44 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Apr 2016 22:07:46 +0200 Subject: Implement user requests autorizations in the client side --- .../users/components/login/login.component.ts | 17 +++++--- client/angular/users/models/token.ts | 24 ++++++++--- client/angular/users/models/user.ts | 20 +++++++++ client/angular/users/services/auth.service.ts | 49 +++++++++++++++++++--- 4 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 client/angular/users/models/user.ts (limited to 'client/angular/users') diff --git a/client/angular/users/components/login/login.component.ts b/client/angular/users/components/login/login.component.ts index 0881a3a15..35dea4f9b 100644 --- a/client/angular/users/components/login/login.component.ts +++ b/client/angular/users/components/login/login.component.ts @@ -3,7 +3,7 @@ import { Router } from 'angular2/router'; import { AuthService } from '../../services/auth.service'; import { AuthStatus } from '../../models/authStatus'; -import { Token } from '../../models/token'; +import { User } from '../../models/user'; @Component({ selector: 'my-user-login', @@ -17,16 +17,21 @@ export class UserLoginComponent { login(username: string, password: string) { this._authService.login(username, password).subscribe( result => { - if (result.error) return alert(result.error_description); - - let token = new Token(result); - token.save(); + const user = new User(username, result); + user.save(); this._authService.setStatus(AuthStatus.LoggedIn); this._router.navigate(['VideosList']); }, - error => alert(error) + error => { + if (error.error === 'invalid_grant') { + alert('Credentials are invalid.'); + } + else { + alert(`${error.error}: ${error.error_description}`) + } + } ); } } diff --git a/client/angular/users/models/token.ts b/client/angular/users/models/token.ts index 688dfdc80..906bf501b 100644 --- a/client/angular/users/models/token.ts +++ b/client/angular/users/models/token.ts @@ -3,13 +3,27 @@ export class Token { refresh_token: string; token_type: string; - constructor (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; - this.token_type = hash.token_type; + constructor (hash?: any) { + if (hash) { + this.access_token = hash.access_token; + this.refresh_token = hash.refresh_token; + if (hash.token_type === 'bearer') { + this.token_type = 'Bearer'; + } else { + this.token_type = hash.token_type; + } + } } - save() { + static load(): Token { + return new Token({ + access_token: localStorage.getItem('access_token'), + refresh_token: localStorage.getItem('refresh_token'), + token_type: localStorage.getItem('token_type') + }); + } + + save():void { localStorage.setItem('access_token', this.access_token); localStorage.setItem('refresh_token', this.refresh_token); localStorage.setItem('token_type', this.token_type); diff --git a/client/angular/users/models/user.ts b/client/angular/users/models/user.ts new file mode 100644 index 000000000..2c56a6132 --- /dev/null +++ b/client/angular/users/models/user.ts @@ -0,0 +1,20 @@ +import { Token } from './token'; + +export class User { + username: string; + token: Token; + + constructor (username: string, hash_token: any) { + this.username = username; + this.token = new Token(hash_token); + } + + static load(): User { + return new User(localStorage.getItem('username'), Token.load()); + } + + save(): void { + localStorage.setItem('username', this.username); + this.token.save(); + } +} diff --git a/client/angular/users/services/auth.service.ts b/client/angular/users/services/auth.service.ts index 80886346c..89412c3df 100644 --- a/client/angular/users/services/auth.service.ts +++ b/client/angular/users/services/auth.service.ts @@ -1,20 +1,23 @@ import { Injectable } from 'angular2/core'; -import { Http, Response, Headers, URLSearchParams } from 'angular2/http'; +import { Http, Response, Headers, URLSearchParams, RequestOptions } from 'angular2/http'; import { Observable, Subject } from 'rxjs/Rx'; import { AuthStatus } from '../models/authStatus'; +import { User } from '../models/user'; @Injectable() export class AuthService { - loginChanged$ = this._loginChanged.asObservable(); - - private _loginChanged = new Subject(); + loginChanged$; + private _loginChanged; private _baseLoginUrl = '/api/v1/users/token'; private _clientId = '56f055587305d40b21904240'; private _clientSecret = 'megustalabanana'; - constructor (private http: Http) {} + constructor (private http: Http) { + this._loginChanged = new Subject(); + this.loginChanged$ = this._loginChanged.asObservable(); + } login(username: string, password: string) { let body = new URLSearchParams(); @@ -42,12 +45,46 @@ export class AuthService { // TODO make HTTP request } + getRequestHeader(): Headers { + return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); + } + + getAuthRequestOptions(): RequestOptions { + return new RequestOptions({ headers: this.getRequestHeader() }); + } + + getToken(): string { + return localStorage.getItem('access_token'); + } + + getTokenType(): string { + return localStorage.getItem('token_type'); + } + + getUser(): User { + if (this.isLoggedIn() === false) { + return null; + } + + const user = User.load(); + + return user; + } + + isLoggedIn(): boolean { + if (this.getToken()) { + return true; + } else { + return false; + } + } + setStatus(status: AuthStatus) { this._loginChanged.next(status); } private handleError (error: Response) { console.error(error); - return Observable.throw(error.json().error || 'Server error'); + return Observable.throw(error.json() || { error: 'Server error' }); } } -- cgit v1.2.3