From 542435db99863009537e0eebbb91ee5955ed0e66 Mon Sep 17 00:00:00 2001 From: ExostFlash Date: Mon, 2 Jun 2025 10:24:50 +0200 Subject: [PATCH] Add connexion with app --- .../src/app/app-routing.module.ts | 15 ++++++-- FencerJudgeFront/src/app/app.module.ts | 16 +++++---- .../essentials/login/login.component.css | 0 .../essentials/login/login.component.html | 18 ++++++++++ .../essentials/login/login.component.spec.ts | 23 ++++++++++++ .../essentials/login/login.component.ts | 25 +++++++++++++ .../essentials/logout/logout.component.css | 0 .../essentials/logout/logout.component.html | 1 + .../logout/logout.component.spec.ts | 23 ++++++++++++ .../essentials/logout/logout.component.ts | 11 ++++++ .../src/app/components/home/home.component.ts | 6 ++-- .../src/app/guards/auth.guard.spec.ts | 17 +++++++++ FencerJudgeFront/src/app/guards/auth.guard.ts | 15 ++++++++ .../src/app/services/auth.service.spec.ts | 16 +++++++++ .../src/app/services/auth.service.ts | 35 +++++++++++++++++++ FencerJudgeFront/tsconfig.json | 8 ++++- 16 files changed, 214 insertions(+), 15 deletions(-) create mode 100644 FencerJudgeFront/src/app/components/essentials/login/login.component.css create mode 100644 FencerJudgeFront/src/app/components/essentials/login/login.component.html create mode 100644 FencerJudgeFront/src/app/components/essentials/login/login.component.spec.ts create mode 100644 FencerJudgeFront/src/app/components/essentials/login/login.component.ts create mode 100644 FencerJudgeFront/src/app/components/essentials/logout/logout.component.css create mode 100644 FencerJudgeFront/src/app/components/essentials/logout/logout.component.html create mode 100644 FencerJudgeFront/src/app/components/essentials/logout/logout.component.spec.ts create mode 100644 FencerJudgeFront/src/app/components/essentials/logout/logout.component.ts create mode 100644 FencerJudgeFront/src/app/guards/auth.guard.spec.ts create mode 100644 FencerJudgeFront/src/app/guards/auth.guard.ts create mode 100644 FencerJudgeFront/src/app/services/auth.service.spec.ts create mode 100644 FencerJudgeFront/src/app/services/auth.service.ts diff --git a/FencerJudgeFront/src/app/app-routing.module.ts b/FencerJudgeFront/src/app/app-routing.module.ts index 0297262..45896b5 100644 --- a/FencerJudgeFront/src/app/app-routing.module.ts +++ b/FencerJudgeFront/src/app/app-routing.module.ts @@ -1,10 +1,19 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; -const routes: Routes = []; +import { authGuard } from './guards/auth.guard'; + +import { LoginComponent } from './components/essentials/login/login.component'; +import { HomeComponent } from './components/home/home.component'; + +const routes: Routes = [ + { path: 'login', component: LoginComponent }, + { path: '', component: HomeComponent }, + { path: 'home', component: HomeComponent }, +]; @NgModule({ imports: [RouterModule.forRoot(routes)], - exports: [RouterModule] + exports: [RouterModule], }) -export class AppRoutingModule { } +export class AppRoutingModule {} diff --git a/FencerJudgeFront/src/app/app.module.ts b/FencerJudgeFront/src/app/app.module.ts index 6a4581d..1a172c4 100644 --- a/FencerJudgeFront/src/app/app.module.ts +++ b/FencerJudgeFront/src/app/app.module.ts @@ -1,24 +1,26 @@ import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HeaderComponent } from './components/essentials/header/header.component'; import { FooterComponent } from './components/essentials/footer/footer.component'; import { HomeComponent } from './components/home/home.component'; +import { LoginComponent } from './components/essentials/login/login.component'; +import { LogoutComponent } from './components/essentials/logout/logout.component'; @NgModule({ declarations: [ AppComponent, HeaderComponent, FooterComponent, - HomeComponent - ], - imports: [ - BrowserModule, - AppRoutingModule + HomeComponent, + LoginComponent, + LogoutComponent, ], + imports: [BrowserModule, AppRoutingModule, FormsModule], providers: [], - bootstrap: [AppComponent] + bootstrap: [AppComponent], }) -export class AppModule { } +export class AppModule {} diff --git a/FencerJudgeFront/src/app/components/essentials/login/login.component.css b/FencerJudgeFront/src/app/components/essentials/login/login.component.css new file mode 100644 index 0000000..e69de29 diff --git a/FencerJudgeFront/src/app/components/essentials/login/login.component.html b/FencerJudgeFront/src/app/components/essentials/login/login.component.html new file mode 100644 index 0000000..4028cda --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/login/login.component.html @@ -0,0 +1,18 @@ +
+ + + +
{{ errorMessage }}
+
diff --git a/FencerJudgeFront/src/app/components/essentials/login/login.component.spec.ts b/FencerJudgeFront/src/app/components/essentials/login/login.component.spec.ts new file mode 100644 index 0000000..4adbc3d --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/login/login.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { LoginComponent } from './login.component'; + +describe('LoginComponent', () => { + let component: LoginComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [LoginComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(LoginComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/FencerJudgeFront/src/app/components/essentials/login/login.component.ts b/FencerJudgeFront/src/app/components/essentials/login/login.component.ts new file mode 100644 index 0000000..3ff2888 --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/login/login.component.ts @@ -0,0 +1,25 @@ +import { Component } from '@angular/core'; +import { Router } from '@angular/router'; +import { AuthService } from '@services/auth.service'; + +@Component({ + selector: 'app-login', + standalone: false, + templateUrl: './login.component.html', + styleUrl: './login.component.css', +}) +export class LoginComponent { + email = ''; + password = ''; + errorMessage = ''; + + constructor(private authService: AuthService, private router: Router) {} + + onSubmit() { + if (this.authService.login(this.email, this.password)) { + this.router.navigate(['/']); + } else { + this.errorMessage = 'Identifiants incorrects'; + } + } +} diff --git a/FencerJudgeFront/src/app/components/essentials/logout/logout.component.css b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.css new file mode 100644 index 0000000..e69de29 diff --git a/FencerJudgeFront/src/app/components/essentials/logout/logout.component.html b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.html new file mode 100644 index 0000000..c6ae40e --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.html @@ -0,0 +1 @@ +

logout works!

diff --git a/FencerJudgeFront/src/app/components/essentials/logout/logout.component.spec.ts b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.spec.ts new file mode 100644 index 0000000..074294b --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { LogoutComponent } from './logout.component'; + +describe('LogoutComponent', () => { + let component: LogoutComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [LogoutComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(LogoutComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/FencerJudgeFront/src/app/components/essentials/logout/logout.component.ts b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.ts new file mode 100644 index 0000000..f8b8892 --- /dev/null +++ b/FencerJudgeFront/src/app/components/essentials/logout/logout.component.ts @@ -0,0 +1,11 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-logout', + standalone: false, + templateUrl: './logout.component.html', + styleUrl: './logout.component.css' +}) +export class LogoutComponent { + +} diff --git a/FencerJudgeFront/src/app/components/home/home.component.ts b/FencerJudgeFront/src/app/components/home/home.component.ts index 906862d..67af68d 100644 --- a/FencerJudgeFront/src/app/components/home/home.component.ts +++ b/FencerJudgeFront/src/app/components/home/home.component.ts @@ -4,8 +4,6 @@ import { Component } from '@angular/core'; selector: 'app-home', standalone: false, templateUrl: './home.component.html', - styleUrl: './home.component.css' + styleUrl: './home.component.css', }) -export class HomeComponent { - -} +export class HomeComponent {} diff --git a/FencerJudgeFront/src/app/guards/auth.guard.spec.ts b/FencerJudgeFront/src/app/guards/auth.guard.spec.ts new file mode 100644 index 0000000..4ae275e --- /dev/null +++ b/FencerJudgeFront/src/app/guards/auth.guard.spec.ts @@ -0,0 +1,17 @@ +import { TestBed } from '@angular/core/testing'; +import { CanActivateFn } from '@angular/router'; + +import { authGuard } from './auth.guard'; + +describe('authGuard', () => { + const executeGuard: CanActivateFn = (...guardParameters) => + TestBed.runInInjectionContext(() => authGuard(...guardParameters)); + + beforeEach(() => { + TestBed.configureTestingModule({}); + }); + + it('should be created', () => { + expect(executeGuard).toBeTruthy(); + }); +}); diff --git a/FencerJudgeFront/src/app/guards/auth.guard.ts b/FencerJudgeFront/src/app/guards/auth.guard.ts new file mode 100644 index 0000000..b3ed4e3 --- /dev/null +++ b/FencerJudgeFront/src/app/guards/auth.guard.ts @@ -0,0 +1,15 @@ +import { inject } from '@angular/core'; +import { CanActivateFn, Router } from '@angular/router'; +import { AuthService } from '@services/auth.service'; + +export const authGuard: CanActivateFn = (route, state) => { + const authService = inject(AuthService); + const router = inject(Router); + + if (!authService.isAuthenticated()) { + router.navigate(['/login']); + return false; + } + + return true; +}; diff --git a/FencerJudgeFront/src/app/services/auth.service.spec.ts b/FencerJudgeFront/src/app/services/auth.service.spec.ts new file mode 100644 index 0000000..f1251ca --- /dev/null +++ b/FencerJudgeFront/src/app/services/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/FencerJudgeFront/src/app/services/auth.service.ts b/FencerJudgeFront/src/app/services/auth.service.ts new file mode 100644 index 0000000..522d786 --- /dev/null +++ b/FencerJudgeFront/src/app/services/auth.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthService { + private isLoggedIn = false; + private readonly hardcodedUser = { + email: 'user@test.com', + password: 'password123', + }; + + constructor(private router: Router) {} + + login(email: string, password: string): boolean { + if ( + email === this.hardcodedUser.email && + password === this.hardcodedUser.password + ) { + this.isLoggedIn = true; + return true; + } + return false; + } + + logout(): void { + this.isLoggedIn = false; + this.router.navigate(['/login']); + } + + isAuthenticated(): boolean { + return this.isLoggedIn; + } +} diff --git a/FencerJudgeFront/tsconfig.json b/FencerJudgeFront/tsconfig.json index 5525117..06d41df 100644 --- a/FencerJudgeFront/tsconfig.json +++ b/FencerJudgeFront/tsconfig.json @@ -16,7 +16,13 @@ "moduleResolution": "bundler", "importHelpers": true, "target": "ES2022", - "module": "ES2022" + "module": "ES2022", + "baseUrl": "./src", + "paths": { + "@interfaces/*": ["app/interfaces/*"], + "@services/*": ["app/services/*"], + "@utils/*": ["app/utils/*"] + } }, "angularCompilerOptions": { "enableI18nLegacyMessageIdFormat": false,