Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(user => {
if(!user){
return res.status(400).json("用户名或密码错误");
}else{
// 校验密码
const password = req.body.password;
const isValidPassword = bcrypt.compareSync(password, user.password);
if(!isValidPassword){
return res.status(400).json("用户名或密码错误");
}else{
UserInfo.findOne({email})
.then(userinfo => {
Course.findOne({email})
.then(usercourse => {
// 用户登录日志
const newLogin_Log = new UserLogin_Log({
email,
username:userinfo.username,
loginTime:new Date().format("yyyy/MM/dd HH:mm:ss")
})
newLogin_Log.save();
function isTokenValid(username, token) {
// loading existing token
var data = confMgmt.load(confMgmt.CONF_FILES.TOKENS);
// getting specific token for username
var encryptedToken = data[username];
// encryptedToken doesn't exist in config ?
if (encryptedToken === undefined) {
return false;
}
return bcrypt.compareSync(token, encryptedToken);
}
const validPassword = function(user, password) {
return bcrypt.compareSync(password, user.password);
}
module.exports = function(passport) {
User.prototype.authenticate = function(value) {
if (bcrypt.compareSync(value, this.passwordDigest)){
return this;
} else{
return false;
}
};
return User.find({ username: username }).exec().then((users, err) => {
if (!users.length) {
throw createError(401, 'That user does not exist');
}
const user = users[0];
if (!compareSync(password, user.password)) {
throw createError(401, 'Wrong password');
}
return user;
});
};
userSchema.methods.passwordMatches = function (plainText) {
var user = this;
return bcrypt.compareSync(plainText, user.password);
};
userModel.findOne({ email: req.body.email }, function(err, userInfo) {
if (err) {
next(err);
} else {
if (bcrypt.compareSync(req.body.password, userInfo.password)) {
const token = jwt.sign(
{ id: userInfo._id },
req.app.get("secretKey"),
{ expiresIn: "1h" }
);
res.json({
status: "success",
message: "user found!!!",
data: { user: userInfo, token: token }
});
} else {
res.json({
status: "error",
message: "Invalid email/password!!!",
data: null
});
authenticate: function(username, password) {
if (config.adminAuth.username === username && bcrypt.compareSync(password,config.adminAuth.password)) {
return when.resolve({username:username,permissions:"*"});
} else {
return when.resolve(null);
}
}
};
.then((users) => {
if (!users.length) {
throw createError(401, 'That user does not exist')
}
const user = users[0]
if (!compareSync(password, user.password)) {
throw createError(401, 'Wrong password')
}
return user
})