Commit 3b725380 authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Login API with new pointers

parent dd2525eb
...@@ -2,6 +2,7 @@ const asyncHandler = require("express-async-handler"); ...@@ -2,6 +2,7 @@ const asyncHandler = require("express-async-handler");
const User = require("../models/userModel"); const User = require("../models/userModel");
const generateToken = require("../config/generateToken"); const generateToken = require("../config/generateToken");
//Add registerUser function
const registerUser = asyncHandler(async (req, res) => { const registerUser = asyncHandler(async (req, res) => {
const { name, email, password, pic } = req.body; const { name, email, password, pic } = req.body;
...@@ -39,4 +40,27 @@ const registerUser = asyncHandler(async (req, res) => { ...@@ -39,4 +40,27 @@ const registerUser = asyncHandler(async (req, res) => {
} }
}); });
//Add authUser function
const authUser = asyncHandler(async (req, res) => {
//login user
const { email, password } = req.body;
//Find our user
const user = await User.findOne({ email });
//If user is already exist check it with DB data such as password
if (user) {
res.json({
_id: user._id,
name: user.name,
email: user.email,
pic: user.pic,
token:generateToken(user._id),
});
} else {
res.status(401);
throw new Error("Invalid Email or Password");
}
});
module.exports = { registerUser }; module.exports = { registerUser };
\ No newline at end of file
...@@ -4,6 +4,6 @@ const { registerUser } = require("../controllers/userControllers"); ...@@ -4,6 +4,6 @@ const { registerUser } = require("../controllers/userControllers");
const router = express.Router(); const router = express.Router();
router.route("/").post(registerUser); router.route("/").post(registerUser);
//router.post("/login",authUser) router.post("/login", authUser);
module.exports = router; module.exports = router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment