mirror of
https://git.zhongyuan.online/ytke/tap.git
synced 2026-11-24 22:15:18 -08:00
23 lines
1.2 KiB
TypeScript
23 lines
1.2 KiB
TypeScript
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' } });
|
|
}
|
|
}
|