feat: 初始化应用修改
This commit is contained in:
parent
c61999e2a4
commit
4e080a0b93
@ -3,7 +3,7 @@ import { sequelize } from '@/modules/sequelize.ts';
|
||||
import { DataTypes, Model, Op } from 'sequelize';
|
||||
import { createToken, checkToken } from '@kevisual/auth';
|
||||
import { cryptPwd } from '@kevisual/auth';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { customRandom, nanoid, customAlphabet } from 'nanoid';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { Org } from './org.ts';
|
||||
import { redis } from '@/app.ts';
|
||||
@ -17,6 +17,7 @@ export class User extends Model {
|
||||
declare id: string;
|
||||
declare username: string;
|
||||
declare nickname: string; // 昵称
|
||||
declare alias: string; // 别名
|
||||
declare password: string;
|
||||
declare salt: string;
|
||||
declare needChangePassword: boolean;
|
||||
@ -36,7 +37,7 @@ export class User extends Model {
|
||||
* @param uid
|
||||
* @returns
|
||||
*/
|
||||
async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year' ) {
|
||||
async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year') {
|
||||
const { id, username, type } = this;
|
||||
let expireTime = 60 * 60 * 24 * 7; // 7 days
|
||||
switch (loginType) {
|
||||
@ -169,6 +170,10 @@ User.init(
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
alias: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
@ -221,38 +226,48 @@ User.sync({ alter: true, logging: false })
|
||||
console.error('Sync User error', err);
|
||||
});
|
||||
|
||||
export const initializeUser = async () => {
|
||||
const letter = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const custom = customAlphabet(letter, 6);
|
||||
export const initializeUser = async (pwd = custom()) => {
|
||||
const w = await User.findAndCountAll();
|
||||
console.info('[User count]', w.count);
|
||||
const password = '2e8a305521bba54f49638ed25e46adf3'; //123456
|
||||
const salt = '123';
|
||||
if (w.count < 1) {
|
||||
const root = await User.create({
|
||||
username: 'root',
|
||||
password: password,
|
||||
needChangePassword: true,
|
||||
type: 'user',
|
||||
description: '系统管理员',
|
||||
salt,
|
||||
});
|
||||
const root = await User.createUser('root', pwd, '系统管理员');
|
||||
const org = await User.createOrg('admin', root.id, '管理员');
|
||||
console.info(' new Users name', root.username, org.username);
|
||||
console.info('new Users root password', pwd);
|
||||
console.info('new Users id', root.id, org.id);
|
||||
CreateDemoUser();
|
||||
const demo = await createDemoUser();
|
||||
return {
|
||||
code: 200,
|
||||
data: { root, org, pwd: pwd, demo },
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: 500,
|
||||
message: 'Users has been created',
|
||||
};
|
||||
}
|
||||
};
|
||||
export const CreateDemoUser = async () => {
|
||||
export const createDemoUser = async (username = 'demo', pwd = custom()) => {
|
||||
const w = await User.findAndCountAll({
|
||||
logging: false,
|
||||
});
|
||||
console.info('[User count]', w.count);
|
||||
const username = 'demo';
|
||||
const u = await User.findOne({ where: { username }, logging: false });
|
||||
if (!u) {
|
||||
const user = await User.createUser(username, '', 'demo');
|
||||
const user = await User.createUser(username, pwd, 'demo');
|
||||
console.info('new Users name', user.username);
|
||||
return {
|
||||
code: 200,
|
||||
data: { user, pwd: pwd },
|
||||
};
|
||||
} else {
|
||||
console.info('Users has been created', u.username);
|
||||
return {
|
||||
code: 500,
|
||||
message: 'Users has been created',
|
||||
};
|
||||
}
|
||||
};
|
||||
// initializeUser();
|
||||
@ -268,4 +283,6 @@ export class UserServices extends User {
|
||||
const token = await user.createToken(null, 'season');
|
||||
return { ...token, isNew };
|
||||
}
|
||||
static initializeUser = initializeUser;
|
||||
static createDemoUser = createDemoUser;
|
||||
}
|
||||
|
8
src/modules/domain.ts
Normal file
8
src/modules/domain.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
|
||||
type MinioConfig = {
|
||||
domain: string;
|
||||
};
|
||||
const config = useConfig<MinioConfig>();
|
||||
|
||||
export const domain = config.domain || 'xiongxiao.me';
|
@ -4,3 +4,5 @@ import './org.ts';
|
||||
import './me.ts';
|
||||
|
||||
import './update.ts'
|
||||
|
||||
import './init.ts'
|
34
src/routes/user/init.ts
Normal file
34
src/routes/user/init.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { UserServices } from '@/models/user.ts';
|
||||
|
||||
import { app } from '@/app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
key: 'init',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { password } = ctx.query.data || {};
|
||||
if (!password) {
|
||||
ctx.throw(500, 'root password are required');
|
||||
}
|
||||
const res = await UserServices.initializeUser(password);
|
||||
ctx.body = res;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
key: 'createDemo',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { username, password } = ctx.query.data || {};
|
||||
if (!username && username.startsWith('demo')) {
|
||||
ctx.throw(500, 'username are required, and must start with demo');
|
||||
}
|
||||
const res = await UserServices.createDemoUser(username, password);
|
||||
ctx.body = res;
|
||||
})
|
||||
.addTo(app);
|
@ -1,6 +1,7 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { Org } from '@/models/org.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { domain } from '@/modules/domain.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
@ -38,12 +39,6 @@ app
|
||||
if (!user && email) {
|
||||
user = await User.findOne({ where: { email } });
|
||||
}
|
||||
console.log('user logiin', ctx.query);
|
||||
console.log('user logiin', user);
|
||||
console.log(
|
||||
'users',
|
||||
(await User.findAll()).map((u) => u.username),
|
||||
);
|
||||
if (!user) {
|
||||
ctx.throw(500, 'Login Failed');
|
||||
}
|
||||
@ -53,7 +48,7 @@ app
|
||||
const token = await user.createToken(null, loginType);
|
||||
ctx.res.cookie('token', token.token, {
|
||||
maxAge: token.expireTime,
|
||||
domain: 'xiongxiao.me',
|
||||
domain: { domain },
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
});
|
||||
@ -69,7 +64,7 @@ app
|
||||
.define(async (ctx) => {
|
||||
ctx.res.cookie('token', '', {
|
||||
maxAge: 0,
|
||||
domain: 'xiongxiao.me',
|
||||
domain: `${domain}`,
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user