diff options
Diffstat (limited to 'client/angular/users')
-rw-r--r-- | client/angular/users/components/login/login.component.html | 14 | ||||
-rw-r--r-- | client/angular/users/components/login/login.component.scss | 0 | ||||
-rw-r--r-- | client/angular/users/components/login/login.component.ts | 36 | ||||
-rw-r--r-- | client/angular/users/models/authStatus.ts | 4 | ||||
-rw-r--r-- | client/angular/users/models/token.ts | 31 | ||||
-rw-r--r-- | client/angular/users/models/user.ts | 20 | ||||
-rw-r--r-- | client/angular/users/services/auth.service.ts | 107 |
7 files changed, 0 insertions, 212 deletions
diff --git a/client/angular/users/components/login/login.component.html b/client/angular/users/components/login/login.component.html deleted file mode 100644 index 940694515..000000000 --- a/client/angular/users/components/login/login.component.html +++ /dev/null | |||
@@ -1,14 +0,0 @@ | |||
1 | <h3>Login</h3> | ||
2 | <form role="form" (submit)="login(username.value, password.value)"> | ||
3 | <div class="form-group"> | ||
4 | <label for="username">Username</label> | ||
5 | <input type="text" #username class="form-control" id="username" placeholder="Username"> | ||
6 | </div> | ||
7 | |||
8 | <div class="form-group"> | ||
9 | <label for="password">Password</label> | ||
10 | <input type="password" #password class="form-control" id="password" placeholder="Password"> | ||
11 | </div> | ||
12 | |||
13 | <input type="submit" value="Login" class="btn btn-default"> | ||
14 | </form> | ||
diff --git a/client/angular/users/components/login/login.component.scss b/client/angular/users/components/login/login.component.scss deleted file mode 100644 index e69de29bb..000000000 --- a/client/angular/users/components/login/login.component.scss +++ /dev/null | |||
diff --git a/client/angular/users/components/login/login.component.ts b/client/angular/users/components/login/login.component.ts deleted file mode 100644 index d339353ef..000000000 --- a/client/angular/users/components/login/login.component.ts +++ /dev/null | |||
@@ -1,36 +0,0 @@ | |||
1 | import { Component } from '@angular/core'; | ||
2 | import { Router } from '@angular/router-deprecated'; | ||
3 | |||
4 | import { AuthService } from '../../services/auth.service'; | ||
5 | import { AuthStatus } from '../../models/authStatus'; | ||
6 | import { User } from '../../models/user'; | ||
7 | |||
8 | @Component({ | ||
9 | selector: 'my-user-login', | ||
10 | styleUrls: [ 'app/angular/users/components/login/login.component.css' ], | ||
11 | templateUrl: 'app/angular/users/components/login/login.component.html' | ||
12 | }) | ||
13 | |||
14 | export class UserLoginComponent { | ||
15 | constructor(private _authService: AuthService, private _router: Router) {} | ||
16 | |||
17 | login(username: string, password: string) { | ||
18 | this._authService.login(username, password).subscribe( | ||
19 | result => { | ||
20 | const user = new User(username, result); | ||
21 | user.save(); | ||
22 | |||
23 | this._authService.setStatus(AuthStatus.LoggedIn); | ||
24 | |||
25 | this._router.navigate(['VideosList']); | ||
26 | }, | ||
27 | error => { | ||
28 | if (error.error === 'invalid_grant') { | ||
29 | alert('Credentials are invalid.'); | ||
30 | } else { | ||
31 | alert(`${error.error}: ${error.error_description}`); | ||
32 | } | ||
33 | } | ||
34 | ); | ||
35 | } | ||
36 | } | ||
diff --git a/client/angular/users/models/authStatus.ts b/client/angular/users/models/authStatus.ts deleted file mode 100644 index f646bd4cf..000000000 --- a/client/angular/users/models/authStatus.ts +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | export enum AuthStatus { | ||
2 | LoggedIn, | ||
3 | LoggedOut | ||
4 | } | ||
diff --git a/client/angular/users/models/token.ts b/client/angular/users/models/token.ts deleted file mode 100644 index b7872e74a..000000000 --- a/client/angular/users/models/token.ts +++ /dev/null | |||
@@ -1,31 +0,0 @@ | |||
1 | export class Token { | ||
2 | access_token: string; | ||
3 | refresh_token: string; | ||
4 | token_type: string; | ||
5 | |||
6 | static load(): Token { | ||
7 | return new Token({ | ||
8 | access_token: localStorage.getItem('access_token'), | ||
9 | refresh_token: localStorage.getItem('refresh_token'), | ||
10 | token_type: localStorage.getItem('token_type') | ||
11 | }); | ||
12 | } | ||
13 | |||
14 | constructor (hash?: any) { | ||
15 | if (hash) { | ||
16 | this.access_token = hash.access_token; | ||
17 | this.refresh_token = hash.refresh_token; | ||
18 | if (hash.token_type === 'bearer') { | ||
19 | this.token_type = 'Bearer'; | ||
20 | } else { | ||
21 | this.token_type = hash.token_type; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | save():void { | ||
27 | localStorage.setItem('access_token', this.access_token); | ||
28 | localStorage.setItem('refresh_token', this.refresh_token); | ||
29 | localStorage.setItem('token_type', this.token_type); | ||
30 | } | ||
31 | } | ||
diff --git a/client/angular/users/models/user.ts b/client/angular/users/models/user.ts deleted file mode 100644 index 3367e3bb5..000000000 --- a/client/angular/users/models/user.ts +++ /dev/null | |||
@@ -1,20 +0,0 @@ | |||
1 | import { Token } from './token'; | ||
2 | |||
3 | export class User { | ||
4 | username: string; | ||
5 | token: Token; | ||
6 | |||
7 | static load(): User { | ||
8 | return new User(localStorage.getItem('username'), Token.load()); | ||
9 | } | ||
10 | |||
11 | constructor (username: string, hash_token: any) { | ||
12 | this.username = username; | ||
13 | this.token = new Token(hash_token); | ||
14 | } | ||
15 | |||
16 | save(): void { | ||
17 | localStorage.setItem('username', this.username); | ||
18 | this.token.save(); | ||
19 | } | ||
20 | } | ||
diff --git a/client/angular/users/services/auth.service.ts b/client/angular/users/services/auth.service.ts deleted file mode 100644 index 099563d43..000000000 --- a/client/angular/users/services/auth.service.ts +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Http, Response, Headers, URLSearchParams, RequestOptions } from '@angular/http'; | ||
3 | import { Observable, Subject } from 'rxjs/Rx'; | ||
4 | |||
5 | import { AuthStatus } from '../models/authStatus'; | ||
6 | import { User } from '../models/user'; | ||
7 | |||
8 | @Injectable() | ||
9 | export class AuthService { | ||
10 | loginChanged$; | ||
11 | |||
12 | private _loginChanged; | ||
13 | private _baseLoginUrl = '/api/v1/users/token'; | ||
14 | private _baseClientUrl = '/api/v1/users/client'; | ||
15 | private _clientId = ''; | ||
16 | private _clientSecret = ''; | ||
17 | |||
18 | constructor (private http: Http) { | ||
19 | this._loginChanged = new Subject<AuthStatus>(); | ||
20 | this.loginChanged$ = this._loginChanged.asObservable(); | ||
21 | |||
22 | // Fetch the client_id/client_secret | ||
23 | // FIXME: save in local storage? | ||
24 | this.http.get(this._baseClientUrl) | ||
25 | .map(res => res.json()) | ||
26 | .catch(this.handleError) | ||
27 | .subscribe( | ||
28 | result => { | ||
29 | this._clientId = result.client_id; | ||
30 | this._clientSecret = result.client_secret; | ||
31 | console.log('Client credentials loaded.'); | ||
32 | }, | ||
33 | error => { | ||
34 | alert(error); | ||
35 | } | ||
36 | ); | ||
37 | } | ||
38 | |||
39 | login(username: string, password: string) { | ||
40 | let body = new URLSearchParams(); | ||
41 | body.set('client_id', this._clientId); | ||
42 | body.set('client_secret', this._clientSecret); | ||
43 | body.set('response_type', 'code'); | ||
44 | body.set('grant_type', 'password'); | ||
45 | body.set('scope', 'upload'); | ||
46 | body.set('username', username); | ||
47 | body.set('password', password); | ||
48 | |||
49 | let headers = new Headers(); | ||
50 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
51 | |||
52 | let options = { | ||
53 | headers: headers | ||
54 | }; | ||
55 | |||
56 | return this.http.post(this._baseLoginUrl, body.toString(), options) | ||
57 | .map(res => res.json()) | ||
58 | .catch(this.handleError); | ||
59 | } | ||
60 | |||
61 | logout() { | ||
62 | // TODO make HTTP request | ||
63 | } | ||
64 | |||
65 | getRequestHeader(): Headers { | ||
66 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | ||
67 | } | ||
68 | |||
69 | getAuthRequestOptions(): RequestOptions { | ||
70 | return new RequestOptions({ headers: this.getRequestHeader() }); | ||
71 | } | ||
72 | |||
73 | getToken(): string { | ||
74 | return localStorage.getItem('access_token'); | ||
75 | } | ||
76 | |||
77 | getTokenType(): string { | ||
78 | return localStorage.getItem('token_type'); | ||
79 | } | ||
80 | |||
81 | getUser(): User { | ||
82 | if (this.isLoggedIn() === false) { | ||
83 | return null; | ||
84 | } | ||
85 | |||
86 | const user = User.load(); | ||
87 | |||
88 | return user; | ||
89 | } | ||
90 | |||
91 | isLoggedIn(): boolean { | ||
92 | if (this.getToken()) { | ||
93 | return true; | ||
94 | } else { | ||
95 | return false; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | setStatus(status: AuthStatus) { | ||
100 | this._loginChanged.next(status); | ||
101 | } | ||
102 | |||
103 | private handleError (error: Response) { | ||
104 | console.error(error); | ||
105 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
106 | } | ||
107 | } | ||