添加用户
This commit is contained in:
@@ -41,7 +41,7 @@ RouterCodeModel.init(
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: '用户code id',
|
||||
comment: 'code id',
|
||||
},
|
||||
path: {
|
||||
type: DataTypes.STRING,
|
||||
|
||||
@@ -1,24 +1,60 @@
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { sequelize } from '@/modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
import { createToken, checkToken } from '@abearxiong/auth/token';
|
||||
import { cryptPwd } from '@abearxiong/auth';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
|
||||
const config = useConfig<{ tokenSecret: string }>();
|
||||
|
||||
export class User extends Model {
|
||||
declare id: number;
|
||||
username: string;
|
||||
password: string;
|
||||
salt: string;
|
||||
remark: string;
|
||||
declare id: string;
|
||||
declare username: string;
|
||||
declare password: string;
|
||||
declare salt: string;
|
||||
declare needChangePassword: boolean;
|
||||
declare description: string;
|
||||
declare data: any;
|
||||
async createToken() {
|
||||
const { id, username } = this;
|
||||
const expireTime = 60 * 60 * 24 * 7; // 7 days
|
||||
const now = new Date().getTime();
|
||||
const token = await createToken({ id, username }, config.tokenSecret);
|
||||
return { token, expireTime: now + expireTime };
|
||||
}
|
||||
static async verifyToken(token: string) {
|
||||
return await checkToken(token, config.tokenSecret);
|
||||
}
|
||||
static createUser(username: string, password?: string, description?: string) {
|
||||
const user = User.findOne({ where: { username } });
|
||||
if (user) {
|
||||
throw new CustomError('User already exists');
|
||||
}
|
||||
const salt = nanoid(6);
|
||||
let needChangePassword = !password;
|
||||
password = password || '123456';
|
||||
const cPassword = cryptPwd(password, salt);
|
||||
return User.create({ username, password: cPassword, description, salt, needChangePassword });
|
||||
}
|
||||
createPassword(password: string) {
|
||||
const salt = this.salt;
|
||||
const cPassword = cryptPwd(password, salt);
|
||||
this.password = cPassword;
|
||||
return cPassword;
|
||||
}
|
||||
}
|
||||
User.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
autoIncrement: true,
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
},
|
||||
|
||||
username: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
@@ -28,28 +64,44 @@ User.init(
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
remark: {
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
needChangePassword: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'cf_user',
|
||||
},
|
||||
);
|
||||
// User.sync({ alter: true });
|
||||
User.sync({ alter: true, logging: false })
|
||||
.then((res) => {
|
||||
// initializeUser();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Sync User error', err);
|
||||
});
|
||||
|
||||
export const initializeUser = async () => {
|
||||
const w = await User.findOne();
|
||||
const w = await User.findAndCountAll();
|
||||
console.info('[User count]', w.count);
|
||||
const password = '2e8a305521bba54f49638ed25e46adf3'; //123456
|
||||
const salt = '123';
|
||||
const users = [{ username: 'admin' }, { username: 'user' }, { username: 'root' }];
|
||||
if (!w) {
|
||||
if (w.count < 1) {
|
||||
const newUsers = await User.bulkCreate(
|
||||
users.map((user) => {
|
||||
return {
|
||||
...user,
|
||||
password,
|
||||
needChangePassword: true,
|
||||
salt,
|
||||
};
|
||||
}),
|
||||
@@ -57,3 +109,5 @@ export const initializeUser = async () => {
|
||||
console.info('[create new Users]', newUsers);
|
||||
}
|
||||
};
|
||||
|
||||
// initializeUser();
|
||||
|
||||
Reference in New Issue
Block a user