feat: cookie和domain优化

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

View File

@ -45,7 +45,7 @@
"node-fetch": "^3.3.2",
"p-queue": "^8.1.0",
"pg": "^8.13.3",
"rollup-plugin-esbuild": "^6.2.0",
"rollup-plugin-esbuild": "^6.2.1",
"semver": "^7.7.1",
"sequelize": "^6.37.5",
"socket.io": "^4.8.1",

20
pnpm-lock.yaml generated
View File

@ -67,9 +67,12 @@ importers:
pg:
specifier: ^8.13.3
version: 8.13.3
pm2:
specifier: ^5.4.3
version: 5.4.3
rollup-plugin-esbuild:
specifier: ^6.2.0
version: 6.2.0(esbuild@0.25.0)(rollup@4.34.8)
specifier: ^6.2.1
version: 6.2.1(esbuild@0.25.0)(rollup@4.34.8)
semver:
specifier: ^7.7.1
version: 7.7.1
@ -146,9 +149,6 @@ importers:
nodemon:
specifier: ^3.1.9
version: 3.1.9
pm2:
specifier: ^5.4.3
version: 5.4.3
rimraf:
specifier: latest
version: 6.0.1
@ -1972,8 +1972,8 @@ packages:
rollup: ^3.29.4 || ^4
typescript: ^4.5 || ^5.0
rollup-plugin-esbuild@6.2.0:
resolution: {integrity: sha512-LbkHaCahA6ceyWzAd6md2yajNS+HfZmZ5o58ShkZp0cQeZOnZECG2D2xWFNBq5SF6X6pfMK2udkZ+wRtvpzyVQ==}
rollup-plugin-esbuild@6.2.1:
resolution: {integrity: sha512-jTNOMGoMRhs0JuueJrJqbW8tOwxumaWYq+V5i+PD+8ecSCVkuX27tGW7BXqDgoULQ55rO7IdNxPcnsWtshz3AA==}
engines: {node: '>=14.18.0'}
peerDependencies:
esbuild: '>=0.18.0'
@ -4224,7 +4224,7 @@ snapshots:
proxy-agent@6.3.1:
dependencies:
agent-base: 7.1.1
debug: 4.3.7(supports-color@5.5.0)
debug: 4.4.0
http-proxy-agent: 7.0.2
https-proxy-agent: 7.0.5
lru-cache: 7.18.3
@ -4302,7 +4302,7 @@ snapshots:
require-in-the-middle@5.2.0:
dependencies:
debug: 4.3.7(supports-color@5.5.0)
debug: 4.4.0
module-details-from-path: 1.0.3
resolve: 1.22.8
transitivePeerDependencies:
@ -4347,7 +4347,7 @@ snapshots:
optionalDependencies:
'@babel/code-frame': 7.26.2
rollup-plugin-esbuild@6.2.0(esbuild@0.25.0)(rollup@4.34.8):
rollup-plugin-esbuild@6.2.1(esbuild@0.25.0)(rollup@4.34.8):
dependencies:
debug: 4.4.0
es-module-lexer: 1.6.0

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后cookieget请求地址的时候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