fix: fix login bugs
This commit is contained in:
parent
b934687314
commit
262ef1d118
@ -18,7 +18,6 @@ export const addAuth = (app: App) => {
|
||||
.route({
|
||||
path: 'auth',
|
||||
id: 'auth',
|
||||
isDebug: true,
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const token = ctx.query.token;
|
||||
|
@ -43,6 +43,42 @@ export class AppModel extends Model {
|
||||
declare proxy: boolean;
|
||||
declare user: string;
|
||||
declare status: string;
|
||||
static async moveToNewUser(oldUserName: string, newUserName: string) {
|
||||
const appIds = await AppModel.findAll({
|
||||
where: {
|
||||
user: oldUserName,
|
||||
},
|
||||
attributes: ['id'],
|
||||
});
|
||||
for (const app of appIds) {
|
||||
const appData = await AppModel.findByPk(app.id);
|
||||
appData.user = newUserName;
|
||||
const data = appData.data;
|
||||
data.files = await AppModel.getNewFiles(data.files, {
|
||||
oldUser: oldUserName,
|
||||
newUser: newUserName,
|
||||
});
|
||||
appData.data = { ...data };
|
||||
await appData.save({ fields: ['data', 'user'] });
|
||||
}
|
||||
}
|
||||
static async getNewFiles(files: { name: string; path: string }[] = [], opts: { oldUser: string; newUser: string } = { oldUser: '', newUser: '' }) {
|
||||
const { oldUser, newUser } = opts;
|
||||
const _ = files.map((item) => {
|
||||
if (item.path.startsWith('http')) {
|
||||
return item;
|
||||
}
|
||||
if (oldUser && item.path.startsWith(oldUser)) {
|
||||
return item;
|
||||
}
|
||||
const paths = item.path.split('/');
|
||||
return {
|
||||
...item,
|
||||
path: newUser + '/' + paths.slice(1).join('/'),
|
||||
};
|
||||
});
|
||||
return _;
|
||||
}
|
||||
}
|
||||
AppModel.init(
|
||||
{
|
||||
|
@ -76,13 +76,7 @@ app
|
||||
rest.user = tokenUser.username;
|
||||
let files = newData?.files || [];
|
||||
if (files.length > 0) {
|
||||
files = files.map((item) => {
|
||||
const paths = item.path.split('/');
|
||||
return {
|
||||
...item,
|
||||
path: newData.user + '/' + paths.slice(1).join('/'),
|
||||
};
|
||||
});
|
||||
files = await AppModel.getNewFiles(files, { oldUser: app.user, newUser: tokenUser.username });
|
||||
}
|
||||
newData.files = files;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { User } from '@/models/user.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts';
|
||||
import { AppModel } from '@/routes/app-manager/index.ts';
|
||||
// import { mvAppFromUserAToUserB } from '@/routes/app-manager/admin/mv-user-app.ts';
|
||||
|
||||
export const checkUsername = (username: string) => {
|
||||
@ -47,6 +48,12 @@ app
|
||||
await backupUserA(oldName, user.id); // 备份文件数据
|
||||
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
|
||||
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
|
||||
|
||||
if (['org', 'user'].includes(user.type)) {
|
||||
const type = user.type === 'org' ? 'org' : 'user';
|
||||
await User.clearUserToken(user.id, type); // 清除旧token
|
||||
}
|
||||
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据
|
||||
} catch (error) {
|
||||
console.error('迁移文件数据失败', error);
|
||||
ctx.throw(500, 'Failed to change username');
|
||||
|
@ -80,12 +80,17 @@ export const clearCookie = (ctx: any) => {
|
||||
if (!domain) {
|
||||
return;
|
||||
}
|
||||
ctx.res.cookie('token', '', {
|
||||
maxAge: 0,
|
||||
domain,
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
});
|
||||
const browser = ctx.req.headers['user-agent'];
|
||||
const isBrowser = browser.includes('Mozilla'); // 浏览器
|
||||
if (isBrowser && ctx.res.cookie) {
|
||||
ctx.res.cookie('token', '_', {
|
||||
maxAge: 1,
|
||||
domain,
|
||||
path: '/',
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
});
|
||||
}
|
||||
};
|
||||
app
|
||||
.route({
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit ad0d2e717f0cd409530735ab7143c94d910f939e
|
||||
Subproject commit 254735596428e47d0f6a64fa676924b01597ae57
|
Loading…
x
Reference in New Issue
Block a user