From 4a6995be18b15de1834a39c8921a0e4109671bb6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 3 Jun 2016 22:08:03 +0200 Subject: First draft to use webpack instead of systemjs --- client/src/app/shared/users/auth-status.model.ts | 4 + client/src/app/shared/users/auth.service.ts | 108 +++++++++++++++++++++++ client/src/app/shared/users/index.ts | 4 + client/src/app/shared/users/token.model.ts | 32 +++++++ client/src/app/shared/users/user.model.ts | 20 +++++ 5 files changed, 168 insertions(+) create mode 100644 client/src/app/shared/users/auth-status.model.ts create mode 100644 client/src/app/shared/users/auth.service.ts create mode 100644 client/src/app/shared/users/index.ts create mode 100644 client/src/app/shared/users/token.model.ts create mode 100644 client/src/app/shared/users/user.model.ts (limited to 'client/src/app/shared/users') diff --git a/client/src/app/shared/users/auth-status.model.ts b/client/src/app/shared/users/auth-status.model.ts new file mode 100644 index 000000000..f646bd4cf --- /dev/null +++ b/client/src/app/shared/users/auth-status.model.ts @@ -0,0 +1,4 @@ +export enum AuthStatus { + LoggedIn, + LoggedOut +} diff --git a/client/src/app/shared/users/auth.service.ts b/client/src/app/shared/users/auth.service.ts new file mode 100644 index 000000000..d63fe38f3 --- /dev/null +++ b/client/src/app/shared/users/auth.service.ts @@ -0,0 +1,108 @@ +import { Injectable } from '@angular/core'; +import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http'; +import { Observable, Subject } from 'rxjs/Rx'; + +import { AuthStatus } from './auth-status.model'; +import { User } from './user.model'; + +@Injectable() +export class AuthService { + private static BASE_CLIENT_URL = '/api/v1/users/client'; + private static BASE_LOGIN_URL = '/api/v1/users/token'; + + loginChangedSource: Observable; + + private clientId: string; + private clientSecret: string; + private loginChanged: Subject; + + constructor(private http: Http) { + 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) + .subscribe( + result => { + this.clientId = result.client_id; + this.clientSecret = result.client_secret; + console.log('Client credentials loaded.'); + }, + error => { + alert(error); + } + ); + } + + getAuthRequestOptions(): RequestOptions { + return new RequestOptions({ headers: this.getRequestHeader() }); + } + + getRequestHeader() { + return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); + } + + getToken() { + return localStorage.getItem('access_token'); + } + + getTokenType() { + return localStorage.getItem('token_type'); + } + + getUser(): User { + if (this.isLoggedIn() === false) { + return null; + } + + const user = User.load(); + + return user; + } + + isLoggedIn() { + if (this.getToken()) { + return true; + } else { + return false; + } + } + + 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(AuthService.BASE_LOGIN_URL, 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' }); + } +} diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts new file mode 100644 index 000000000..c6816b3c6 --- /dev/null +++ b/client/src/app/shared/users/index.ts @@ -0,0 +1,4 @@ +export * from './auth-status.model'; +export * from './auth.service'; +export * from './token.model'; +export * from './user.model'; diff --git a/client/src/app/shared/users/token.model.ts b/client/src/app/shared/users/token.model.ts new file mode 100644 index 000000000..021c83fad --- /dev/null +++ b/client/src/app/shared/users/token.model.ts @@ -0,0 +1,32 @@ +export class Token { + access_token: string; + refresh_token: string; + token_type: string; + + static load() { + return new Token({ + access_token: localStorage.getItem('access_token'), + refresh_token: localStorage.getItem('refresh_token'), + token_type: localStorage.getItem('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() { + 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/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts new file mode 100644 index 000000000..ca0a5f26c --- /dev/null +++ b/client/src/app/shared/users/user.model.ts @@ -0,0 +1,20 @@ +import { Token } from './token.model'; + +export class User { + username: string; + token: Token; + + static load() { + return new User(localStorage.getItem('username'), Token.load()); + } + + constructor(username: string, hash_token: any) { + this.username = username; + this.token = new Token(hash_token); + } + + save() { + localStorage.setItem('username', this.username); + this.token.save(); + } +} -- cgit v1.2.3