refactor: simplify user service and update routes

This commit is contained in:
Constantin Simonis 2025-05-07 14:39:10 +02:00
commit 3b95725d88
No known key found for this signature in database
GPG key ID: 3878FF77C24AF4D2
12 changed files with 24 additions and 115 deletions

View file

@ -6,7 +6,7 @@ import { LoginRequest } from '../model/auth/LoginRequest';
import { RegisterRequest } from '../model/auth/RegisterRequest';
import { AuthResponse } from '../model/auth/AuthResponse';
import { User } from '../model/User';
import { environment } from '../../environments/environment';
import { environment } from '@environments/environment';
const TOKEN_KEY = 'auth-token';
const USER_KEY = 'auth-user';
@ -15,8 +15,8 @@ const USER_KEY = 'auth-user';
providedIn: 'root',
})
export class AuthService {
private authUrl = `${environment.apiUrl}/api/auth`;
private userUrl = `${environment.apiUrl}/api/users`;
private authUrl = `${environment.apiUrl}/auth`;
private userUrl = `${environment.apiUrl}/users`;
private currentUserSubject: BehaviorSubject<User | null>;
public currentUser: Observable<User | null>;
@ -30,7 +30,10 @@ export class AuthService {
// Check if token exists and load user data
if (this.getToken()) {
console.log('Token found, loading user data...');
this.loadCurrentUser();
} else {
console.log('No token found, user not logged in.');
}
}

View file

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs';
import { User } from '../model/User';
@ -8,18 +8,11 @@ import { environment } from '@environments/environment';
providedIn: 'root',
})
export class UserService {
private apiUrl = `${environment.apiUrl}/api/users`;
private apiUrl = `${environment.apiUrl}/users`;
private currentUserSubject = new BehaviorSubject<User | null>(null);
private http: HttpClient = inject(HttpClient);
constructor(private http: HttpClient) {}
public getUserById(id: number): Observable<User> {
return this.http.get<User>(`${this.apiUrl}/${id}`);
}
public getUserByUsername(username: string): Observable<User> {
return this.http.get<User>(`${this.apiUrl}/username/${username}`);
}
public getCurrentUser(): Observable<User | null> {
return this.http.get<User | null>('/backend/user').pipe(

View file

@ -1,14 +1,6 @@
import {
ChangeDetectionStrategy,
Component,
inject,
OnDestroy,
OnInit,
signal,
} from '@angular/core';
import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit, signal } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AuthService } from '../../../service/auth.service';
import { UserService } from '@service/user.service';
import { AuthService } from '@service/auth.service';
import { Subscription } from 'rxjs';
import { AnimatedNumberComponent } from '@blackjack/components/animated-number/animated-number.component';
@ -24,7 +16,6 @@ export class NavbarComponent implements OnInit, OnDestroy {
private authService: AuthService = inject(AuthService);
isLoggedIn = this.authService.isLoggedIn();
private userService = inject(UserService);
private userSubscription: Subscription | undefined;
private authSubscription: Subscription | undefined;
public balance = signal(0);

View file

@ -1,5 +1,5 @@
export const environment = {
STRIPE_KEY:
'pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG',
apiUrl: 'http://localhost:8080',
apiUrl: 'http://localhost:4200/backend',
};

View file

@ -1,10 +1,10 @@
{
"/api": {
"/backend": {
"target": "http://localhost:8080/",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": ""
"^/backend": ""
},
"changeOrigin": true
}