40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Observable } from 'rxjs';
|
|
|
|
@Component({
|
|
selector: 'login',
|
|
templateUrl: './login.component.html'
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
|
|
model: any = {};
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private http: HttpClient
|
|
) { }
|
|
|
|
ngOnInit() {
|
|
sessionStorage.setItem('token', '');
|
|
}
|
|
|
|
login() {
|
|
let url = 'http://localhost:8082/login';
|
|
this.http.post<Observable<boolean>>(url, {
|
|
userName: this.model.username,
|
|
password: this.model.password
|
|
}).subscribe(isValid => {
|
|
if (isValid) {
|
|
sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password));
|
|
this.router.navigate(['']);
|
|
} else {
|
|
alert("Authentication failed.")
|
|
}
|
|
});
|
|
}
|
|
}
|