refactor: simplify user service and update routes
This commit is contained in:
parent
51540c930b
commit
3b95725d88
12 changed files with 24 additions and 115 deletions
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in a new issue