pref: 去除开发环境配置
This commit is contained in:
@@ -3,10 +3,6 @@ import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useToast } from '@chakra-ui/react';
|
||||
|
||||
// API基础URL配置
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL || "http://49.232.185.254:5001";
|
||||
|
||||
// 创建认证上下文
|
||||
const AuthContext = createContext();
|
||||
|
||||
@@ -36,7 +32,7 @@ export const AuthProvider = ({ children }) => {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/session`, {
|
||||
const response = await fetch(`/api/auth/session`, {
|
||||
method: 'GET',
|
||||
credentials: 'include', // 重要:包含cookie
|
||||
headers: {
|
||||
@@ -114,14 +110,14 @@ export const AuthProvider = ({ children }) => {
|
||||
formData.append('username', credential);
|
||||
}
|
||||
|
||||
console.log('📤 发送登录请求到:', `${API_BASE_URL}/api/auth/login`);
|
||||
console.log('📤 发送登录请求到:', `/api/auth/login`);
|
||||
console.log('📝 请求数据:', {
|
||||
credential,
|
||||
loginType,
|
||||
formData: formData.toString()
|
||||
});
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
|
||||
const response = await fetch(`/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
@@ -194,7 +190,7 @@ export const AuthProvider = ({ children }) => {
|
||||
formData.append('email', email);
|
||||
formData.append('password', password);
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/register`, {
|
||||
const response = await fetch(`/api/auth/register`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
@@ -245,7 +241,7 @@ export const AuthProvider = ({ children }) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/register/phone`, {
|
||||
const response = await fetch(`/api/auth/register/phone`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -301,7 +297,7 @@ export const AuthProvider = ({ children }) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/register/email`, {
|
||||
const response = await fetch(`/api/auth/register/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -355,11 +351,12 @@ export const AuthProvider = ({ children }) => {
|
||||
// 发送手机验证码
|
||||
const sendSmsCode = async (phone) => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/send-sms-code`, {
|
||||
const response = await fetch(`/api/auth/send-sms-code`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'include', // 必须包含以支持跨域 session cookie
|
||||
body: JSON.stringify({ phone })
|
||||
});
|
||||
|
||||
@@ -397,11 +394,12 @@ export const AuthProvider = ({ children }) => {
|
||||
// 发送邮箱验证码
|
||||
const sendEmailCode = async (email) => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/send-email-code`, {
|
||||
const response = await fetch(`/api/auth/send-email-code`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'include', // 必须包含以支持跨域 session cookie
|
||||
body: JSON.stringify({ email })
|
||||
});
|
||||
|
||||
@@ -440,7 +438,7 @@ export const AuthProvider = ({ children }) => {
|
||||
const logout = async () => {
|
||||
try {
|
||||
// 调用后端登出API
|
||||
await fetch(`${API_BASE_URL}/api/auth/logout`, {
|
||||
await fetch(`/api/auth/logout`, {
|
||||
method: 'POST',
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user