BAEL-797 adding login to http calls

This commit is contained in:
tschiman 2017-04-24 22:29:56 -06:00
parent 765cdecb59
commit 136bb775f3
5 changed files with 42 additions and 22 deletions

View File

@ -1,9 +1,10 @@
import {Component} from "@angular/core"; import {Component} from "@angular/core";
import {Principal} from "./principal"; import {Principal} from "./principal";
import {Response, RequestOptions, Headers, Http} from "@angular/http"; import {Response} from "@angular/http";
import {Observable} from "rxjs"; import {Observable} from "rxjs";
import {NgForm} from "@angular/forms"; import {NgForm} from "@angular/forms";
import {Book} from "./book"; import {Book} from "./book";
import {HttpService} from "./http.service";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -22,12 +23,12 @@ export class AppComponent {
private password: String = ''; private password: String = '';
principal: Principal = new Principal(false, []); principal: Principal = new Principal(false, [], null);
// principal: Principal = new Principal(true, [new Authority("ROLE_USER")]); // principal: Principal = new Principal(true, [new Authority("ROLE_USER")], {username: 'user', password: 'password'});
loginFailed: boolean = false; loginFailed: boolean = false;
constructor(private http: Http){} constructor(private httpService: HttpService){}
ngOnInit(): void { ngOnInit(): void {
@ -35,14 +36,8 @@ export class AppComponent {
onLogin(form: NgForm) { onLogin(form: NgForm) {
this.loginFailed = false; this.loginFailed = false;
let headers = new Headers({'Content-Type': 'application/json'}); this.credentials = {username: form.value.username, password: form.value.password};
this.username = form.value.username; this.httpService.login(this.credentials)
this.password = form.value.password;
headers.append('Authorization','Basic ' + btoa(form.value.username + ':' + form.value.password));
headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers});
this.http.get("/me", options)
.map((response: Response) => response.json()) .map((response: Response) => response.json())
.catch((error: Response) => { .catch((error: Response) => {
if (error.status === 401) { if (error.status === 401) {
@ -51,7 +46,7 @@ export class AppComponent {
console.log(error); console.log(error);
return Observable.throw(error); return Observable.throw(error);
}) })
.map((data: any) => new Principal(data.authenticated, data.authorities)) .map((data: any) => new Principal(data.authenticated, data.authorities, this.credentials))
.subscribe((principal: Principal) => { .subscribe((principal: Principal) => {
console.log(principal); console.log(principal);
this.principal = principal; this.principal = principal;
@ -59,11 +54,7 @@ export class AppComponent {
} }
onLogout() { onLogout() {
let headers = new Headers({'Content-Type': 'application/json'}); this.httpService.logout(this.principal.credentials)
headers.append('Authorization','Basic ' + btoa(this.username + ':' + this.password));
headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers});
this.http.post("/logout", '', options)
.catch((error: Response) => { .catch((error: Response) => {
console.log(error); console.log(error);
return Observable.throw(error); return Observable.throw(error);
@ -73,7 +64,7 @@ export class AppComponent {
this.loginFailed = false; this.loginFailed = false;
this.credentials.username = ''; this.credentials.username = '';
this.credentials.password = ''; this.credentials.password = '';
this.principal = new Principal(false, []); this.principal = new Principal(false, [], null);
} }
}); });
} }

View File

@ -8,6 +8,7 @@ import {RatingComponent} from "./rating/rating.component";
import {ClickStopPropagationDirective} from "./click-stop-propagation.directive"; import {ClickStopPropagationDirective} from "./click-stop-propagation.directive";
import {BookDetailComponent} from "./book/book-detail/book-detail.component"; import {BookDetailComponent} from "./book/book-detail/book-detail.component";
import {BookListComponent} from "./book/book-list/book-list.component"; import {BookListComponent} from "./book/book-list/book-list.component";
import {HttpService} from "./http.service";
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -23,7 +24,7 @@ import {BookListComponent} from "./book/book-list/book-list.component";
HttpModule, HttpModule,
NgbModule.forRoot() NgbModule.forRoot()
], ],
providers: [], providers: [HttpService],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }

View File

@ -1,6 +1,7 @@
import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core"; import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";
import {Principal} from "../../principal"; import {Principal} from "../../principal";
import {Book} from "../../book"; import {Book} from "../../book";
import {Http} from "@angular/http";
@Component({ @Component({
selector: 'app-book-list', selector: 'app-book-list',
@ -19,7 +20,7 @@ export class BookListComponent implements OnInit {
isAddNewBook: boolean = false; isAddNewBook: boolean = false;
selectedBook: Book = null; selectedBook: Book = null;
constructor() { } constructor(private http: Http) { }
ngOnInit() { ngOnInit() {
this.loadBooks(); this.loadBooks();

View File

@ -0,0 +1,25 @@
import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {Response, Http, Headers, RequestOptions} from "@angular/http";
@Injectable()
export class HttpService {
constructor(private http: Http) { }
login(user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers});
return this.http.get("/me", options)
}
logout(user: any): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization','Basic ' + btoa(user.username + ':' + user.password));
headers.append('X-Requested-With','XMLHttpRequest');
let options = new RequestOptions({headers: headers});
return this.http.post("/logout", '', options)
}
}

View File

@ -4,10 +4,12 @@
export class Principal { export class Principal {
public authenticated: boolean; public authenticated: boolean;
public authorities: Authority[] = []; public authorities: Authority[] = [];
public credentials: any;
constructor(authenticated: boolean, authorities: any[]) { constructor(authenticated: boolean, authorities: any[], credentials: any) {
this.authenticated = authenticated; this.authenticated = authenticated;
authorities.map(auth => this.authorities.push(new Authority(auth.authority))) authorities.map(auth => this.authorities.push(new Authority(auth.authority)))
this.credentials = credentials;
} }
isAdmin() { isAdmin() {