增强钉钉直达登录Cookie落地

This commit is contained in:
chenpei 2026-07-06 14:39:53 +08:00
parent 447cd4b26e
commit 06b9678fe1

View File

@ -1,5 +1,5 @@
import { NextResponse, type NextRequest } from 'next/server'
import { parseDingtalkOAuthState } from '@/lib/dingtalk-oauth'
import { DINGTALK_DIRECT_LOGIN_SOURCE, parseDingtalkOAuthState } from '@/lib/dingtalk-oauth'
import { getPublicAppOrigin } from '@/lib/public-app-url'
import { buildDingtalkAutoLoginPath } from '@/lib/safe-redirect'
@ -32,9 +32,48 @@ function getInternalLoginUrl() {
return `http://127.0.0.1:${port}/api/dingtalk/login`
}
function createCookieLandingResponse(redirectPath: string, loginResponse: Response) {
const escapedPath = JSON.stringify(redirectPath).replace(/</g, '\\u003c')
const response = new NextResponse(
`<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>正在进入素材库</title>
<meta http-equiv="Cache-Control" content="no-store" />
<style>
body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#f8fafc;color:#0f172a;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}
main{padding:28px;text-align:center}
p{margin:8px 0 0;color:#64748b}
</style>
</head>
<body>
<main>
<h1>登录成功</h1>
<p>正在进入考核调研...</p>
</main>
<script>
window.location.replace(${escapedPath});
</script>
</body>
</html>`,
{
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
},
},
)
appendLoginSetCookie(response, loginResponse)
return response
}
export async function GET(request: NextRequest) {
const code = request.nextUrl.searchParams.get('code') || request.nextUrl.searchParams.get('authCode')
const { redirectPath } = parseDingtalkOAuthState(request.nextUrl.searchParams.get('state'))
const { redirectPath, source } = parseDingtalkOAuthState(request.nextUrl.searchParams.get('state'))
const publicOrigin = getPublicAppOrigin()
if (!code) {
@ -62,6 +101,10 @@ export async function GET(request: NextRequest) {
return NextResponse.redirect(url)
}
if (source === DINGTALK_DIRECT_LOGIN_SOURCE) {
return createCookieLandingResponse(redirectPath, loginResponse)
}
const redirectResponse = NextResponse.redirect(new URL(redirectPath, publicOrigin))
appendLoginSetCookie(redirectResponse, loginResponse)
redirectResponse.headers.set('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')