Implementation of login and logout mechanisms.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {UserRestService} from "../../core/rest-services/user.rest-service";
|
||||
import {LoginRequest} from "../../core/model/login-request";
|
||||
import {Subject} from "rxjs";
|
||||
import {MessageService} from "../../core/services/message.service";
|
||||
import {AuthenticationService} from "../../core/services/authentication.service";
|
||||
import {Router} from "@angular/router";
|
||||
|
||||
@Injectable()
|
||||
export class LoginService {
|
||||
private isLoginPending: Subject<boolean> = new Subject<boolean>();
|
||||
|
||||
constructor(
|
||||
private authenticationService: AuthenticationService,
|
||||
private messageService: MessageService,
|
||||
private router: Router,
|
||||
private userRestService: UserRestService
|
||||
) {}
|
||||
|
||||
login(loginRequest: LoginRequest): void {
|
||||
this.isLoginPending.next(true);
|
||||
|
||||
this.userRestService.login(loginRequest)
|
||||
.then(loginResponse => {
|
||||
this.messageService.display('Login success!');
|
||||
this.authenticationService.setAuthenticated(loginResponse);
|
||||
this.router.navigate(['/']);
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.status === 400) {
|
||||
this.messageService.display('Login or password incorrect.')
|
||||
} else {
|
||||
this.messageService.display('An error occured while login.')
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user