From 3f91be07a2d5d0c54258df511b6a276f913200b8 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sun, 1 Sep 2024 10:02:10 +0200 Subject: [PATCH] added auth.guard & clean code --- src/app/app.routes.ts | 43 +++++++++++++------ src/app/auth.guard.spec.ts | 17 ++++++++ src/app/auth.guard.ts | 29 +++++++++++++ src/app/components/login/login.component.html | 1 + src/app/services/firebase.service.ts | 13 ++++++ src/app/services/login.service.ts | 8 ++++ .../help/help.component.html | 0 .../help/help.component.scss | 0 .../help/help.component.spec.ts | 0 .../help/help.component.ts | 0 .../imprint/imprint.component.html | 0 .../imprint/imprint.component.scss | 0 .../imprint/imprint.component.spec.ts | 0 .../imprint/imprint.component.ts | 0 .../privacy-policy.component.html | 0 .../privacy-policy.component.scss | 0 .../privacy-policy.component.spec.ts | 0 .../privacy-policy.component.ts | 0 src/assets/i18n/de.json | 2 +- 19 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/app/auth.guard.spec.ts create mode 100644 src/app/auth.guard.ts rename src/app/shared/components/{legal-informations => legal-information}/help/help.component.html (100%) rename src/app/shared/components/{legal-informations => legal-information}/help/help.component.scss (100%) rename src/app/shared/components/{legal-informations => legal-information}/help/help.component.spec.ts (100%) rename src/app/shared/components/{legal-informations => legal-information}/help/help.component.ts (100%) rename src/app/shared/components/{legal-informations => legal-information}/imprint/imprint.component.html (100%) rename src/app/shared/components/{legal-informations => legal-information}/imprint/imprint.component.scss (100%) rename src/app/shared/components/{legal-informations => legal-information}/imprint/imprint.component.spec.ts (100%) rename src/app/shared/components/{legal-informations => legal-information}/imprint/imprint.component.ts (100%) rename src/app/shared/components/{legal-informations => legal-information}/privacy-policy/privacy-policy.component.html (100%) rename src/app/shared/components/{legal-informations => legal-information}/privacy-policy/privacy-policy.component.scss (100%) rename src/app/shared/components/{legal-informations => legal-information}/privacy-policy/privacy-policy.component.spec.ts (100%) rename src/app/shared/components/{legal-informations => legal-information}/privacy-policy/privacy-policy.component.ts (100%) diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index a3b8203..9da3b6e 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,8 +1,8 @@ import { Routes } from '@angular/router'; import { SummaryComponent } from './components/summary/summary.component'; -import { HelpComponent } from './shared/components/legal-informations/help/help.component'; -import { ImprintComponent } from './shared/components/legal-informations/imprint/imprint.component'; -import { PrivacyPolicyComponent } from './shared/components/legal-informations/privacy-policy/privacy-policy.component'; +import { HelpComponent } from './shared/components/legal-information/help/help.component'; +import { ImprintComponent } from './shared/components/legal-information/imprint/imprint.component'; +import { PrivacyPolicyComponent } from './shared/components/legal-information/privacy-policy/privacy-policy.component'; import { AddTaskComponent } from './components/add-task/add-task.component'; import { BoardComponent } from './components/board/board.component'; import { ContactsComponent } from './components/contacts/contacts.component'; @@ -12,6 +12,7 @@ import { LoginComponent } from './components/login/login.component'; import { RegisterComponent } from './components/login/register/register.component'; import { ForgotPwComponent } from './components/login/forgot-pw/forgot-pw.component'; import { PwResetComponent } from './components/login/forgot-pw/pw-reset/pw-reset.component'; +import { AuthGuard } from './auth.guard'; export const routes: Routes = [ { path: '', component: LoginComponent }, @@ -22,14 +23,30 @@ export const routes: Routes = [ { path: 'register', component: RegisterComponent }, { path: 'forgot-pw', component: ForgotPwComponent }, { path: 'pw-reset', component: PwResetComponent }, - { path: 'summary', component: SummaryComponent }, - { path: 'add-task', component: AddTaskComponent }, - { path: 'add-task/:id', component: AddTaskComponent }, - { path: 'board', component: BoardComponent }, - { path: 'task/:id', component: TaskOverlayComponent }, - { path: 'task-edit/:id', component: TaskEditOverlayComponent }, - { path: 'contacts', component: ContactsComponent }, - { path: 'help', component: HelpComponent }, - { path: 'imprint', component: ImprintComponent }, - { path: 'privacy-policy', component: PrivacyPolicyComponent }, + { path: 'summary', component: SummaryComponent, canActivate: [AuthGuard] }, + { path: 'add-task', component: AddTaskComponent, canActivate: [AuthGuard] }, + { + path: 'add-task/:id', + component: AddTaskComponent, + canActivate: [AuthGuard], + }, + { path: 'board', component: BoardComponent, canActivate: [AuthGuard] }, + { + path: 'task/:id', + component: TaskOverlayComponent, + canActivate: [AuthGuard], + }, + { + path: 'task-edit/:id', + component: TaskEditOverlayComponent, + canActivate: [AuthGuard], + }, + { path: 'contacts', component: ContactsComponent, canActivate: [AuthGuard] }, + { path: 'help', component: HelpComponent, canActivate: [AuthGuard] }, + { path: 'imprint', component: ImprintComponent, canActivate: [AuthGuard] }, + { + path: 'privacy-policy', + component: PrivacyPolicyComponent, + canActivate: [AuthGuard], + }, ]; diff --git a/src/app/auth.guard.spec.ts b/src/app/auth.guard.spec.ts new file mode 100644 index 0000000..4ae275e --- /dev/null +++ b/src/app/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/src/app/auth.guard.ts b/src/app/auth.guard.ts new file mode 100644 index 0000000..6ca3723 --- /dev/null +++ b/src/app/auth.guard.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { Observable, of } from 'rxjs'; +import { map, catchError } from 'rxjs/operators'; +import { LoginService } from './services/login.service'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthGuard { + constructor(private loginService: LoginService, private router: Router) {} + + canActivate(): Observable | Promise | boolean { + return this.loginService.checkAuthUser().pipe( + map((isAuthenticated) => { + if (isAuthenticated) { + return true; + } else { + this.router.navigate(['/']); + return false; + } + }), + catchError(() => { + this.router.navigate(['/']); + return of(false); + }) + ); + } +} diff --git a/src/app/components/login/login.component.html b/src/app/components/login/login.component.html index 3259203..674be76 100644 --- a/src/app/components/login/login.component.html +++ b/src/app/components/login/login.component.html @@ -55,6 +55,7 @@ autocomplete="current-password" pattern="[^<>]*" [(ngModel)]="loginData.password" + minlength="6" required /> @if(!loginData.password) { diff --git a/src/app/services/firebase.service.ts b/src/app/services/firebase.service.ts index a8daaab..5501630 100644 --- a/src/app/services/firebase.service.ts +++ b/src/app/services/firebase.service.ts @@ -13,6 +13,7 @@ import { Task } from '../interfaces/task.interface'; import { User } from '../interfaces/user.interface'; import CryptoES from 'crypto-es'; import { CryptoESSecretKey } from './../environments/config'; +import { Observable, of } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -172,6 +173,18 @@ export class FirebaseService implements OnDestroy { } } + // ------------- AUTH ------------- // + + getAuthUser(): Observable { + const encryptedValue = localStorage.getItem('currentUserJOIN'); + if (encryptedValue) { + const bytes = CryptoES.AES.decrypt(encryptedValue, this.secretKey); + const decryptedValue = bytes.toString(CryptoES.enc.Utf8); + return of(JSON.parse(decryptedValue)); + } + return of(null); + } + ngOnDestroy() { this.unsubTask(); this.unsubUser(); diff --git a/src/app/services/login.service.ts b/src/app/services/login.service.ts index 3d4d935..b1b354d 100644 --- a/src/app/services/login.service.ts +++ b/src/app/services/login.service.ts @@ -24,6 +24,7 @@ import { User } from '../interfaces/user.interface'; import { Router } from '@angular/router'; import CryptoES from 'crypto-es'; import { CryptoESSecretKey } from './../environments/config'; +import { catchError, map, Observable, of } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -42,6 +43,13 @@ export class LoginService { private router: Router ) {} + checkAuthUser(): Observable { + return this.firebaseService.getAuthUser().pipe( + map((user) => !!user), + catchError(() => of(false)) + ); + } + login(loginData: { mail: string; password: string }) { signInWithEmailAndPassword(getAuth(), loginData.mail, loginData.password) .then((userCredential) => { diff --git a/src/app/shared/components/legal-informations/help/help.component.html b/src/app/shared/components/legal-information/help/help.component.html similarity index 100% rename from src/app/shared/components/legal-informations/help/help.component.html rename to src/app/shared/components/legal-information/help/help.component.html diff --git a/src/app/shared/components/legal-informations/help/help.component.scss b/src/app/shared/components/legal-information/help/help.component.scss similarity index 100% rename from src/app/shared/components/legal-informations/help/help.component.scss rename to src/app/shared/components/legal-information/help/help.component.scss diff --git a/src/app/shared/components/legal-informations/help/help.component.spec.ts b/src/app/shared/components/legal-information/help/help.component.spec.ts similarity index 100% rename from src/app/shared/components/legal-informations/help/help.component.spec.ts rename to src/app/shared/components/legal-information/help/help.component.spec.ts diff --git a/src/app/shared/components/legal-informations/help/help.component.ts b/src/app/shared/components/legal-information/help/help.component.ts similarity index 100% rename from src/app/shared/components/legal-informations/help/help.component.ts rename to src/app/shared/components/legal-information/help/help.component.ts diff --git a/src/app/shared/components/legal-informations/imprint/imprint.component.html b/src/app/shared/components/legal-information/imprint/imprint.component.html similarity index 100% rename from src/app/shared/components/legal-informations/imprint/imprint.component.html rename to src/app/shared/components/legal-information/imprint/imprint.component.html diff --git a/src/app/shared/components/legal-informations/imprint/imprint.component.scss b/src/app/shared/components/legal-information/imprint/imprint.component.scss similarity index 100% rename from src/app/shared/components/legal-informations/imprint/imprint.component.scss rename to src/app/shared/components/legal-information/imprint/imprint.component.scss diff --git a/src/app/shared/components/legal-informations/imprint/imprint.component.spec.ts b/src/app/shared/components/legal-information/imprint/imprint.component.spec.ts similarity index 100% rename from src/app/shared/components/legal-informations/imprint/imprint.component.spec.ts rename to src/app/shared/components/legal-information/imprint/imprint.component.spec.ts diff --git a/src/app/shared/components/legal-informations/imprint/imprint.component.ts b/src/app/shared/components/legal-information/imprint/imprint.component.ts similarity index 100% rename from src/app/shared/components/legal-informations/imprint/imprint.component.ts rename to src/app/shared/components/legal-information/imprint/imprint.component.ts diff --git a/src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.html b/src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.html similarity index 100% rename from src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.html rename to src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.html diff --git a/src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.scss b/src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.scss similarity index 100% rename from src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.scss rename to src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.scss diff --git a/src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.spec.ts b/src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.spec.ts similarity index 100% rename from src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.spec.ts rename to src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.spec.ts diff --git a/src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.ts b/src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.ts similarity index 100% rename from src/app/shared/components/legal-informations/privacy-policy/privacy-policy.component.ts rename to src/app/shared/components/legal-information/privacy-policy/privacy-policy.component.ts diff --git a/src/assets/i18n/de.json b/src/assets/i18n/de.json index 6a51abc..337135c 100644 --- a/src/assets/i18n/de.json +++ b/src/assets/i18n/de.json @@ -10,7 +10,7 @@ "errorMail1": "Diese E-Mail Adresse existiert nicht!", "errorMail2": "Dies ist kein gültiges E-Mail-Format!", "errorPassword0": "Sie müssen ein Passwort eingeben!", - "errorPassword1": "Das Passwort stimmt nicht mit der E-Mail Adresse überein!", + "errorPassword1": "Das Passwort passt nicht zur E-Mail-Adresse!", "loginDialog0": "Einloggen", "loginDialog1": "Bitte warten..." },