Fix: frontend API response format - remove ApiResult wrapper
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -31,13 +31,6 @@ export interface UserDepartmentDto {
|
||||
role: string;
|
||||
}
|
||||
|
||||
export interface ApiResult<T> {
|
||||
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<ApiResult<LoginResponse>> {
|
||||
return this.http.post<ApiResult<LoginResponse>>(`${environment.apiUrl}/auth/login`, request).pipe(
|
||||
tap(res => {
|
||||
if (res.success) {
|
||||
this.setSession(res.data);
|
||||
}
|
||||
})
|
||||
login(request: LoginRequest): Observable<LoginResponse> {
|
||||
return this.http.post<LoginResponse>(`${environment.apiUrl}/auth/login`, request).pipe(
|
||||
tap(res => this.setSession(res))
|
||||
);
|
||||
}
|
||||
|
||||
refresh(): Observable<ApiResult<LoginResponse> | null> {
|
||||
refresh(): Observable<LoginResponse | null> {
|
||||
const refreshToken = localStorage.getItem(this.REFRESH_KEY);
|
||||
if (!refreshToken) return of(null);
|
||||
|
||||
return this.http.post<ApiResult<LoginResponse>>(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe(
|
||||
tap(res => {
|
||||
if (res.success) {
|
||||
this.setSession(res.data);
|
||||
}
|
||||
}),
|
||||
return this.http.post<LoginResponse>(`${environment.apiUrl}/auth/refresh`, { refreshToken }).pipe(
|
||||
tap(res => this.setSession(res)),
|
||||
catchError(() => {
|
||||
this.logout();
|
||||
return of(null);
|
||||
|
||||
@@ -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<ApiResult<DepartmentDto[]>> {
|
||||
return this.http.get<ApiResult<DepartmentDto[]>>(`${environment.apiUrl}/departments`);
|
||||
getAll(): Observable<DepartmentDto[]> {
|
||||
return this.http.get<DepartmentDto[]>(`${environment.apiUrl}/departments`);
|
||||
}
|
||||
|
||||
getBySlug(slug: string): Observable<ApiResult<DepartmentDto>> {
|
||||
return this.http.get<ApiResult<DepartmentDto>>(`${environment.apiUrl}/departments/${slug}`);
|
||||
getBySlug(slug: string): Observable<DepartmentDto> {
|
||||
return this.http.get<DepartmentDto>(`${environment.apiUrl}/departments/${slug}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user