Login works
This commit is contained in:
parent
eb7ac86759
commit
4d402778b0
3 changed files with 21 additions and 20 deletions
|
@ -1,8 +1,8 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import {HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
import {HttpClient, HttpResponse } from '@angular/common/http';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { catchError } from 'rxjs/operators';
|
import { Observable } from 'rxjs';
|
||||||
import { Observable, ObservableInput } from 'rxjs';
|
import { tap } from 'rxjs/operators';
|
||||||
|
|
||||||
export class Jwt {
|
export class Jwt {
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -18,19 +18,12 @@ export class APIService {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
login(failFunc: () => void, username: string, password: string) {
|
login(username: string, password: string): Observable<HttpResponse<Jwt>> {
|
||||||
let ff = (err: any, caught: Observable<HttpResponse<Jwt>>): ObservableInput<any> => {
|
return this.http.post<Jwt>('/login', {username: username, password: password}, {observe: 'response'}).pipe(tap((event) => {
|
||||||
console.log(err);
|
|
||||||
failFunc();
|
|
||||||
return caught.pipe();
|
|
||||||
};
|
|
||||||
this.http.post<Jwt>('/login', {username: username, password: password}, {observe: 'response'}).pipe(catchError(ff)).subscribe(event => {
|
|
||||||
if (event.status == 200) {
|
if (event.status == 200) {
|
||||||
sessionStorage.setItem('jwt', event.body?.jwt.toString() ?? '');
|
sessionStorage.setItem('jwt', event.body?.jwt.toString() ?? '');
|
||||||
this._router.navigateByUrl('/home');
|
this._router.navigateByUrl('/home');
|
||||||
} else {
|
|
||||||
failFunc();
|
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@if (failed) {
|
@if (failed) {
|
||||||
<div>
|
<div role="alert" class="alert alert-error">
|
||||||
Login Failed
|
<span>Login Failed!</span>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { Component, inject } from '@angular/core';
|
import { Component, inject } from '@angular/core';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {APIService} from '../api.service';
|
import {APIService} from '../api.service';
|
||||||
|
import {catchError, of } from 'rxjs';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
|
@ -11,16 +13,22 @@ import {APIService} from '../api.service';
|
||||||
})
|
})
|
||||||
export class LoginComponent {
|
export class LoginComponent {
|
||||||
apiService: APIService = inject(APIService);
|
apiService: APIService = inject(APIService);
|
||||||
|
router: Router = inject(Router);
|
||||||
username: string = '';
|
username: string = '';
|
||||||
password: string = '';
|
password: string = '';
|
||||||
failed: boolean = false;
|
failed: boolean = false;
|
||||||
|
|
||||||
failLogin() {
|
|
||||||
failed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
onSubmit() {
|
onSubmit() {
|
||||||
this.failed = false;
|
this.failed = false;
|
||||||
this.apiService.login(this.failLogin, this.username, this.password);
|
this.apiService.login(this.username, this.password).pipe(catchError(() => {
|
||||||
|
this.failed = true;
|
||||||
|
return of(null);
|
||||||
|
})).subscribe((event) => {
|
||||||
|
if (event?.status == 200) {
|
||||||
|
this.router.navigateByUrl('/home')
|
||||||
|
} else {
|
||||||
|
this.failed = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue