Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions app/_services/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import {Http, Headers, Response, RequestOptions} from '@angular/http';
import {Observable} from "rxjs";
import 'rxjs/add/operator/map'
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';

@Injectable()
export class AuthenticationService {

//private url = 'http://localhost:8000/api-token-auth/'; // FOR REMOTE TESTING (also remove fake backend in app.module.ts)
private url = '/api/authenticate'; // FOR LOCAL TESTING

private options: RequestOptions;

public token: string;

constructor(private http: Http) {
// set token if saved in local storage
var currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.token = currentUser && currentUser.token;

let headers = new Headers({ 'Content-Type': 'application/json' });
this.options = new RequestOptions({ headers: headers });
}

login(username: string, password: string): Observable<boolean> {
return this.http.post('/api/authenticate', JSON.stringify({ username: username, password: password }))
return this.http.post(this.url, JSON.stringify({ username: username, password: password }), this.options)
.map((response: Response) => {

// login successful if there's a jwt token in the response
let token = response.json() && response.json().token;
if (token) {
Expand All @@ -31,12 +43,26 @@ export class AuthenticationService {
// return false to indicate failed login
return false;
}
});
})
.catch(this.handleError);
}

logout(): void {
// clear token remove user from local storage to log user out
this.token = null;
localStorage.removeItem('currentUser');
}

private handleError (error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
26 changes: 18 additions & 8 deletions app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ export class LoginComponent implements OnInit {
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(result => {
if (result === true) {
this.router.navigate(['/']);
} else {
this.error = 'Username or password is incorrect';
this.loading = false;
}
});
.subscribe(
result => this.handleResponse(result),
error => this.handleError(error)
);
}

handleResponse(result: boolean) {
if (result === true) {
this.router.navigate(['/']);
} else {
this.error = 'Username or password is incorrect';
this.loading = false;
}
}

handleError(error: string) {
this.error = 'Error caught during authentication: ' + error;
this.loading = false;
}
}
1 change: 1 addition & 0 deletions systemjs.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
defaultExtension: 'js'
},
rxjs: {
main: 'bundles/Rx.js',
defaultExtension: 'js'
}
}
Expand Down