feat: cookie和domain优化
This commit is contained in:
@@ -8,4 +8,4 @@ const config = useConfig<MinioConfig>();
|
||||
/**
|
||||
* 用来放cookie的域名
|
||||
*/
|
||||
export const domain = config.domain || 'xiongxiao.me';
|
||||
export const domain = config.domain || ''; // 请在这里填写你的域名
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user