diff options
Diffstat (limited to 'client/app/users')
-rw-r--r-- | client/app/users/login/login.component.ts | 5 | ||||
-rw-r--r-- | client/app/users/shared/auth.service.ts | 60 |
2 files changed, 34 insertions, 31 deletions
diff --git a/client/app/users/login/login.component.ts b/client/app/users/login/login.component.ts index 8e369541d..09c5f1af7 100644 --- a/client/app/users/login/login.component.ts +++ b/client/app/users/login/login.component.ts | |||
@@ -10,7 +10,10 @@ import { AuthService, AuthStatus, User } from '../shared/index'; | |||
10 | }) | 10 | }) |
11 | 11 | ||
12 | export class UserLoginComponent { | 12 | export class UserLoginComponent { |
13 | constructor(private authService: AuthService, private router: Router) {} | 13 | constructor( |
14 | private authService: AuthService, | ||
15 | private router: Router | ||
16 | ) {} | ||
14 | 17 | ||
15 | login(username: string, password: string) { | 18 | login(username: string, password: string) { |
16 | this.authService.login(username, password).subscribe( | 19 | this.authService.login(username, password).subscribe( |
diff --git a/client/app/users/shared/auth.service.ts b/client/app/users/shared/auth.service.ts index b1da94436..d63fe38f3 100644 --- a/client/app/users/shared/auth.service.ts +++ b/client/app/users/shared/auth.service.ts | |||
@@ -7,14 +7,14 @@ import { User } from './user.model'; | |||
7 | 7 | ||
8 | @Injectable() | 8 | @Injectable() |
9 | export class AuthService { | 9 | export class AuthService { |
10 | private static BASE_LOGIN_URL = '/api/v1/users/token'; | ||
11 | private static BASE_CLIENT_URL = '/api/v1/users/client'; | 10 | private static BASE_CLIENT_URL = '/api/v1/users/client'; |
11 | private static BASE_LOGIN_URL = '/api/v1/users/token'; | ||
12 | 12 | ||
13 | loginChangedSource: Observable<AuthStatus>; | 13 | loginChangedSource: Observable<AuthStatus>; |
14 | 14 | ||
15 | private loginChanged: Subject<AuthStatus>; | ||
16 | private clientId: string; | 15 | private clientId: string; |
17 | private clientSecret: string; | 16 | private clientSecret: string; |
17 | private loginChanged: Subject<AuthStatus>; | ||
18 | 18 | ||
19 | constructor(private http: Http) { | 19 | constructor(private http: Http) { |
20 | this.loginChanged = new Subject<AuthStatus>(); | 20 | this.loginChanged = new Subject<AuthStatus>(); |
@@ -37,40 +37,14 @@ export class AuthService { | |||
37 | ); | 37 | ); |
38 | } | 38 | } |
39 | 39 | ||
40 | login(username: string, password: string) { | 40 | getAuthRequestOptions(): RequestOptions { |
41 | let body = new URLSearchParams(); | 41 | return new RequestOptions({ headers: this.getRequestHeader() }); |
42 | body.set('client_id', this.clientId); | ||
43 | body.set('client_secret', this.clientSecret); | ||
44 | body.set('response_type', 'code'); | ||
45 | body.set('grant_type', 'password'); | ||
46 | body.set('scope', 'upload'); | ||
47 | body.set('username', username); | ||
48 | body.set('password', password); | ||
49 | |||
50 | let headers = new Headers(); | ||
51 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
52 | |||
53 | let options = { | ||
54 | headers: headers | ||
55 | }; | ||
56 | |||
57 | return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) | ||
58 | .map(res => res.json()) | ||
59 | .catch(this.handleError); | ||
60 | } | ||
61 | |||
62 | logout() { | ||
63 | // TODO make HTTP request | ||
64 | } | 42 | } |
65 | 43 | ||
66 | getRequestHeader() { | 44 | getRequestHeader() { |
67 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | 45 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); |
68 | } | 46 | } |
69 | 47 | ||
70 | getAuthRequestOptions(): RequestOptions { | ||
71 | return new RequestOptions({ headers: this.getRequestHeader() }); | ||
72 | } | ||
73 | |||
74 | getToken() { | 48 | getToken() { |
75 | return localStorage.getItem('access_token'); | 49 | return localStorage.getItem('access_token'); |
76 | } | 50 | } |
@@ -97,6 +71,32 @@ export class AuthService { | |||
97 | } | 71 | } |
98 | } | 72 | } |
99 | 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) { | 100 | setStatus(status: AuthStatus) { |
101 | this.loginChanged.next(status); | 101 | this.loginChanged.next(status); |
102 | } | 102 | } |