-
-
Save techomoro/3e9048da6e3ebef78790a0814dae5ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const router = express.Router(); | |
let models = require("../../model"); | |
let _ = require("lodash"); | |
const jwt = require("jsonwebtoken"); | |
const config = require("../../config"); | |
router.post("/", async (req, res) => { | |
try { | |
let user = await models.User.findOne({ | |
email: req.body.email, | |
}); | |
if (user) { | |
let isUserAuthenticated = user.validatePassword( | |
req.body.password, | |
user.password | |
); | |
if (isUserAuthenticated) { | |
user.token = jwt.sign( | |
{ | |
id: user._id, | |
}, | |
config.secret, | |
{ | |
expiresIn: "10d", | |
} | |
); | |
res.status(200).json({ | |
status: 200, | |
data: _.pick(user, models.User.returnable), | |
}); | |
} else { | |
res.status(400).json({ | |
status: 400, | |
message: "Password is incorrect", | |
}); | |
} | |
} else { | |
res.status(400).json({ | |
status: 400, | |
message: "No account exist", | |
}); | |
} | |
} catch (err) { | |
console.log(err); | |
res.status(400).json({ | |
message: "Some error occured", | |
err, | |
}); | |
} | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rewrite that does the same thing but it is much shorter, easier to read and understand