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

@@ -11,6 +11,7 @@ app
key: 'upload',
middleware: ['auth'],
description: 'Upload micro app in server',
isDebug: true,
})
.define(async (ctx) => {
const { files, collection } = ctx.query?.data;

View File

@@ -38,11 +38,11 @@ MicroAppUploadModel.init(
comment: 'id',
},
title: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
description: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
tags: {
@@ -50,11 +50,11 @@ MicroAppUploadModel.init(
defaultValue: [],
},
type: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
source: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
data: {
@@ -66,7 +66,7 @@ MicroAppUploadModel.init(
defaultValue: false,
},
uname: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
uid: {

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