"use client"; import { useState } from "react"; import Image from "next/image"; import { useRouter } from "next/navigation"; import { handleAuthorizeAction } from "@/actions/authorizing"; import { Client } from "@prisma/client"; import { ChevronsDownUp, ChevronsUpDown, GithubIcon, Users, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; interface Permission { id: string; name: string; description: string; } const permissions: Permission[] = [ { id: "read_profile", name: "个人用户数据", description: "电子邮件地址(只读), 个人资料信息(只读)", }, ]; export function AuthorizationCard({ client, oauthParams, }: { client: Client; oauthParams: string; }) { const [expandedPermission, setExpandedPermission] = useState( null, ); const [isAuthorizing, setIsAuthorizing] = useState(false); const [error, setError] = useState(null); const router = useRouter(); const togglePermission = (id: string) => { setExpandedPermission(expandedPermission === id ? null : id); }; const authorizingHandler = async () => { try { setIsAuthorizing(true); setError(null); const result = await handleAuthorizeAction( oauthParams, client.id, permissions[0].id, ); if (result.error) { setError(result.error); setIsAuthorizing(false); return; } if (result.redirectUrl) { const url = await Promise.resolve(result.redirectUrl); router.push(url); } } catch (error) { setError("授权处理失败,请稍后重试"); setIsAuthorizing(false); } }; const handleCancel = () => { // 从 URL 中获取 redirect_uri 参数 const params = new URLSearchParams(atob(oauthParams)); const redirectUri = params.get("redirect_uri"); if (redirectUri) { // 添加错误参数返回 const redirectUrl = new URL(redirectUri); redirectUrl.searchParams.set("error", "access_denied"); redirectUrl.searchParams.set("error_description", "用户取消了授权请求"); router.push(redirectUrl.toString()); } else { // 如果没有 redirect_uri,返回首页 router.push("/"); } }; return (
{client.logo ? ( {client.name} ) : ( {client.name[0].toUpperCase()} )}
授权 {client.name}

该应用程序请求访问您的Q58论坛账号

{error && (

错误: {error}

)}

请求的权限

{permissions.map((permission) => (
togglePermission(permission.id)} >
{expandedPermission === permission.id ? ( ) : ( )}
{expandedPermission === permission.id && (
{permission.description}
)}
))}

授权将重定向到 {client.redirectUri}

Q58论坛运营
); }