Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const { models } = require("../config/sequelize");
const { ApiError } = require("../utils/errorHandler");
const config = require("../config/config");
/**
* Signin (Login) Service
*/
exports.signin = async (email, password) => {
// Include soft-deleted users by disabling paranoid mode
const user = await models.User.findOne({
where: { email },
paranoid: false,
});
// If no user exists at all
Iif (!user) {
throw new ApiError("Email or password is incorrect", 401);
}
// If user is soft-deleted
Iif (user.deletedAt) {
throw new ApiError(
"This account has been deactivated. Please contact support.",
403,
);
}
// Check password
const isPasswordValid = await bcrypt.compare(password, user.password);
Iif (!isPasswordValid) {
throw new ApiError("Email or password is incorrect", 401);
}
// Generate JWT
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiration,
});
delete user.get({ plain: true }).password;
return { user, token };
};
|