Cleanup
Some checks failed
Gitea/swiss-client/pipeline/head There was a failure building this commit
Some checks failed
Gitea/swiss-client/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -79,7 +79,6 @@ export class AuthService {
|
||||
const decodedToken = jwtDecode<JwtPayload>(token);
|
||||
return decodedToken.sub || null; // 'sub' is the standard JWT claim for subject/username
|
||||
} catch (error) {
|
||||
console.error('Error decoding token:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import {environment} from "../../environments/environment";
|
||||
import {LoginCredentials} from "./loginCredentials";
|
||||
import {TokenModel} from "./tokenModel";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService {
|
||||
|
||||
private readonly authUrl: string
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.authUrl = `${environment.backendUrl}`
|
||||
}
|
||||
|
||||
public login(loginCredentials: LoginCredentials) {
|
||||
return this.http.post<any>(`${this.authUrl}/authenticate`, loginCredentials);
|
||||
}
|
||||
|
||||
public logout(tokenModel: TokenModel) {
|
||||
return this.http.post<string>(
|
||||
`${this.authUrl}/logout`,
|
||||
tokenModel
|
||||
);
|
||||
}
|
||||
|
||||
public logoutEverywhere() {
|
||||
return this.http.post<string>(
|
||||
`${this.authUrl}/logout-everywhere`,
|
||||
undefined
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from "@angular/router";
|
||||
import {UserService} from "./user.service";
|
||||
import {delay, map, Observable, of} from "rxjs";
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private userService: UserService
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||||
if (this.userService.getUser()) {
|
||||
return true;
|
||||
}
|
||||
this.router.navigate(['/auth/login'], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
|
||||
import {catchError, Observable, throwError} from "rxjs";
|
||||
import {UserService} from "./user.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ErrorInterceptor implements HttpInterceptor {
|
||||
|
||||
constructor(private userService: UserService) { }
|
||||
|
||||
intercept(
|
||||
request: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
|
||||
return next.handle(request).pipe(catchError(error=>{
|
||||
if((error.status == 401 || error.status == 403) && !this.isLoginPage(request)){
|
||||
this.userService.removeUser();
|
||||
}
|
||||
// const errMsg = error.error.message || error.statusText;
|
||||
return throwError(() => error);
|
||||
// return throwError(()=> errMsg);
|
||||
}));
|
||||
}
|
||||
|
||||
private isLoginPage(request: HttpRequest<any>){
|
||||
return request.url.includes("/authenticate")
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class IpService {
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public getIPAddress() {
|
||||
return this.http.get("http://api.ipify.org/?format=json");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export class LoginCredentials {
|
||||
username: string;
|
||||
password: string;
|
||||
// ipAddress: string;
|
||||
// recaptcha: string;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
|
||||
import {Observable} from "rxjs";
|
||||
import {UserService} from "./user.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TokenInterceptor implements HttpInterceptor {
|
||||
|
||||
constructor(private userService: UserService) { }
|
||||
|
||||
intercept(
|
||||
request: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
if (this.userService.isLoggedIn()) {
|
||||
let newRequest = request.clone({
|
||||
setHeaders: {
|
||||
Authorization: `Bearer ${this.userService.getUser()?.accessToken}`,
|
||||
},
|
||||
});
|
||||
return next.handle(newRequest);
|
||||
}
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
export class TokenModel {
|
||||
constructor(token: string, refreshToken: string, ipAddress: string) {
|
||||
this.token = token;
|
||||
this.refreshToken = refreshToken;
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
token: string;
|
||||
refreshToken: string;
|
||||
ipAddress: string;
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { SsrCookieService } from "ngx-cookie-service-ssr";
|
||||
import { Router } from "@angular/router";
|
||||
import { User } from "./user";
|
||||
import { BehaviorSubject } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
private user?: User;
|
||||
private userEmitter = new BehaviorSubject<string>('');
|
||||
private initialized = false;
|
||||
|
||||
currentUser = this.userEmitter.asObservable();
|
||||
|
||||
constructor(
|
||||
private cookieService: SsrCookieService,
|
||||
private router: Router,
|
||||
) {}
|
||||
|
||||
initializeUser() {
|
||||
if (this.initialized) return;
|
||||
|
||||
try {
|
||||
const userCookie = this.cookieService.get('swissuser');
|
||||
if (userCookie && userCookie.trim() !== '') {
|
||||
this.user = JSON.parse(userCookie);
|
||||
if (this.user?.username) {
|
||||
this.userEmitter.next(this.user.username);
|
||||
} else {
|
||||
this.userEmitter.next('');
|
||||
}
|
||||
} else {
|
||||
this.user = undefined;
|
||||
this.userEmitter.next('');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing user cookie:', error);
|
||||
this.user = undefined;
|
||||
this.userEmitter.next('');
|
||||
}
|
||||
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
public getUser(): User | undefined {
|
||||
this.initializeUser();
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public isLoggedIn(): boolean {
|
||||
this.initializeUser();
|
||||
return !!this.user;
|
||||
}
|
||||
|
||||
public setUser(user: User) {
|
||||
this.cookieService.set('swissuser', JSON.stringify(user));
|
||||
this.user = user;
|
||||
this.userEmitter.next(this.user.username);
|
||||
}
|
||||
|
||||
public removeUser() {
|
||||
this.cookieService.delete('swissuser');
|
||||
this.user = undefined;
|
||||
this.router.navigate(['/tournaments']);
|
||||
this.userEmitter.next('');
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export class User {
|
||||
accessToken: string;
|
||||
username: string;
|
||||
// passwordHash: string;
|
||||
// email: string;
|
||||
// token: string;
|
||||
// refreshToken: string;
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {FormGroup, FormsModule, ReactiveFormsModule} 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";
|
||||
import {MatError, MatFormField, MatLabel} from "@angular/material/form-field";
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {MatCard, MatCardContent} from "@angular/material/card";
|
||||
import {MatFormField, MatLabel} from "@angular/material/form-field";
|
||||
import {MatButton} from "@angular/material/button";
|
||||
import {MatInput} from "@angular/material/input";
|
||||
import {NgIf} from "@angular/common";
|
||||
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";
|
||||
@@ -52,13 +47,11 @@ export class LoginComponent implements OnInit {
|
||||
}
|
||||
|
||||
public login(): void {
|
||||
console.log('Na inloggen door naar' + this.returnUrl);
|
||||
sessionStorage.removeItem("app.token");
|
||||
|
||||
this.authService.login(this.username, this.password)
|
||||
.subscribe({
|
||||
next: (token) => {
|
||||
console.log('Login successful');
|
||||
sessionStorage.setItem("app.token", token);
|
||||
|
||||
const decodedToken = jwtDecode<JwtPayload>(token);
|
||||
@@ -66,13 +59,11 @@ export class LoginComponent implements OnInit {
|
||||
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")
|
||||
}
|
||||
});
|
||||
|
||||
@@ -213,7 +213,6 @@ export class TournamentManageComponent implements OnInit, OnDestroy {
|
||||
minHeight: '250px'
|
||||
}).afterClosed().subscribe(result => {
|
||||
if (result != undefined) {
|
||||
console.log('Start match on court ' + result.court + ' with counter ' + result.counter.name);
|
||||
this.tournamentService.startMatch(this.tournament.id, match.id, result.court, result.counter.playerId).subscribe(data => {
|
||||
this.tournament = data;
|
||||
});
|
||||
|
||||
@@ -67,13 +67,8 @@ export class TournamentPlayersComponent {
|
||||
minHeight: '250px'
|
||||
}).afterClosed().subscribe(result => {
|
||||
if (result != undefined) {
|
||||
console.log('Substitutes selected for ' + player.name + ': ');
|
||||
console.log(result.substitutions);
|
||||
console.log(result.substitutions[0].event + ': ' + result.substitutions[0].substitute);
|
||||
console.log(result.substitutions[1].event + ': ' + result.substitutions[1].substitute);
|
||||
this.tournamentService.playerSubstitute(this.tournament.id, player.playerId, result.substitutions).subscribe(data => {
|
||||
this.tournament = data;
|
||||
console.log(this.tournament);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user