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 { DataTypes, Model, Op } from 'sequelize';
|
||||||
import { createToken, checkToken } from '@kevisual/auth';
|
import { createToken, checkToken } from '@kevisual/auth';
|
||||||
import { cryptPwd } from '@kevisual/auth';
|
import { cryptPwd } from '@kevisual/auth';
|
||||||
import { nanoid } from 'nanoid';
|
import { customRandom, nanoid, customAlphabet } from 'nanoid';
|
||||||
import { CustomError } from '@kevisual/router';
|
import { CustomError } from '@kevisual/router';
|
||||||
import { Org } from './org.ts';
|
import { Org } from './org.ts';
|
||||||
import { redis } from '@/app.ts';
|
import { redis } from '@/app.ts';
|
||||||
@ -17,6 +17,7 @@ export class User extends Model {
|
|||||||
declare id: string;
|
declare id: string;
|
||||||
declare username: string;
|
declare username: string;
|
||||||
declare nickname: string; // 昵称
|
declare nickname: string; // 昵称
|
||||||
|
declare alias: string; // 别名
|
||||||
declare password: string;
|
declare password: string;
|
||||||
declare salt: string;
|
declare salt: string;
|
||||||
declare needChangePassword: boolean;
|
declare needChangePassword: boolean;
|
||||||
@ -36,7 +37,7 @@ export class User extends Model {
|
|||||||
* @param uid
|
* @param uid
|
||||||
* @returns
|
* @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;
|
const { id, username, type } = this;
|
||||||
let expireTime = 60 * 60 * 24 * 7; // 7 days
|
let expireTime = 60 * 60 * 24 * 7; // 7 days
|
||||||
switch (loginType) {
|
switch (loginType) {
|
||||||
@ -169,6 +170,10 @@ User.init(
|
|||||||
type: DataTypes.TEXT,
|
type: DataTypes.TEXT,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
|
alias: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
||||||
|
},
|
||||||
password: {
|
password: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
@ -221,38 +226,48 @@ User.sync({ alter: true, logging: false })
|
|||||||
console.error('Sync User error', err);
|
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();
|
const w = await User.findAndCountAll();
|
||||||
console.info('[User count]', w.count);
|
console.info('[User count]', w.count);
|
||||||
const password = '2e8a305521bba54f49638ed25e46adf3'; //123456
|
|
||||||
const salt = '123';
|
|
||||||
if (w.count < 1) {
|
if (w.count < 1) {
|
||||||
const root = await User.create({
|
const root = await User.createUser('root', pwd, '系统管理员');
|
||||||
username: 'root',
|
|
||||||
password: password,
|
|
||||||
needChangePassword: true,
|
|
||||||
type: 'user',
|
|
||||||
description: '系统管理员',
|
|
||||||
salt,
|
|
||||||
});
|
|
||||||
const org = await User.createOrg('admin', root.id, '管理员');
|
const org = await User.createOrg('admin', root.id, '管理员');
|
||||||
console.info(' new Users name', root.username, org.username);
|
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);
|
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({
|
const w = await User.findAndCountAll({
|
||||||
logging: false,
|
logging: false,
|
||||||
});
|
});
|
||||||
console.info('[User count]', w.count);
|
console.info('[User count]', w.count);
|
||||||
const username = 'demo';
|
|
||||||
const u = await User.findOne({ where: { username }, logging: false });
|
const u = await User.findOne({ where: { username }, logging: false });
|
||||||
if (!u) {
|
if (!u) {
|
||||||
const user = await User.createUser(username, '', 'demo');
|
const user = await User.createUser(username, pwd, 'demo');
|
||||||
console.info('new Users name', user.username);
|
console.info('new Users name', user.username);
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
data: { user, pwd: pwd },
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
console.info('Users has been created', u.username);
|
console.info('Users has been created', u.username);
|
||||||
|
return {
|
||||||
|
code: 500,
|
||||||
|
message: 'Users has been created',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// initializeUser();
|
// initializeUser();
|
||||||
@ -268,4 +283,6 @@ export class UserServices extends User {
|
|||||||
const token = await user.createToken(null, 'season');
|
const token = await user.createToken(null, 'season');
|
||||||
return { ...token, isNew };
|
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 './me.ts';
|
||||||
|
|
||||||
import './update.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 { 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';
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
@ -38,12 +39,6 @@ app
|
|||||||
if (!user && email) {
|
if (!user && email) {
|
||||||
user = await User.findOne({ where: { 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) {
|
if (!user) {
|
||||||
ctx.throw(500, 'Login Failed');
|
ctx.throw(500, 'Login Failed');
|
||||||
}
|
}
|
||||||
@ -53,7 +48,7 @@ app
|
|||||||
const token = await user.createToken(null, loginType);
|
const token = await user.createToken(null, loginType);
|
||||||
ctx.res.cookie('token', token.token, {
|
ctx.res.cookie('token', token.token, {
|
||||||
maxAge: token.expireTime,
|
maxAge: token.expireTime,
|
||||||
domain: 'xiongxiao.me',
|
domain: { domain },
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
});
|
});
|
||||||
@ -69,7 +64,7 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
ctx.res.cookie('token', '', {
|
ctx.res.cookie('token', '', {
|
||||||
maxAge: 0,
|
maxAge: 0,
|
||||||
domain: 'xiongxiao.me',
|
domain: `${domain}`,
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user