Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exam1 dhlieu2 #67

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
28 changes: 26 additions & 2 deletions src/controllers/Auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const register = async (req, res, next) => {
err.status = 400;
throw err;
}
const existingUser = await User.findOne({userCode});
const existingUser = await User.findOne({ userCode });
if (existingUser) {
const err = new Error("User is exist");
err.status = 400;
Expand All @@ -21,7 +21,31 @@ const register = async (req, res, next) => {
next(error);
}
};

const login = async (req, res, next) => {
const { password, userCode } = req.body;
try {
if (!password) {
const err = new Error("Password is require");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required

err.status = 400;
throw err;
}
const existingUser = await User.findOne({ userCode });
if (!existingUser) {
const err = new Error("User is not exist");
err.status = 400;
throw err;
}
const token = await jwt.sign(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check comparePassword vs trả về lỗi cho người dùng là: username or password is incorrect

{ userId: existingUser._id },
process.env.SECRET_KEY,
{ expiresIn: process.env.JWT_EXPIRES_IN }
);
res.status(201).json(token);
} catch (error) {
next(error);
}
};
module.exports = {
register,
login,
};
13 changes: 6 additions & 7 deletions src/routers/auth.route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const express = require('express')
const {register} = require('../controllers/Auth.controller')
const express = require("express");
const { register, login } = require("../controllers/auth.controller");

const authMiddleware = express.Router()
const authMiddleware = express.Router();

authMiddleware
.route('/register').post(register)

module.exports = authMiddleware
authMiddleware.route("/register").post(register);
authMiddleware.route("/login").post(login);
module.exports = authMiddleware;