aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users/auth.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/users/auth.service.ts')
-rw-r--r--client/src/app/shared/users/auth.service.ts113
1 files changed, 0 insertions, 113 deletions
diff --git a/client/src/app/shared/users/auth.service.ts b/client/src/app/shared/users/auth.service.ts
deleted file mode 100644
index 1c822c1e1..000000000
--- a/client/src/app/shared/users/auth.service.ts
+++ /dev/null
@@ -1,113 +0,0 @@
1import { Injectable } from '@angular/core';
2import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4import { Subject } from 'rxjs/Subject';
5
6import { AuthStatus } from './auth-status.model';
7import { User } from './user.model';
8
9@Injectable()
10export class AuthService {
11 private static BASE_CLIENT_URL = '/api/v1/users/client';
12 private static BASE_LOGIN_URL = '/api/v1/users/token';
13
14 loginChangedSource: Observable<AuthStatus>;
15
16 private clientId: string;
17 private clientSecret: string;
18 private loginChanged: Subject<AuthStatus>;
19
20 constructor(private http: Http) {
21 this.loginChanged = new Subject<AuthStatus>();
22 this.loginChangedSource = this.loginChanged.asObservable();
23
24 // Fetch the client_id/client_secret
25 // FIXME: save in local storage?
26 this.http.get(AuthService.BASE_CLIENT_URL)
27 .map(res => res.json())
28 .catch(this.handleError)
29 .subscribe(
30 result => {
31 this.clientId = result.client_id;
32 this.clientSecret = result.client_secret;
33 console.log('Client credentials loaded.');
34 },
35 error => {
36 alert(error);
37 }
38 );
39 }
40
41 getAuthRequestOptions(): RequestOptions {
42 return new RequestOptions({ headers: this.getRequestHeader() });
43 }
44
45 getRequestHeader() {
46 return new Headers({ 'Authorization': this.getRequestHeaderValue() });
47 }
48
49 getRequestHeaderValue() {
50 return `${this.getTokenType()} ${this.getToken()}`;
51 }
52
53 getToken() {
54 return localStorage.getItem('access_token');
55 }
56
57 getTokenType() {
58 return localStorage.getItem('token_type');
59 }
60
61 getUser(): User {
62 if (this.isLoggedIn() === false) {
63 return null;
64 }
65
66 const user = User.load();
67
68 return user;
69 }
70
71 isLoggedIn() {
72 if (this.getToken()) {
73 return true;
74 } else {
75 return false;
76 }
77 }
78
79 login(username: string, password: string) {
80 let body = new URLSearchParams();
81 body.set('client_id', this.clientId);
82 body.set('client_secret', this.clientSecret);
83 body.set('response_type', 'code');
84 body.set('grant_type', 'password');
85 body.set('scope', 'upload');
86 body.set('username', username);
87 body.set('password', password);
88
89 let headers = new Headers();
90 headers.append('Content-Type', 'application/x-www-form-urlencoded');
91
92 let options = {
93 headers: headers
94 };
95
96 return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options)
97 .map(res => res.json())
98 .catch(this.handleError);
99 }
100
101 logout() {
102 // TODO make HTTP request
103 }
104
105 setStatus(status: AuthStatus) {
106 this.loginChanged.next(status);
107 }
108
109 private handleError (error: Response) {
110 console.error(error);
111 return Observable.throw(error.json() || { error: 'Server error' });
112 }
113}