feat: cookie和domain优化

This commit is contained in:
2025-02-27 18:21:47 +08:00
parent bb571631d6
commit 409067f13f
7 changed files with 41 additions and 26 deletions

View File

@@ -2,7 +2,16 @@ import { app } from '@/app.ts';
import { Org } from '@/models/org.ts';
import { User } from '@/models/user.ts';
import { domain } from '@/modules/domain.ts';
/**
* 当配置了domain后创建cookie当get请求地址的时候会自动带上cookie
* @param token
* @param ctx
* @returns
*/
const createCookie = (token: any, ctx: any) => {
if (!domain) {
return;
}
ctx.res.cookie('token', token.token, {
maxAge: token.expireTime,
domain,
@@ -10,6 +19,17 @@ const createCookie = (token: any, ctx: any) => {
httpOnly: true,
});
};
const clearCookie = (ctx: any) => {
if (!domain) {
return;
}
ctx.res.cookie('token', '', {
maxAge: 0,
domain,
sameSite: 'lax',
httpOnly: true,
});
};
app
.route({
path: 'user',
@@ -64,12 +84,7 @@ app
key: 'logout',
})
.define(async (ctx) => {
ctx.res.cookie('token', '', {
maxAge: 0,
domain: `${domain}`,
sameSite: 'lax',
httpOnly: true,
});
clearCookie(ctx);
})
.addTo(app);
app