Fix: frontend API response format - remove ApiResult wrapper

This commit is contained in:
Joao
2026-02-08 01:41:56 +00:00
parent 1372f12952
commit ac3260e9a6
5 changed files with 17 additions and 37 deletions

View File

@@ -18,9 +18,9 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => {
if (error.status === 401 && !req.url.includes('/auth/login') && !req.url.includes('/auth/refresh')) { if (error.status === 401 && !req.url.includes('/auth/login') && !req.url.includes('/auth/refresh')) {
return auth.refresh().pipe( return auth.refresh().pipe(
switchMap(res => { switchMap(res => {
if (res?.success) { if (res?.accessToken) {
const newReq = req.clone({ const newReq = req.clone({
setHeaders: { Authorization: `Bearer ${res.data.accessToken}` } setHeaders: { Authorization: `Bearer ${res.accessToken}` }
}); });
return next(newReq); return next(newReq);
} }

View File

@@ -31,13 +31,6 @@ export interface UserDepartmentDto {
role: string; role: string;
} }
export interface ApiResult<T> {
success: boolean;
data: T;
error: string;
statusCode: number;
}
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class AuthService { export class AuthService {
private readonly TOKEN_KEY = 'access_token'; private readonly TOKEN_KEY = 'access_token';
@@ -50,26 +43,18 @@ export class AuthService {
constructor(private http: HttpClient, private router: Router) {} constructor(private http: HttpClient, private router: Router) {}
login(request: LoginRequest): Observable<ApiResult<LoginResponse>> { login(request: LoginRequest): Observable<LoginResponse> {
return this.http.post<ApiResult<LoginResponse>>(`${environment.apiUrl}/auth/login`, request).pipe( return this.http.post<LoginResponse>(`${environment.apiUrl}/auth/login`, request).pipe(
tap(res => { tap(res => this.setSession(res))
if (res.success) {
this.setSession(res.data);
}
})
); );
} }
refresh(): Observable<ApiResult<LoginResponse> | null> { refresh(): Observable<LoginResponse | null> {
const refreshToken = localStorage.getItem(this.REFRESH_KEY); const refreshToken = localStorage.getItem(this.REFRESH_KEY);
if (!refreshToken) return of(null); if (!refreshToken) return of(null);
return this.http.post<ApiResult<LoginResponse>>(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe( return this.http.post<LoginResponse>(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe(
tap(res => { tap(res => this.setSession(res)),
if (res.success) {
this.setSession(res.data);
}
}),
catchError(() => { catchError(() => {
this.logout(); this.logout();
return of(null); return of(null);

View File

@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment'; import { environment } from '../../../environments/environment';
import { ApiResult } from './auth.service';
export interface DepartmentDto { export interface DepartmentDto {
id: string; id: string;
@@ -17,11 +16,11 @@ export interface DepartmentDto {
export class DepartmentService { export class DepartmentService {
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {}
getAll(): Observable<ApiResult<DepartmentDto[]>> { getAll(): Observable<DepartmentDto[]> {
return this.http.get<ApiResult<DepartmentDto[]>>(`${environment.apiUrl}/departments`); return this.http.get<DepartmentDto[]>(`${environment.apiUrl}/departments`);
} }
getBySlug(slug: string): Observable<ApiResult<DepartmentDto>> { getBySlug(slug: string): Observable<DepartmentDto> {
return this.http.get<ApiResult<DepartmentDto>>(`${environment.apiUrl}/departments/${slug}`); return this.http.get<DepartmentDto>(`${environment.apiUrl}/departments/${slug}`);
} }
} }

View File

@@ -55,8 +55,8 @@ export class DepartmentsComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.deptService.getAll().subscribe({ this.deptService.getAll().subscribe({
next: (res) => { next: (data) => {
if (res.success) this.departments.set(res.data); this.departments.set(data);
this.loading.set(false); this.loading.set(false);
}, },
error: () => this.loading.set(false) error: () => this.loading.set(false)

View File

@@ -101,17 +101,13 @@ export class LoginComponent {
this.error.set(''); this.error.set('');
this.auth.login({ email: this.email, password: this.password }).subscribe({ this.auth.login({ email: this.email, password: this.password }).subscribe({
next: (res) => { next: () => {
this.loading.set(false); this.loading.set(false);
if (res.success) { this.router.navigate(['/']);
this.router.navigate(['/']);
} else {
this.error.set(res.error || 'Credenciais invalidas');
}
}, },
error: (err) => { error: (err) => {
this.loading.set(false); this.loading.set(false);
this.error.set(err.error?.error || 'Erro ao conectar'); this.error.set(err.error?.error || 'Credenciais invalidas');
} }
}); });
} }