feat: update Gitea workflow trigger and enhance API error handling with token refresh logic
This commit is contained in:
parent
2487076e92
commit
0be511c245
@ -1,9 +1,6 @@
|
|||||||
name: Main Build & Deploy
|
name: Main Build & Deploy
|
||||||
|
|
||||||
on:
|
on: workflow_dispatch
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@ -44,9 +41,6 @@ jobs:
|
|||||||
name: Deploy Live
|
name: Deploy Live
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: build
|
needs: build
|
||||||
environment:
|
|
||||||
name: production
|
|
||||||
url: https://pet.ivanch.me # adjust this URL as needed
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Recreate container
|
- name: Recreate container
|
||||||
|
@ -49,4 +49,8 @@ export class ApiErrorHandler {
|
|||||||
static isClientError(error: ApiError): boolean {
|
static isClientError(error: ApiError): boolean {
|
||||||
return error.status >= 400 && error.status < 500;
|
return error.status >= 400 && error.status < 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static isUnauthorizedError(error: ApiError): boolean {
|
||||||
|
return error.status === 401;
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,6 +3,7 @@ import { apiConfig } from './config';
|
|||||||
import { setupInterceptors } from './interceptors';
|
import { setupInterceptors } from './interceptors';
|
||||||
import { ApiErrorHandler } from './error';
|
import { ApiErrorHandler } from './error';
|
||||||
import { ApiResponse, RequestOptions, ApiError } from './types';
|
import { ApiResponse, RequestOptions, ApiError } from './types';
|
||||||
|
import { auth } from '../../config/firebase';
|
||||||
|
|
||||||
class ApiService {
|
class ApiService {
|
||||||
private static instance: ApiService;
|
private static instance: ApiService;
|
||||||
@ -17,8 +18,8 @@ class ApiService {
|
|||||||
|
|
||||||
// Add request interceptor
|
// Add request interceptor
|
||||||
this.axiosInstance.interceptors.request.use(
|
this.axiosInstance.interceptors.request.use(
|
||||||
(config) => {
|
async (config) => {
|
||||||
const token = localStorage.getItem('TOKEN');
|
const token = await auth.currentUser?.getIdToken();
|
||||||
if (token) {
|
if (token) {
|
||||||
config.headers.Authorization = `Bearer ${token}`;
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
}
|
}
|
||||||
@ -28,6 +29,34 @@ class ApiService {
|
|||||||
return Promise.reject(error);
|
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 {
|
public static getInstance(): ApiService {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user