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

@@ -8,4 +8,4 @@ const config = useConfig<MinioConfig>();
/**
* 用来放cookie的域名
*/
export const domain = config.domain || 'xiongxiao.me';
export const domain = config.domain || ''; // 请在这里填写你的域名

View File

@@ -34,15 +34,14 @@ app
}
user.setTokenUser(tokenUser);
const orgs = await user.getOrgs();
if (orgs.includes('admin')) {
ctx.body = 'admin';
ctx.nextQuery = ctx.query;
} else {
ctx.throw(403, 'forbidden');
}
} catch (e) {
console.error('auth-admin error', e);
console.error(`auth-admin error`, e);
console.error('tokenUser', tokenUser?.id, tokenUser?.username, tokenUser?.uid);
ctx.throw(500, e.message);
}
})

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