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", "node-fetch": "^3.3.2",
"p-queue": "^8.1.0", "p-queue": "^8.1.0",
"pg": "^8.13.3", "pg": "^8.13.3",
"rollup-plugin-esbuild": "^6.2.0", "rollup-plugin-esbuild": "^6.2.1",
"semver": "^7.7.1", "semver": "^7.7.1",
"sequelize": "^6.37.5", "sequelize": "^6.37.5",
"socket.io": "^4.8.1", "socket.io": "^4.8.1",

20
pnpm-lock.yaml generated
View File

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

View File

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

View File

@ -34,15 +34,14 @@ app
} }
user.setTokenUser(tokenUser); user.setTokenUser(tokenUser);
const orgs = await user.getOrgs(); const orgs = await user.getOrgs();
if (orgs.includes('admin')) { if (orgs.includes('admin')) {
ctx.body = 'admin'; ctx.body = 'admin';
ctx.nextQuery = ctx.query;
} else { } else {
ctx.throw(403, 'forbidden'); ctx.throw(403, 'forbidden');
} }
} catch (e) { } 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); ctx.throw(500, e.message);
} }
}) })

View File

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

View File

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

View File

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