From 8083e7fc5fe05ac24bd918dd16e7634ed5b319a4 Mon Sep 17 00:00:00 2001 From: Michel ten Voorde Date: Wed, 8 Oct 2025 21:36:13 +0200 Subject: [PATCH] Updated auth config --- package-lock.json | 10 +++ package.json | 1 + src/app/app.component.html | 4 +- src/app/app.component.ts | 15 ++-- src/app/app.config.ts | 5 +- src/app/app.routes.ts | 26 +++--- src/app/auth/auth-guard.service.ts | 22 +++++ src/app/auth/auth.interceptor.ts | 35 ++++++++ src/app/auth/auth.service.ts | 86 +++++++++++++++++++ src/app/components/login/login.component.html | 9 +- src/app/components/login/login.component.ts | 68 ++++++++------- .../player-edit/player-edit.component.ts | 1 - yarn.lock | 5 ++ 13 files changed, 225 insertions(+), 62 deletions(-) create mode 100644 src/app/auth/auth-guard.service.ts create mode 100644 src/app/auth/auth.interceptor.ts create mode 100644 src/app/auth/auth.service.ts diff --git a/package-lock.json b/package-lock.json index a79818e..be1f7f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "@popperjs/core": "^2.11.8", "bootstrap": "^5.3.7", "express": "^4.21.2", + "jwt-decode": "^4.0.0", "moment": "2.30.1", "ngx-cookie-service-ssr": "^20.1.0", "ngx-mask": "^20.0.3", @@ -6835,6 +6836,15 @@ ], "license": "MIT" }, + "node_modules/jwt-decode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", + "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/karma": { "version": "6.4.4", "resolved": "https://registry.npmjs.org/karma/-/karma-6.4.4.tgz", diff --git a/package.json b/package.json index bbb019a..6167216 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@popperjs/core": "^2.11.8", "bootstrap": "^5.3.7", "express": "^4.21.2", + "jwt-decode": "^4.0.0", "moment": "2.30.1", "ngx-cookie-service-ssr": "^20.1.0", "ngx-mask": "^20.0.3", diff --git a/src/app/app.component.html b/src/app/app.component.html index c16560c..c3efecc 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -17,10 +17,10 @@ group Spelers - @if (this.userService.isLoggedIn()) { + @if (this.authService.isLoggedIn()) { +

{{message}}

diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 46a784b..694e014 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -1,4 +1,4 @@ -import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms'; +import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms'; import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Router, RouterLink} from '@angular/router'; import {MatCard, MatCardContent, MatCardHeader, MatCardTitle} from "@angular/material/card"; @@ -10,6 +10,9 @@ import {AuthenticationService} from "../../authentication/authentication.service import {UserService} from "../../authentication/user.service"; import {LoginCredentials} from "../../authentication/loginCredentials"; import {User} from "../../authentication/user"; +import {jwtDecode, JwtPayload} from "jwt-decode"; +import {AuthService} from "../../auth/auth.service"; +import {MatSnackBar} from "@angular/material/snack-bar"; @Component({ @@ -23,6 +26,7 @@ import {User} from "../../authentication/user"; MatInput, MatLabel, MatCard, + FormsModule, ], standalone: true, styleUrls: ['./login.component.scss'] @@ -32,48 +36,46 @@ export class LoginComponent implements OnInit { private returnUrl: string; private ipAddress: string; - constructor( - private route: ActivatedRoute, - private fb: FormBuilder, - private authenticationService: AuthenticationService, - private router: Router, - private userPersistenceService: UserService, - ) { - this.initializeForm(); + username: string = ""; + password: string = ""; + message: string = ""; + + constructor(private authService: AuthService, + private route: ActivatedRoute, + private router: Router, + private snackBar: MatSnackBar) { } - private initializeForm() { - this.form = this.fb.group({ - username: ['', [Validators.required]], - password: ['', [Validators.required]], - }); - } ngOnInit() { this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; } - login() { - if (this.form.invalid) { - return; - } + public login(): void { + console.log('Na inloggen door naar' + this.returnUrl); + sessionStorage.removeItem("app.token"); - let loginCredentials = new LoginCredentials(); - loginCredentials = { - ...loginCredentials, - ...this.form.value, - }; + this.authService.login(this.username, this.password) + .subscribe({ + next: (token) => { + console.log('Login successful'); + sessionStorage.setItem("app.token", token); - this.authenticationService - .login(loginCredentials) - .subscribe( - { - next: (user: User) => { - this.userPersistenceService.setUser(user); - this.router.navigate([this.returnUrl]); - } + const decodedToken = jwtDecode(token); + // @ts-ignore + sessionStorage.setItem("app.roles", decodedToken.scope); + + // this.router.navigateByUrl("/persons"); + console.log('Let us go to {url}', this.returnUrl); + + // this.router.navigate([this.returnUrl]); + this.router.navigate([this.route.snapshot.queryParams['returnUrl'] || '/']); + }, + error: (error) => { + console.log('Login unsuccesful'); + this.snackBar.open(`Login failed: ${error.status}`, "OK") } - ); + }); } } diff --git a/src/app/components/player-edit/player-edit.component.ts b/src/app/components/player-edit/player-edit.component.ts index d252d67..1f60886 100644 --- a/src/app/components/player-edit/player-edit.component.ts +++ b/src/app/components/player-edit/player-edit.component.ts @@ -45,7 +45,6 @@ export class PlayerEditComponent implements OnInit { player: Player; isEditMode: boolean = false; - constructor( private playerService: PlayerService, private route: ActivatedRoute, diff --git a/yarn.lock b/yarn.lock index ab11aee..1583c84 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3114,6 +3114,11 @@ jsonparse@^1.3.1: resolved "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz" integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg== +jwt-decode@^4.0.0: + version "4.0.0" + resolved "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz" + integrity sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA== + karma-chrome-launcher@~3.2.0: version "3.2.0" resolved "https://registry.npmjs.org/karma-chrome-launcher/-/karma-chrome-launcher-3.2.0.tgz"