Skip to content

一分钟搭建影视站,支持Docker等部署方式

fix(auth): Cookie Secure 按请求协议推导,修复局域网 HTTP 部署登录态丢失

bestZwei1576c0c2a244
diff --git a/.env.example b/.env.example
index 1c4f8cf..ea921e1 100644
--- a/.env.example
+++ b/.env.example
@@ -4,6 +4,9 @@ PASSWORD=change-me
# 可选:会话/代理签名密钥;不设置时从 PASSWORD 派生
# PROXY_SECRET=
+# 可选:Cookie Secure 策略覆盖(true=强制开启 / false=强制关闭;不设置时按请求协议自动推导,HTTP 部署无需配置)
+# COOKIE_SECURE=
+
# 可选:代理请求上游的超时与重试
REQUEST_TIMEOUT=8000
MAX_RETRIES=1
diff --git a/docker-compose.yml b/docker-compose.yml
index 7b4aa39..884b13d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -16,6 +16,8 @@ services:
- PASSWORD=${PASSWORD:?请在 .env 中设置 PASSWORD}
# 可选:会话/代理签名密钥(多实例部署建议显式设置:openssl rand -hex 32)
# - PROXY_SECRET=your-secret
+ # 可选:Cookie Secure 策略覆盖(true=强制开启 / false=强制关闭;不设置时按请求协议自动推导,HTTP 部署无需配置)
+ # - COOKIE_SECURE=true
# 可选:预置采集站(JSON 数组),用户端自动出现且默认勾选
# - DEFAULT_SOURCES=[{"name":"示例源","url":"https://example.com/api.php/provide/vod"}]
# 可选:预置直播源(M3U 订阅,JSON 数组),epg 为可选节目单地址
diff --git a/src/app/api/auth/route.ts b/src/app/api/auth/route.ts
index 91c0120..1282a16 100644
--- a/src/app/api/auth/route.ts
+++ b/src/app/api/auth/route.ts
@@ -34,10 +34,17 @@ export async function POST(req: Request) {
clearRateLimit(ip);
const { token, expiresAt } = signSession();
const res = NextResponse.json({ success: true });
+ // Cookie Secure 策略:COOKIE_SECURE 环境变量显式覆盖;否则按 x-forwarded-proto 推导。
+ // 不能依赖 req.url——Next.js Route Handler 中它是内部转发地址,并非用户侧的原始协议。
+ const secure = process.env.COOKIE_SECURE === 'true'
+ ? true
+ : process.env.COOKIE_SECURE === 'false'
+ ? false
+ : (req.headers.get('x-forwarded-proto')?.split(',')[0]?.trim() ?? 'http') === 'https';
res.cookies.set(SESSION_COOKIE, token, {
httpOnly: true,
sameSite: 'lax',
- secure: process.env.NODE_ENV === 'production',
+ secure,
maxAge: Math.floor((expiresAt - Date.now()) / 1000),
path: '/',
});