Remove .env.local file and update middleware, page, and auth callback files to enhance logging and user experience. Translate comments and console logs to English for better clarity. Refactor Create Short URL form and Debug page for improved user feedback and error handling.

This commit is contained in:
2025-04-23 18:17:41 +08:00
parent ebb1e77ecc
commit bee8369a6b
9 changed files with 196 additions and 192 deletions

View File

@@ -5,10 +5,10 @@ import { useRouter } from 'next/navigation';
import { Session, User } from '@supabase/supabase-js';
import supabase from './supabase';
// 定义用户类型
// Define user type
export type AuthUser = User | null;
// 定义验证上下文类型
// Define auth context type
export type AuthContextType = {
user: AuthUser;
session: Session | null;
@@ -20,22 +20,22 @@ export type AuthContextType = {
signOut: () => Promise<void>;
};
// 创建验证上下文
// Create auth context
const AuthContext = createContext<AuthContextType | undefined>(undefined);
// 验证提供者组件
// Auth provider component
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [user, setUser] = useState<AuthUser>(null);
const [session, setSession] = useState<Session | null>(null);
const [isLoading, setIsLoading] = useState(true);
const router = useRouter();
// 初始化验证状态
// Initialize auth state
useEffect(() => {
const getSession = async () => {
setIsLoading(true);
try {
// 尝试从Supabase获取会话
// Try to get session from Supabase
const { data: { session }, error } = await supabase.auth.getSession();
if (error) {
@@ -43,7 +43,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
return;
}
// 打印会话信息,帮助调试
// Print session info for debugging
console.log('Supabase session loaded:', session ? 'Found' : 'Not found');
if (session) {
console.log('User authenticated:', session.user.email);
@@ -63,7 +63,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
getSession();
// 监听验证状态变化
// Listen for auth state changes
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
console.log('Auth state changed, event:', _event);
console.log('New session:', session ? 'Valid' : 'None');
@@ -71,56 +71,56 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
setUser(session?.user || null);
});
// 清理函数
// Cleanup function
return () => {
subscription.unsubscribe();
};
}, []);
// 登录函数
// Sign in function
const signIn = async (email: string, password: string) => {
setIsLoading(true);
try {
console.log('尝试登录:', { email });
console.log('Attempting to sign in:', { email });
// 尝试通过Supabase登录
// Try to sign in with Supabase
const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
});
if (error) {
console.error('登录出错:', error);
console.error('Sign in error:', error);
return { error };
}
// 登录成功,设置会话和用户信息
console.log('登录成功,用户信息:', data.user?.email);
// Sign in successful, set session and user info
console.log('Sign in successful, user:', data.user?.email);
setSession(data.session);
setUser(data.user);
// 使用硬重定向代替router.push确保页面完全刷新
console.log('准备重定向到分析页面');
// Use hard redirect instead of router.push to ensure full page refresh
console.log('Preparing to redirect to analytics page');
// 添加短暂延时确保状态已更新,然后重定向
// Add short delay to ensure state is updated, then redirect
setTimeout(() => {
window.location.href = '/analytics';
}, 100);
return {};
} catch (error) {
console.error('登录过程出错:', error);
console.error('Error during sign in process:', error);
return { error };
} finally {
setIsLoading(false);
}
};
// Google登录函数
// Google sign in function
const signInWithGoogle = async () => {
setIsLoading(true);
try {
// 尝试通过Supabase登录Google
// Try to sign in with Google via Supabase
const { error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
@@ -133,24 +133,24 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
});
if (error) {
console.error('Google登录出错:', error);
console.error('Google sign in error:', error);
return { error };
}
return {}; // Return empty object when successful
} catch (error) {
console.error('Google登录过程出错:', error);
console.error('Error during Google sign in process:', error);
return { error };
} finally {
setIsLoading(false);
}
};
// GitHub登录函数
// GitHub sign in function
const signInWithGitHub = async () => {
setIsLoading(true);
try {
// 尝试通过Supabase登录GitHub
// Try to sign in with GitHub via Supabase
const { error } = await supabase.auth.signInWithOAuth({
provider: 'github',
options: {
@@ -172,11 +172,11 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
}
};
// 注册函数
// Sign up function
const signUp = async (email: string, password: string) => {
setIsLoading(true);
try {
// 尝试通过Supabase注册
// Try to sign up via Supabase
const { error } = await supabase.auth.signUp({
email,
password,
@@ -186,35 +186,35 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
});
if (error) {
console.error('注册出错:', error);
console.error('Sign up error:', error);
throw error;
}
// 注册成功后跳转到登录页面并显示确认消息
// After successful registration, redirect to login page with confirmation message
router.push('/login?message=Registration successful! Please check your email to verify your account before logging in.');
} catch (error) {
console.error('注册过程出错:', error);
console.error('Error during sign up process:', error);
throw error;
} finally {
setIsLoading(false);
}
};
// 登出函数
// Sign out function
const signOut = async () => {
setIsLoading(true);
try {
// 尝试通过Supabase登出
// Try to sign out via Supabase
const { error } = await supabase.auth.signOut();
if (error) {
console.error('登出出错:', error);
console.error('Sign out error:', error);
throw error;
}
setSession(null);
setUser(null);
router.push('/login');
} catch (error) {
console.error('登出过程出错:', error);
console.error('Error during sign out process:', error);
throw error;
} finally {
setIsLoading(false);
@@ -239,7 +239,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
);
};
// 自定义钩子
// Custom hook
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
@@ -248,7 +248,7 @@ export const useAuth = () => {
return context;
};
// 受保护路由组件
// Protected route component
export const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const { user, isLoading } = useAuth();
const router = useRouter();
@@ -264,7 +264,7 @@ export const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({ childr
<div className="flex items-center justify-center min-h-screen">
<div className="text-center">
<div className="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500 mx-auto"></div>
<p className="mt-4 text-lg text-gray-700 dark:text-gray-300">...</p>
<p className="mt-4 text-lg text-gray-700 dark:text-gray-300">Loading...</p>
</div>
</div>
);