import { appPath } from '@/lib/base-path'; import { assertSameOrigin, AuthError } from '@/lib/server/auth'; import { applicationOrigin } from '@/lib/server/database'; import { beginFeishuAuth, FeishuAuthError } from '@/lib/server/feishu-auth'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; export async function POST(request: Request) { let intent = 'login'; try { assertSameOrigin(request); const raw = await request.text(); if (raw.length > 8192) throw new FeishuAuthError('failed'); const form = new URLSearchParams(raw); intent = form.get('intent') || 'login'; const result = beginFeishuAuth(request, intent, form.get('next')); return new Response(null, { status: 303, headers: { Location: result.url, 'Set-Cookie': result.cookie, 'Cache-Control': 'no-store' } }); } catch (error) { const code = error instanceof FeishuAuthError ? error.code : error instanceof AuthError && error.status === 429 ? 'rate_limited' : error instanceof AuthError && error.status === 403 ? 'source' : 'failed'; return new Response(null, { status: 303, headers: { Location: applicationOrigin() + appPath(`${intent === 'bind' ? '/account/feishu' : '/login'}?feishuError=${code}`), 'Cache-Control': 'no-store' } }); } }