Add frontend.
This commit is contained in:
4
frontend/src/app/core/model/FormError.ts
Normal file
4
frontend/src/app/core/model/FormError.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface FormError {
|
||||
fieldName: string;
|
||||
errorMessage: string;
|
||||
}
|
||||
7
frontend/src/app/core/model/User.ts
Normal file
7
frontend/src/app/core/model/User.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
pseudo: string;
|
||||
photoId?: string;
|
||||
roles: string[];
|
||||
}
|
||||
10
frontend/src/app/core/rest-services/model/login.model.ts
Normal file
10
frontend/src/app/core/rest-services/model/login.model.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface LoginRequest {
|
||||
email?: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
tokenType: string,
|
||||
accessToken: string,
|
||||
refreshToken: string
|
||||
}
|
||||
15
frontend/src/app/core/rest-services/user.rest-service.ts
Normal file
15
frontend/src/app/core/rest-services/user.rest-service.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { Injectable, inject } from "@angular/core";
|
||||
import { LoginRequest, LoginResponse } from "./model/login.model";
|
||||
import { lastValueFrom } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserRestService {
|
||||
private httpClient = inject(HttpClient);
|
||||
|
||||
login(request: LoginRequest): Promise<LoginResponse> {
|
||||
return lastValueFrom(this.httpClient.post<LoginResponse>('/api/users/login', request));
|
||||
}
|
||||
}
|
||||
73
frontend/src/app/core/service/authentication.service.ts
Normal file
73
frontend/src/app/core/service/authentication.service.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { User } from "../model/User";
|
||||
|
||||
const JWT_PARAM = 'jwt';
|
||||
|
||||
interface UserDetails {
|
||||
sub: string;
|
||||
exp: number;
|
||||
email: string;
|
||||
pseudo: string;
|
||||
roles: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService {
|
||||
authenticate(token: string): void {
|
||||
localStorage.setItem(JWT_PARAM, token);
|
||||
}
|
||||
|
||||
unauthenticate(): void {
|
||||
localStorage.removeItem(JWT_PARAM);
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
let result = false;
|
||||
|
||||
const userDetails = this.extractUserDetails();
|
||||
|
||||
if (userDetails) {
|
||||
const authenticationExpiration = new Date(userDetails.exp * 1000);
|
||||
result = authenticationExpiration > new Date();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private extractUserFromLocalStorage(): User | undefined {
|
||||
let result: User | undefined = undefined;
|
||||
|
||||
const userDetails = this.extractUserDetails();
|
||||
if (userDetails) {
|
||||
const user = this.convertToUser(userDetails);
|
||||
result = user;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private extractUserDetails(): UserDetails | undefined {
|
||||
let result: UserDetails | undefined = undefined;
|
||||
|
||||
const token = localStorage.getItem(JWT_PARAM);
|
||||
|
||||
const tokenParts = token?.split('.');
|
||||
if (tokenParts?.length === 3 && tokenParts[1].length) {
|
||||
const userDetails: UserDetails = JSON.parse(tokenParts[1]);
|
||||
result = userDetails;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private convertToUser(userDetails: UserDetails): User {
|
||||
return {
|
||||
id: userDetails.sub,
|
||||
email: userDetails.email,
|
||||
pseudo: userDetails.pseudo,
|
||||
roles: userDetails.roles.split(',')
|
||||
};
|
||||
}
|
||||
}
|
||||
5
frontend/src/app/core/utils/ObjectUtils.ts
Normal file
5
frontend/src/app/core/utils/ObjectUtils.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export function copy<T>(object: T): T {
|
||||
return JSON.parse(
|
||||
JSON.stringify(object)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user