import { Injectable, signal } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; import type { AuthUser } from './auth.service'; export interface UserPreferences { language?: string | null; defaultProvider?: string | null; defaultProviders?: string[] | null; theme?: 'light' | 'dark' | 'black' | 'system' | string | null; videoQuality?: string | null; region?: string | null; version?: number | null; } @Injectable({ providedIn: 'root' }) export class UserService { private _preferences = signal(null); constructor(private http: HttpClient) {} preferences() { return this._preferences(); } /** Même règle que les autres services : /proxy/api en dev (proxy Angular), * /api quand on est servi par le backend :4000 (prod/docker). L'URL codée * en dur cassait loadMe/loadPreferences en prod (-> currentUser null). */ private apiBase(): string { try { const port = window?.location?.port || ''; return port && port !== '4000' ? '/proxy/api' : '/api'; } catch { return '/api'; } } loadPreferences(): Observable { return this.http.get(`${this.apiBase()}/user/preferences`, { withCredentials: true }).pipe( tap(p => this._preferences.set(p || {})) ); } updatePreferences(patch: Partial): Observable { return this.http.patch(`${this.apiBase()}/user/preferences`, patch, { withCredentials: true }).pipe( tap(p => this._preferences.set(p || {})) ); } loadMe(): Observable { return this.http.get(`${this.apiBase()}/user/me`, { withCredentials: true }); } }