Add disconnection and minor improvements on login page.

This commit is contained in:
Florian THIERRY
2024-03-27 12:15:41 +01:00
parent 13c2cc8118
commit 0900df463a
14 changed files with 193 additions and 76 deletions

View File

@@ -1,76 +1,81 @@
import { Injectable, inject } from "@angular/core";
import { BehaviorSubject, Observable } from "rxjs";
import { copy } from "../../core/utils/ObjectUtils";
import { FormError } from "../../core/model/FormError";
import { UserRestService } from "../../core/rest-services/user.rest-service";
import { LoginRequest } from "../../core/rest-services/model/login.model";
import { AuthenticationService } from "../../core/service/authentication.service";
import { MatSnackBar } from "@angular/material/snack-bar";
import { Router } from "@angular/router";
import { Injectable, inject } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { copy } from '../../core/utils/ObjectUtils';
import { FormError } from '../../core/model/FormError';
import { UserRestService } from '../../core/rest-services/user.rest-service';
import { LoginRequest } from '../../core/rest-services/model/login.model';
import { AuthenticationService } from '../../core/service/authentication.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';
export interface LoginState {
request: LoginRequest;
errors: FormError[]
request: LoginRequest;
errors: FormError[];
}
const DEFAULT_STATE: LoginState = {
request: {
email: undefined,
password: undefined
},
errors: []
}
request: {
email: undefined,
password: undefined,
},
errors: [],
};
@Injectable()
export class LoginService {
private stateSubject = new BehaviorSubject<LoginState>(copy(DEFAULT_STATE));
private userRestService = inject(UserRestService);
private authenticationService = inject(AuthenticationService);
private snackBar = inject(MatSnackBar);
private router = inject(Router);
get state$(): Observable<LoginState> {
return this.stateSubject.asObservable();
}
private stateSubject = new BehaviorSubject<LoginState>(copy(DEFAULT_STATE));
private userRestService = inject(UserRestService);
private authenticationService = inject(AuthenticationService);
private snackBar = inject(MatSnackBar);
private router = inject(Router);
private get state(): LoginState {
return this.stateSubject.value;
}
get state$(): Observable<LoginState> {
return this.stateSubject.asObservable();
}
private save(newState: LoginState): void {
this.stateSubject.next(newState);
}
private get state(): LoginState {
return this.stateSubject.value;
}
editEmail(newEmail: string): void {
const state = this.state;
private save(newState: LoginState): void {
this.stateSubject.next(newState);
}
state.request.email = newEmail;
editEmail(newEmail: string): void {
const state = this.state;
this.save(state);
}
state.request.email = newEmail;
editPassword(newPassword: string): void {
const state = this.state;
this.save(state);
}
state.request.password = newPassword;
editPassword(newPassword: string): void {
const state = this.state;
this.save(state);
}
state.request.password = newPassword;
performLogin(): void {
const state = this.state;
this.save(state);
}
// Check state is valid
performLogin(): void {
const state = this.state;
this.userRestService.login(state.request)
.then(response => {
this.authenticationService.authenticate(response.accessToken);
this.snackBar.open('Authentication succeeded!', 'Close', { duration: 5000 });
this.router.navigate(['/home']);
})
.catch(error => {
console.error(error)
this.snackBar.open('Authentication failed.', 'Close', { duration: 5000 });
});
}
}
// Check state is valid
this.userRestService
.login(state.request)
.then((response) => {
this.authenticationService.authenticate(response.accessToken);
this.snackBar.open('Authentication succeeded!', 'Close', {
duration: 5000,
});
this.router.navigate(['/home']);
})
.catch((error) => {
console.error(error);
this.snackBar.open('Authentication failed.', 'Close', {
duration: 5000,
});
});
}
}