feat: update Gitea workflow trigger and enhance API error handling with token refresh logic

This commit is contained in:
José Henrique 2025-02-16 13:16:41 -03:00
parent 2487076e92
commit 0be511c245
3 changed files with 36 additions and 9 deletions

View File

@ -1,9 +1,6 @@
name: Main Build & Deploy
on:
push:
branches:
- main
on: workflow_dispatch
jobs:
build:
@ -44,9 +41,6 @@ jobs:
name: Deploy Live
runs-on: ubuntu-latest
needs: build
environment:
name: production
url: https://pet.ivanch.me # adjust this URL as needed
steps:
- name: Recreate container

View File

@ -49,4 +49,8 @@ export class ApiErrorHandler {
static isClientError(error: ApiError): boolean {
return error.status >= 400 && error.status < 500;
}
public static isUnauthorizedError(error: ApiError): boolean {
return error.status === 401;
}
}

View File

@ -3,6 +3,7 @@ import { apiConfig } from './config';
import { setupInterceptors } from './interceptors';
import { ApiErrorHandler } from './error';
import { ApiResponse, RequestOptions, ApiError } from './types';
import { auth } from '../../config/firebase';
class ApiService {
private static instance: ApiService;
@ -17,8 +18,8 @@ class ApiService {
// Add request interceptor
this.axiosInstance.interceptors.request.use(
(config) => {
const token = localStorage.getItem('TOKEN');
async (config) => {
const token = await auth.currentUser?.getIdToken();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
@ -28,6 +29,34 @@ class ApiService {
return Promise.reject(error);
}
);
// Add response interceptor
this.axiosInstance.interceptors.response.use(
(response) => response,
async (error) => {
const originalRequest = error.config;
// Check if error is 401 and we haven't tried to refresh token yet
if (error.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
try {
// Force token refresh
const user = auth.currentUser;
if (user) {
const newToken = await user.getIdToken(true);
originalRequest.headers.Authorization = `Bearer ${newToken}`;
localStorage.setItem('TOKEN', newToken);
return this.axiosInstance(originalRequest);
}
} catch (refreshError) {
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
}
);
}
public static getInstance(): ApiService {