"use client"; import { client } from "@/lib/auth-client"; import { Button } from "@nice/ui/components/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@nice/ui/components/card"; import { Input } from "@nice/ui/components/input"; import { Label } from "@nice/ui/components/label"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@nice/ui/components/tabs"; import { toast } from "@nice/ui/components/sonner"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; export default function SignInPage() { const [loginCredentials, setLoginCredentials] = useState({ email: "", username: "", password: "" }); const [registerCredentials, setRegisterCredentials] = useState({ email: "", username: "", password: "", confirmPassword: "", name: "" }); const [isLoading, setIsLoading] = useState(false); const [currentTab, setCurrentTab] = useState("login"); const router = useRouter(); const searchParams = useSearchParams(); // 检查是否是从 OIDC Provider 重定向过来的 const isOIDCFlow = searchParams.get('response_type') === 'code' || searchParams.get('client_id'); // 登录处理 const handleSignIn = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const result = await client.signIn.username({ username: loginCredentials.username || loginCredentials.email, password: loginCredentials.password, callbackURL: 'http://localhost:3000/dashboard' }); if (result.data) { toast.success("登录成功!"); // 如果是 OIDC 流程,重定向回原始请求 if (isOIDCFlow) { // 获取所有查询参数并重建 URL const params = new URLSearchParams(window.location.search); const redirectUrl = `http://localhost:3001/api/auth/oauth2/authorize?${params.toString()}`; window.location.href = redirectUrl; } else { router.push('/dashboard'); } } else if (result.error) { toast.error(`登录失败: ${result.error.message}`); } } catch (error: any) { console.error("登录失败:", error); toast.error(`登录失败: ${error.message || "未知错误"}`); } finally { setIsLoading(false); } }; // 注册处理 const handleSignUp = async (e: React.FormEvent) => { e.preventDefault(); if (registerCredentials.password !== registerCredentials.confirmPassword) { toast.error("密码不匹配"); return; } setIsLoading(true); try { const result = await client.signUp.email({ email: registerCredentials.email, password: registerCredentials.password, name: registerCredentials.name, username: registerCredentials.username, callbackURL: 'http://localhost:3000/dashboard' }); if (result.data) { toast.success("注册成功!"); // 如果是 OIDC 流程,重定向回原始请求 if (isOIDCFlow) { const params = new URLSearchParams(window.location.search); const redirectUrl = `http://localhost:3001/api/auth/oauth2/authorize?${params.toString()}`; window.location.href = redirectUrl; } else { router.push('/dashboard'); } } else if (result.error) { toast.error(`注册失败: ${result.error.message}`); } } catch (error: any) { console.error("注册失败:", error); toast.error(`注册失败: ${error.message || "未知错误"}`); } finally { setIsLoading(false); } }; return (
检测到 OIDC 授权流程。登录或注册后将继续完成授权。