feat: 10.10线上最新代码提交
This commit is contained in:
34
src/contexts/AuthContext.js
Normal file → Executable file
34
src/contexts/AuthContext.js
Normal file → Executable file
@@ -5,7 +5,7 @@ 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 API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL || "http://49.232.185.254:5000";
|
||||
|
||||
// 创建认证上下文
|
||||
const AuthContext = createContext();
|
||||
@@ -81,6 +81,11 @@ export const AuthProvider = ({ children }) => {
|
||||
return () => window.removeEventListener('popstate', handleRouteChange);
|
||||
}, [isAuthenticated]);
|
||||
|
||||
// 更新本地用户的便捷方法
|
||||
const updateUser = (partial) => {
|
||||
setUser((prev) => ({ ...(prev || {}), ...partial }));
|
||||
};
|
||||
|
||||
// 传统登录方法
|
||||
const login = async (credential, password, loginType = 'email') => {
|
||||
try {
|
||||
@@ -98,7 +103,14 @@ export const AuthProvider = ({ children }) => {
|
||||
formData.append('username', credential);
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/login`, {
|
||||
console.log('📤 发送登录请求到:', `${API_BASE_URL}/api/auth/login`);
|
||||
console.log('📝 请求数据:', {
|
||||
credential,
|
||||
loginType,
|
||||
formData: formData.toString()
|
||||
});
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
@@ -107,7 +119,22 @@ export const AuthProvider = ({ children }) => {
|
||||
body: formData
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
console.log('📨 响应状态:', response.status, response.statusText);
|
||||
console.log('📨 响应头:', Object.fromEntries(response.headers.entries()));
|
||||
|
||||
// 获取响应文本,然后尝试解析JSON
|
||||
const responseText = await response.text();
|
||||
console.log('📨 响应原始内容:', responseText);
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(responseText);
|
||||
console.log('✅ JSON解析成功:', data);
|
||||
} catch (parseError) {
|
||||
console.error('❌ JSON解析失败:', parseError);
|
||||
console.error('📄 响应内容:', responseText);
|
||||
throw new Error(`服务器响应格式错误: ${responseText.substring(0, 100)}...`);
|
||||
}
|
||||
|
||||
if (!response.ok || !data.success) {
|
||||
throw new Error(data.error || '登录失败');
|
||||
@@ -444,6 +471,7 @@ export const AuthProvider = ({ children }) => {
|
||||
user,
|
||||
isAuthenticated,
|
||||
isLoading,
|
||||
updateUser,
|
||||
login,
|
||||
register,
|
||||
registerWithPhone,
|
||||
|
||||
0
src/contexts/SidebarContext.js
Normal file → Executable file
0
src/contexts/SidebarContext.js
Normal file → Executable file
Reference in New Issue
Block a user