Enhance login page to display messages from URL parameters and update email redirect options for sign-up confirmation.

This commit is contained in:
2025-04-18 21:50:06 +08:00
parent 3af015ca44
commit 9cb85a2910
2 changed files with 22 additions and 5 deletions

View File

@@ -1,12 +1,13 @@
'use client'; 'use client';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter, useSearchParams } from 'next/navigation';
import Link from 'next/link'; import Link from 'next/link';
import { useAuth } from '@/lib/auth'; import { useAuth } from '@/lib/auth';
export default function LoginPage() { export default function LoginPage() {
const router = useRouter(); const router = useRouter();
const searchParams = useSearchParams();
const { signIn, signInWithGitHub, signInWithGoogle, user } = useAuth(); const { signIn, signInWithGitHub, signInWithGoogle, user } = useAuth();
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
@@ -14,6 +15,14 @@ export default function LoginPage() {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [message, setMessage] = useState({ type: '', content: '' }); const [message, setMessage] = useState({ type: '', content: '' });
// 读取URL中的消息参数
useEffect(() => {
const messageParam = searchParams.get('message');
if (messageParam) {
setMessage({ type: 'info', content: messageParam });
}
}, [searchParams]);
// 如果用户已登录,重定向到首页 // 如果用户已登录,重定向到首页
useEffect(() => { useEffect(() => {
if (user) { if (user) {
@@ -112,11 +121,16 @@ export default function LoginPage() {
{/* Message display */} {/* Message display */}
{message.content && ( {message.content && (
<div className={`p-4 mb-4 text-sm ${ <div className={`p-4 mb-4 text-sm rounded-lg ${
message.type === 'error' message.type === 'error'
? 'text-red-700 bg-red-100 rounded-lg' ? 'text-red-700 bg-red-100 border border-red-200'
: 'text-blue-700 bg-blue-100 rounded-lg' : 'text-blue-700 bg-blue-50 border border-blue-200'
}`}> }`}>
{message.type === 'error' ? (
<span className="font-medium">Error: </span>
) : (
<span className="font-medium">Notice: </span>
)}
{message.content} {message.content}
</div> </div>
)} )}

View File

@@ -160,6 +160,9 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const { error } = await supabase.auth.signUp({ const { error } = await supabase.auth.signUp({
email, email,
password, password,
options: {
emailRedirectTo: `${window.location.origin}/auth/callback`,
}
}); });
if (error) { if (error) {
@@ -168,7 +171,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
} }
// 注册成功后跳转到登录页面并显示确认消息 // 注册成功后跳转到登录页面并显示确认消息
router.push('/login?message=注册成功,请查看邮箱确认账户'); router.push('/login?message=Registration successful! Please check your email to verify your account before logging in.');
} catch (error) { } catch (error) {
console.error('注册过程出错:', error); console.error('注册过程出错:', error);
throw error; throw error;