add user module
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import { createUserConfig } from '@/modules/user/config';
|
||||
|
||||
export const user = createUserConfig(() => ({}));
|
||||
@@ -0,0 +1,47 @@
|
||||
import { get, isNil, toNumber } from 'lodash';
|
||||
|
||||
import { Configure } from '@/modules/config/configure';
|
||||
import { ConfigureFactory, ConfigureRegister } from '@/modules/config/types';
|
||||
import { UserConfig } from '@/modules/user/types';
|
||||
|
||||
/**
|
||||
* 默认用户配置
|
||||
* @param configure
|
||||
*/
|
||||
export function defaultUserConfig(configure: Configure): UserConfig {
|
||||
return {
|
||||
hash: 10,
|
||||
jwt: {
|
||||
token_expired: configure.env.get('USER_TOKEN_EXPIRED', (v) => toNumber(v), 1800),
|
||||
refresh_token_expired: configure.env.get(
|
||||
'USER_REFRESH_TOKEN_EXPIRED',
|
||||
(v) => toNumber(v),
|
||||
3600 * 30,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户配置创建函数
|
||||
* @param register
|
||||
*/
|
||||
export const createUserConfig: (
|
||||
register: ConfigureRegister<RePartial<UserConfig>>,
|
||||
) => ConfigureFactory<UserConfig> = (register) => ({
|
||||
register,
|
||||
defaultRegister: defaultUserConfig,
|
||||
});
|
||||
|
||||
/**
|
||||
* 获取user模块配置的值
|
||||
* @param configure
|
||||
* @param key
|
||||
*/
|
||||
export async function getUserConfig<T>(configure: Configure, key?: string): Promise<T> {
|
||||
const userConfig = await configure.get<UserConfig>('user', defaultUserConfig(configure));
|
||||
if (isNil(key)) {
|
||||
return userConfig as T;
|
||||
}
|
||||
return get(userConfig, key) as T;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 用户配置类型
|
||||
*/
|
||||
export interface UserConfig {
|
||||
/**
|
||||
* 对密码进行混淆时的hash数量值
|
||||
*/
|
||||
hash: number;
|
||||
/**
|
||||
* jwt token的生成配置
|
||||
*/
|
||||
jwt: JwtConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* JWT配置类型
|
||||
*/
|
||||
export interface JwtConfig {
|
||||
/**
|
||||
* token过期时间
|
||||
*/
|
||||
token_expired: number;
|
||||
/**
|
||||
* refresh token
|
||||
*/
|
||||
refresh_token_expired: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* JWT荷载签出对象
|
||||
*/
|
||||
export interface JwtPayload {
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
sub: string;
|
||||
/**
|
||||
* 签出时间
|
||||
*/
|
||||
iat: number;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
import { Configure } from '@/modules/config/configure';
|
||||
import { getUserConfig } from '@/modules/user/config';
|
||||
|
||||
/**
|
||||
* 加密明文密码
|
||||
* @param configure
|
||||
* @param password
|
||||
*/
|
||||
export async function encrypt(configure: Configure, password: string) {
|
||||
const hash: number = (await getUserConfig<number>(configure, 'hash')) || 10;
|
||||
return bcrypt.hashSync(password, hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证密码
|
||||
* @param password
|
||||
* @param hashed
|
||||
*/
|
||||
export function decrypt(password: string, hashed: string) {
|
||||
return bcrypt.compareSync(password, hashed);
|
||||
}
|
||||
Reference in New Issue
Block a user