Skip to content
Snippets Groups Projects
Commit cff32d9f authored by KPOTY Kpotivi's avatar KPOTY Kpotivi
Browse files

feat: added authentication and guard service

parent bdc26961
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"generate-api": "rm -rf openapi-generator-cli.jar && curl --insecure https://repo.maven.apache.org/maven2/org/openapitools/openapi-generator-cli/6.6.0/openapi-generator-cli-6.6.0.jar -o openapi-generator-cli.jar && java -jar openapi-generator-cli.jar generate -i ../backend/src/main/java/com/shop/sphere/api/shop-sphere.yaml -g typescript-angular -o ./src/api-client --additional-properties=stringEnums=true,ngVersion=16.2.11",
"generate-api": "rm -rf openapi-generator-cli.jar ./src/api-client && curl --insecure https://repo.maven.apache.org/maven2/org/openapitools/openapi-generator-cli/6.6.0/openapi-generator-cli-6.6.0.jar -o openapi-generator-cli.jar && java -jar openapi-generator-cli.jar generate -i ../backend/src/main/java/com/shop/sphere/api/shop-sphere.yaml -g typescript-angular -o ./src/api-client --additional-properties=stringEnums=true,ngVersion=16.2.11",
"start": "npm run generate-api && ng serve --port ",
"build": "npm run generate-api && ng build",
"watch": "ng build --watch --configuration development",
......
......@@ -22,7 +22,6 @@
<li><strong>FirstName</strong>: {{ order.client.firstName }}<br /></li>
<li><strong>LastName</strong>: {{ order.client.lastName }}<br /></li>
<li><strong>Email</strong>: {{ order.client.email }}<br /></li>
<li><strong>Password</strong>: {{ order.client.password }}<br /></li>
<li><strong>Address</strong>: {{ order.client.address }}<br /></li>
</ul>
</div>
......
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { LoginDTO, LoginService, UserConnectedDTO } from 'src/api-client';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private isAuthenticated: boolean = false;
constructor(private loginService: LoginService, private router: Router) {}
public login(email: string, password: string): void {
this.loginService.login({ email, password } as LoginDTO).subscribe({
next: (data: UserConnectedDTO) => {
this.isAuthenticated = true;
localStorage.setItem('auth-data', JSON.stringify(data));
this.navigateToHome();
},
error: (error) => console.log(error),
});
}
public logout(): void {
this.isAuthenticated = false;
localStorage.removeItem('auth-data');
this.navigateToHome();
}
public isAuthenticatedUser(): boolean {
return this.isAuthenticated;
}
private navigateToHome(): void {
this.router.navigate(['home']);
}
}
import { inject } from '@angular/core';
import { CanActivateFn } from '@angular/router';
import { AuthService } from './auth.service';
/*
* This function control the access to a path
* by checking wether user is connected or not
*/
export const canActivate: CanActivateFn = () => {
const authService = inject(AuthService);
return authService.isAuthenticatedUser();
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment