2026-05-28 15:11:36 +08:00

40 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server';
import { getLocalLicenseReadOnly, isLicenseConfigExpired } from '@/lib/license/license-service';
/**
* 检查数据库中是否有已激活的许可证
* 用于恢复 cookie(当 cookie 丢失但数据库中仍有许可证时)
*/
export async function GET(request: NextRequest) {
try {
const license = await getLocalLicenseReadOnly();
if (!license || isLicenseConfigExpired(license.cachedConfig)) {
return NextResponse.json({ activated: false });
}
const response = NextResponse.json({ activated: true });
// 自动检测协议:根据请求的协议决定 cookie 的 secure 属性
const isHttps = request.headers.get('x-forwarded-proto') === 'https'
|| request.url.startsWith('https://');
// 恢复 cookie
response.cookies.set('license_activated', 'true', {
httpOnly: true,
secure: isHttps,
sameSite: 'lax',
maxAge: 365 * 24 * 60 * 60, // 1年
path: '/',
});
return response;
} catch (error) {
console.error('检查许可证状态失败:', error);
return NextResponse.json(
{ activated: false, error: '服务器错误' },
{ status: 500 }
);
}
}