pref: 去除开发环境配置

This commit is contained in:
zdl
2025-10-16 15:54:52 +08:00
parent ca51252fce
commit 081eb3c5c3
6 changed files with 42 additions and 57 deletions

View File

@@ -37,7 +37,8 @@ import WechatRegister from './WechatRegister';
// API配置
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : "http://49.232.185.254:5001";
// 使用空字符串让请求通过 webpack proxy避免跨域 cookie 问题
const API_BASE_URL = "";
// 统一配置对象
const AUTH_CONFIG = {
@@ -154,12 +155,12 @@ export default function AuthFormContent() {
try {
setSendingCode(true);
const response = await fetch(`${API_BASE_URL}/api/auth/send-verification-code`, {
const response = await fetch('/api/auth/send-verification-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify({
credential,
type: 'phone',
@@ -243,12 +244,12 @@ export default function AuthFormContent() {
};
// 调用API根据模式选择不同的endpoint
const response = await fetch(`${API_BASE_URL}/api/auth/login-with-code`, {
const response = await fetch('/api/auth/login-with-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
credentials: 'include', // 必须包含以支持跨域 session cookie
body: JSON.stringify(requestBody),
});

View File

@@ -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();
@@ -32,7 +28,7 @@ export const AuthProvider = ({ children }) => {
try {
console.log('🔍 检查Session状态...');
const response = await fetch(`${API_BASE_URL}/api/auth/session`, {
const response = await fetch(`/api/auth/session`, {
method: 'GET',
credentials: 'include', // 重要包含cookie
headers: {
@@ -103,14 +99,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',
@@ -181,7 +177,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',
@@ -232,7 +228,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',
@@ -288,7 +284,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',
@@ -342,11 +338,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 })
});
@@ -384,11 +381,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 })
});
@@ -427,7 +425,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'
});
@@ -476,7 +474,7 @@ export const AuthProvider = ({ children }) => {
}
// 获取用户权限信息
const response = await fetch(`${API_BASE_URL}/api/subscription/permissions`, {
const response = await fetch(`/api/subscription/permissions`, {
method: 'GET',
credentials: 'include',
headers: {