一、node-auth0
主要提供的功能
二、包的使用
npm install node-auth0
三、使用步骤
import NodeAuth from 'node-auth0';
class User1Dao extends BaseDao {
constructor() {
super();
/**
* 关于NodeAuth构造函数说明
* 1.可以不传递参数(默认是8位)
* 2.可以传递参数(minLength, maxLength, 是否随机长度)
*/
this.nodeAuth = new NodeAuth(8, 10, true);
}
...
}
...
async createUser(params) {
try {
const { name, password } = params;
return await UserModel.create({
name,
password: this.nodeAuth.makePassword(password)
});
} catch (e) {
throw e;
}
}
async login(params) {
try {
const { name, password } = params;
const user = await UserModel.findOne({
where: {
name
}
});
if (this.nodeAuth.authenticate(password, user.password)) {
return user;
} else {
throw {
msg: '登录错误',
desc: '用户名与密码错误'
};
}
} catch (e) {
throw {
msg: '登录错误',
desc: '用户名与密码错误'
};
}
}