web only use supabase auth
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
||||
import supabase from './supabase';
|
||||
|
||||
// Type definitions
|
||||
interface LoginCredentials {
|
||||
@@ -27,10 +28,13 @@ const apiClient: AxiosInstance = axios.create({
|
||||
|
||||
// Request interceptor for adding auth token
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
async (config) => {
|
||||
// 从 Supabase 获取当前会话
|
||||
const { data } = await supabase.auth.getSession();
|
||||
const session = data.session;
|
||||
|
||||
if (session) {
|
||||
config.headers.Authorization = `Bearer ${session.access_token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
@@ -44,18 +48,32 @@ apiClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
async (error) => {
|
||||
// Handle errors globally
|
||||
if (error.response) {
|
||||
// Server responded with error status (4xx, 5xx)
|
||||
if (error.response.status === 401) {
|
||||
// Unauthorized - clear local storage
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('user');
|
||||
|
||||
// Redirect to login page if not already there
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
// Unauthorized - 可能是 token 过期,尝试刷新
|
||||
try {
|
||||
const { data, error: refreshError } = await supabase.auth.refreshSession();
|
||||
|
||||
if (refreshError || !data.session) {
|
||||
// 刷新失败,重定向到登录页面
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
} else {
|
||||
// 刷新成功,重试请求
|
||||
const originalRequest = error.config;
|
||||
originalRequest.headers.Authorization = `Bearer ${data.session.access_token}`;
|
||||
return axios(originalRequest);
|
||||
}
|
||||
} catch (refreshError) {
|
||||
console.error('Failed to refresh token:', refreshError);
|
||||
// 重定向到登录页面
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,16 +81,23 @@ apiClient.interceptors.response.use(
|
||||
}
|
||||
);
|
||||
|
||||
// Auth API
|
||||
// Auth API - 不再需要大部分方法,因为现在直接使用 Supabase
|
||||
export const authApi = {
|
||||
login: (credentials: LoginCredentials): Promise<AxiosResponse<LoginResponse>> =>
|
||||
apiClient.post('/api/auth/login', credentials),
|
||||
verify: (headers?: Record<string, string>): Promise<AxiosResponse> =>
|
||||
apiClient.get('/api/auth/verify', headers ? { headers } : undefined),
|
||||
register: (data: { email: string; password: string; name: string }): Promise<AxiosResponse> =>
|
||||
apiClient.post('/api/auth/register', data),
|
||||
refreshToken: (): Promise<AxiosResponse<{token: string}>> =>
|
||||
apiClient.post('/api/auth/refresh-token'),
|
||||
// 保留 verify 方法用于与后端验证
|
||||
verify: async (): Promise<AxiosResponse> => {
|
||||
const { data } = await supabase.auth.getSession();
|
||||
const session = data.session;
|
||||
|
||||
if (!session) {
|
||||
throw new Error('No active session');
|
||||
}
|
||||
|
||||
return apiClient.get('/api/auth/verify', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.access_token}`
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Comments API
|
||||
|
||||
Reference in New Issue
Block a user