Skip to content

Commit

Permalink
fix: 支持设置auth cookies sameSite #6949
Browse files Browse the repository at this point in the history
  • Loading branch information
baozhoutao committed Aug 2, 2024
1 parent 09cf828 commit dde1c37
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/utils/src/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Cookies = require('cookies');
const psl = require('psl');

const useSubdomainCookies = process.env.STEEDOS_AUTH_USE_SUBDOMAIN_COOKIES === 'true';
const useSubdomainCookies = process.env.STEEDOS_AUTH_COOKIES_USE_SUBDOMAIN === 'true';
const sameSite = process.env.STEEDOS_AUTH_COOKIES_USE_SAMESITE || null;

// 从请求的 Host 头中提取二级域名部分
function getSubdomain(host) {
Expand All @@ -19,6 +20,11 @@ export function setCookie(req, res, name, value, options = {domain: null, maxAge
const domain = getSubdomain(host);
options.domain = `.${domain}`; // 动态设置二级域名
}

if(sameSite){
(options as any).sameSite = sameSite;
}

cookies.set(name, value, options);
}

Expand All @@ -29,6 +35,11 @@ export function clearCookie(req, res, name, options = {domain: null, maxAge: 0,
const domain = getSubdomain(host);
options.domain = `.${domain}`; // 动态设置二级域名
}

if(sameSite){
(options as any).sameSite = sameSite;
}

options.maxAge = 0; // 通过将 maxAge 设置为 0 来清除 cookie
cookies.set(name, null, options);
}

0 comments on commit dde1c37

Please sign in to comment.