From b1794c53ac97d77a16c10ed915336f08cff1e5e3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 22 Mar 2016 15:51:54 +0100 Subject: Login in Angular : first draft --- .../users/components/login/login.component.html | 14 ++++++ .../users/components/login/login.component.scss | 0 .../users/components/login/login.component.ts | 32 +++++++++++++ client/angular/users/models/authStatus.ts | 4 ++ client/angular/users/models/token.ts | 17 +++++++ client/angular/users/services/auth.service.ts | 54 ++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 client/angular/users/components/login/login.component.html create mode 100644 client/angular/users/components/login/login.component.scss create mode 100644 client/angular/users/components/login/login.component.ts create mode 100644 client/angular/users/models/authStatus.ts create mode 100644 client/angular/users/models/token.ts create mode 100644 client/angular/users/services/auth.service.ts (limited to 'client/angular/users') diff --git a/client/angular/users/components/login/login.component.html b/client/angular/users/components/login/login.component.html new file mode 100644 index 000000000..940694515 --- /dev/null +++ b/client/angular/users/components/login/login.component.html @@ -0,0 +1,14 @@ +

Login

+
+
+ + +
+ +
+ + +
+ + +
diff --git a/client/angular/users/components/login/login.component.scss b/client/angular/users/components/login/login.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/client/angular/users/components/login/login.component.ts b/client/angular/users/components/login/login.component.ts new file mode 100644 index 000000000..0881a3a15 --- /dev/null +++ b/client/angular/users/components/login/login.component.ts @@ -0,0 +1,32 @@ +import { Component } from 'angular2/core'; +import { Router } from 'angular2/router'; + +import { AuthService } from '../../services/auth.service'; +import { AuthStatus } from '../../models/authStatus'; +import { Token } from '../../models/token'; + +@Component({ + selector: 'my-user-login', + styleUrls: [ 'app/angular/users/components/login/login.component.css' ], + templateUrl: 'app/angular/users/components/login/login.component.html' +}) + +export class UserLoginComponent { + constructor(private _authService: AuthService, private _router: Router) {} + + 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(); + + this._authService.setStatus(AuthStatus.LoggedIn); + + this._router.navigate(['VideosList']); + }, + error => alert(error) + ); + } +} diff --git a/client/angular/users/models/authStatus.ts b/client/angular/users/models/authStatus.ts new file mode 100644 index 000000000..f646bd4cf --- /dev/null +++ b/client/angular/users/models/authStatus.ts @@ -0,0 +1,4 @@ +export enum AuthStatus { + LoggedIn, + LoggedOut +} diff --git a/client/angular/users/models/token.ts b/client/angular/users/models/token.ts new file mode 100644 index 000000000..688dfdc80 --- /dev/null +++ b/client/angular/users/models/token.ts @@ -0,0 +1,17 @@ +export class Token { + access_token: string; + 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; + } + + save() { + 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/services/auth.service.ts b/client/angular/users/services/auth.service.ts new file mode 100644 index 000000000..a512c3d9c --- /dev/null +++ b/client/angular/users/services/auth.service.ts @@ -0,0 +1,54 @@ +import { Injectable } from 'angular2/core'; +import { Http, Response, Headers, URLSearchParams } from 'angular2/http'; +import { Observable, Subject } from 'rxjs/Rx'; + +import { Token } from '../models/token'; +import { AuthStatus } from '../models/authStatus'; + +@Injectable() +export class AuthService { + private _loginChanged = new Subject(); + + private _baseLoginUrl = '/api/v1/users/token'; + private _clientId = '56f055587305d40b21904240'; + private _clientSecret = 'megustalabanana'; + + loginChanged$ = this._loginChanged.asObservable(); + + constructor (private http: Http) {} + + login(username: string, password: string) { + let body = new URLSearchParams(); + body.set('client_id', this._clientId); + body.set('client_secret', this._clientSecret); + body.set('response_type', 'code'); + body.set('grant_type', 'password'); + body.set('scope', 'upload'); + body.set('username', username); + body.set('password', password); + + let headers = new Headers(); + headers.append('Content-Type', 'application/x-www-form-urlencoded'); + + let options = { + headers: headers + } + + return this.http.post(this._baseLoginUrl, body.toString(), options) + .map(res => res.json()) + .catch(this.handleError); + } + + logout() { + // TODO make HTTP request + } + + setStatus(status: AuthStatus) { + this._loginChanged.next(status); + } + + private handleError (error: Response) { + console.error(error); + return Observable.throw(error.json().error || 'Server error'); + } +} -- cgit v1.2.3