This commit is contained in:
Bastian Wagner
2026-07-15 14:09:28 +02:00
commit 3e5348b7ec
104 changed files with 30367 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
import { InjectionToken } from '@angular/core';
export const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');

View File

@@ -0,0 +1,15 @@
import { HttpErrorResponse } from '@angular/common/http';
export function apiErrorMessage(error: unknown): string {
if (error instanceof HttpErrorResponse) {
const message = error.error?.message;
if (Array.isArray(message)) {
return message.join(' ');
}
if (typeof message === 'string') {
return message;
}
}
return 'Die Anfrage konnte nicht verarbeitet werden.';
}

View File

@@ -0,0 +1,42 @@
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable, signal } from '@angular/core';
import { Router } from '@angular/router';
import { tap } from 'rxjs';
import { API_BASE_URL } from './api-base-url';
interface LoginResponse {
accessToken: string;
user: {
username: string;
};
}
@Injectable({ providedIn: 'root' })
export class AuthService {
readonly username = signal<string | null>(localStorage.getItem('username'));
constructor(
private readonly http: HttpClient,
private readonly router: Router,
@Inject(API_BASE_URL) private readonly apiBaseUrl: string,
) {}
login(username: string, password: string) {
return this.http
.post<LoginResponse>(`${this.apiBaseUrl}/auth/login`, { username, password })
.pipe(
tap((response) => {
localStorage.setItem('accessToken', response.accessToken);
localStorage.setItem('username', response.user.username);
this.username.set(response.user.username);
}),
);
}
logout(): void {
localStorage.removeItem('accessToken');
localStorage.removeItem('username');
this.username.set(null);
void this.router.navigateByUrl('/login');
}
}