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