From ac3260e9a610d8d72ec09789a576f1e7f486203f Mon Sep 17 00:00:00 2001 From: Joao Date: Sun, 8 Feb 2026 01:41:56 +0000 Subject: [PATCH] Fix: frontend API response format - remove ApiResult wrapper --- .../app/core/interceptors/auth.interceptor.ts | 4 +-- .../src/app/core/services/auth.service.ts | 27 +++++-------------- .../app/core/services/department.service.ts | 9 +++---- .../departments/departments.component.ts | 4 +-- .../src/app/pages/login/login.component.ts | 10 +++---- 5 files changed, 17 insertions(+), 37 deletions(-) diff --git a/frontend/src/app/core/interceptors/auth.interceptor.ts b/frontend/src/app/core/interceptors/auth.interceptor.ts index 85fcc89..44f6024 100644 --- a/frontend/src/app/core/interceptors/auth.interceptor.ts +++ b/frontend/src/app/core/interceptors/auth.interceptor.ts @@ -18,9 +18,9 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => { if (error.status === 401 && !req.url.includes('/auth/login') && !req.url.includes('/auth/refresh')) { return auth.refresh().pipe( switchMap(res => { - if (res?.success) { + if (res?.accessToken) { const newReq = req.clone({ - setHeaders: { Authorization: `Bearer ${res.data.accessToken}` } + setHeaders: { Authorization: `Bearer ${res.accessToken}` } }); return next(newReq); } diff --git a/frontend/src/app/core/services/auth.service.ts b/frontend/src/app/core/services/auth.service.ts index 63bcce4..4401860 100644 --- a/frontend/src/app/core/services/auth.service.ts +++ b/frontend/src/app/core/services/auth.service.ts @@ -31,13 +31,6 @@ export interface UserDepartmentDto { role: string; } -export interface ApiResult { - success: boolean; - data: T; - error: string; - statusCode: number; -} - @Injectable({ providedIn: 'root' }) export class AuthService { private readonly TOKEN_KEY = 'access_token'; @@ -50,26 +43,18 @@ export class AuthService { constructor(private http: HttpClient, private router: Router) {} - login(request: LoginRequest): Observable> { - return this.http.post>(`${environment.apiUrl}/auth/login`, request).pipe( - tap(res => { - if (res.success) { - this.setSession(res.data); - } - }) + login(request: LoginRequest): Observable { + return this.http.post(`${environment.apiUrl}/auth/login`, request).pipe( + tap(res => this.setSession(res)) ); } - refresh(): Observable | null> { + refresh(): Observable { const refreshToken = localStorage.getItem(this.REFRESH_KEY); if (!refreshToken) return of(null); - return this.http.post>(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe( - tap(res => { - if (res.success) { - this.setSession(res.data); - } - }), + return this.http.post(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe( + tap(res => this.setSession(res)), catchError(() => { this.logout(); return of(null); diff --git a/frontend/src/app/core/services/department.service.ts b/frontend/src/app/core/services/department.service.ts index c3ce4f4..d368981 100644 --- a/frontend/src/app/core/services/department.service.ts +++ b/frontend/src/app/core/services/department.service.ts @@ -2,7 +2,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { environment } from '../../../environments/environment'; -import { ApiResult } from './auth.service'; export interface DepartmentDto { id: string; @@ -17,11 +16,11 @@ export interface DepartmentDto { export class DepartmentService { constructor(private http: HttpClient) {} - getAll(): Observable> { - return this.http.get>(`${environment.apiUrl}/departments`); + getAll(): Observable { + return this.http.get(`${environment.apiUrl}/departments`); } - getBySlug(slug: string): Observable> { - return this.http.get>(`${environment.apiUrl}/departments/${slug}`); + getBySlug(slug: string): Observable { + return this.http.get(`${environment.apiUrl}/departments/${slug}`); } } diff --git a/frontend/src/app/pages/departments/departments.component.ts b/frontend/src/app/pages/departments/departments.component.ts index d7412e4..fcdbc5b 100644 --- a/frontend/src/app/pages/departments/departments.component.ts +++ b/frontend/src/app/pages/departments/departments.component.ts @@ -55,8 +55,8 @@ export class DepartmentsComponent implements OnInit { ngOnInit() { this.deptService.getAll().subscribe({ - next: (res) => { - if (res.success) this.departments.set(res.data); + next: (data) => { + this.departments.set(data); this.loading.set(false); }, error: () => this.loading.set(false) diff --git a/frontend/src/app/pages/login/login.component.ts b/frontend/src/app/pages/login/login.component.ts index fddd652..1dbb21f 100644 --- a/frontend/src/app/pages/login/login.component.ts +++ b/frontend/src/app/pages/login/login.component.ts @@ -101,17 +101,13 @@ export class LoginComponent { this.error.set(''); this.auth.login({ email: this.email, password: this.password }).subscribe({ - next: (res) => { + next: () => { this.loading.set(false); - if (res.success) { - this.router.navigate(['/']); - } else { - this.error.set(res.error || 'Credenciais invalidas'); - } + this.router.navigate(['/']); }, error: (err) => { this.loading.set(false); - this.error.set(err.error?.error || 'Erro ao conectar'); + this.error.set(err.error?.error || 'Credenciais invalidas'); } }); }