fix: fix login bugs

This commit is contained in:
熊潇 2025-05-23 00:08:27 +08:00
parent b934687314
commit 262ef1d118
6 changed files with 56 additions and 15 deletions

View File

@ -18,7 +18,6 @@ export const addAuth = (app: App) => {
.route({ .route({
path: 'auth', path: 'auth',
id: 'auth', id: 'auth',
isDebug: true,
}) })
.define(async (ctx) => { .define(async (ctx) => {
const token = ctx.query.token; const token = ctx.query.token;

View File

@ -43,6 +43,42 @@ export class AppModel extends Model {
declare proxy: boolean; declare proxy: boolean;
declare user: string; declare user: string;
declare status: 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( AppModel.init(
{ {

View File

@ -76,13 +76,7 @@ app
rest.user = tokenUser.username; rest.user = tokenUser.username;
let files = newData?.files || []; let files = newData?.files || [];
if (files.length > 0) { if (files.length > 0) {
files = files.map((item) => { files = await AppModel.getNewFiles(files, { oldUser: app.user, newUser: tokenUser.username });
const paths = item.path.split('/');
return {
...item,
path: newData.user + '/' + paths.slice(1).join('/'),
};
});
} }
newData.files = files; newData.files = files;
} }

View File

@ -3,6 +3,7 @@ import { User } from '@/models/user.ts';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { CustomError } from '@kevisual/router'; import { CustomError } from '@kevisual/router';
import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts'; 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'; // import { mvAppFromUserAToUserB } from '@/routes/app-manager/admin/mv-user-app.ts';
export const checkUsername = (username: string) => { export const checkUsername = (username: string) => {
@ -47,6 +48,12 @@ app
await backupUserA(oldName, user.id); // 备份文件数据 await backupUserA(oldName, user.id); // 备份文件数据
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据 await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据 // 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) { } catch (error) {
console.error('迁移文件数据失败', error); console.error('迁移文件数据失败', error);
ctx.throw(500, 'Failed to change username'); ctx.throw(500, 'Failed to change username');

View File

@ -80,12 +80,17 @@ export const clearCookie = (ctx: any) => {
if (!domain) { if (!domain) {
return; return;
} }
ctx.res.cookie('token', '', { const browser = ctx.req.headers['user-agent'];
maxAge: 0, const isBrowser = browser.includes('Mozilla'); // 浏览器
if (isBrowser && ctx.res.cookie) {
ctx.res.cookie('token', '_', {
maxAge: 1,
domain, domain,
path: '/',
sameSite: 'lax', sameSite: 'lax',
httpOnly: true, httpOnly: true,
}); });
}
}; };
app app
.route({ .route({

@ -1 +1 @@
Subproject commit ad0d2e717f0cd409530735ab7143c94d910f939e Subproject commit 254735596428e47d0f6a64fa676924b01597ae57