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/index.ts | 2 + client/src/app/shared/search/index.ts | 3 + client/src/app/shared/search/search-field.type.ts | 1 + client/src/app/shared/search/search.component.html | 17 ++++ client/src/app/shared/search/search.component.ts | 46 +++++++++ client/src/app/shared/search/search.model.ts | 6 ++ 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 ++++ 11 files changed, 243 insertions(+) create mode 100644 client/src/app/shared/index.ts create mode 100644 client/src/app/shared/search/index.ts create mode 100644 client/src/app/shared/search/search-field.type.ts create mode 100644 client/src/app/shared/search/search.component.html create mode 100644 client/src/app/shared/search/search.component.ts create mode 100644 client/src/app/shared/search/search.model.ts 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') diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts new file mode 100644 index 000000000..0cab7dad0 --- /dev/null +++ b/client/src/app/shared/index.ts @@ -0,0 +1,2 @@ +export * from './search'; +export * from './users' diff --git a/client/src/app/shared/search/index.ts b/client/src/app/shared/search/index.ts new file mode 100644 index 000000000..a49a4f1a9 --- /dev/null +++ b/client/src/app/shared/search/index.ts @@ -0,0 +1,3 @@ +export * from './search-field.type'; +export * from './search.component'; +export * from './search.model'; diff --git a/client/src/app/shared/search/search-field.type.ts b/client/src/app/shared/search/search-field.type.ts new file mode 100644 index 000000000..846236290 --- /dev/null +++ b/client/src/app/shared/search/search-field.type.ts @@ -0,0 +1 @@ +export type SearchField = "name" | "author" | "podUrl" | "magnetUri"; diff --git a/client/src/app/shared/search/search.component.html b/client/src/app/shared/search/search.component.html new file mode 100644 index 000000000..fb13ac72e --- /dev/null +++ b/client/src/app/shared/search/search.component.html @@ -0,0 +1,17 @@ +
+
+ + +
+ + +
diff --git a/client/src/app/shared/search/search.component.ts b/client/src/app/shared/search/search.component.ts new file mode 100644 index 000000000..31f8b1535 --- /dev/null +++ b/client/src/app/shared/search/search.component.ts @@ -0,0 +1,46 @@ +import { Component, EventEmitter, Output } from '@angular/core'; + +import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown'; + +import { Search } from './search.model'; +import { SearchField } from './search-field.type'; + +@Component({ + selector: 'my-search', + template: require('./search.component.html'), + directives: [ DROPDOWN_DIRECTIVES ] +}) + +export class SearchComponent { + @Output() search = new EventEmitter(); + + fieldChoices = { + name: 'Name', + author: 'Author', + podUrl: 'Pod Url', + magnetUri: 'Magnet Uri' + }; + searchCriterias: Search = { + field: 'name', + value: '' + }; + + get choiceKeys() { + return Object.keys(this.fieldChoices); + } + + choose($event: MouseEvent, choice: SearchField) { + $event.preventDefault(); + $event.stopPropagation(); + + this.searchCriterias.field = choice; + } + + doSearch() { + this.search.emit(this.searchCriterias); + } + + getStringChoice(choiceKey: SearchField) { + return this.fieldChoices[choiceKey]; + } +} diff --git a/client/src/app/shared/search/search.model.ts b/client/src/app/shared/search/search.model.ts new file mode 100644 index 000000000..932a6566c --- /dev/null +++ b/client/src/app/shared/search/search.model.ts @@ -0,0 +1,6 @@ +import { SearchField } from './search-field.type'; + +export interface Search { + field: SearchField; + value: string; +} 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