|
@@ -51,4 +51,34 @@ router.get('/auth/user', virifyToken, async (req, res) => {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+router.post('/auth/login', async (req, res) => {
|
|
|
+ try {
|
|
|
+ let foundUser = await User.findOne({ email: req.body.email})
|
|
|
+ if (!foundUser) {
|
|
|
+ res.status(403).json({
|
|
|
+ success: false,
|
|
|
+ message: 'Authentication failed, User not found '
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ if (foundUser.comparePassword(req.body.password)) {
|
|
|
+ let token = jwt.sign(foundUser.toJSON(), process.env.SECRET, {
|
|
|
+ expiresIn: 604800
|
|
|
+ })
|
|
|
+s
|
|
|
+ res.json({ success: true, token})
|
|
|
+ } else {
|
|
|
+ res.status(403).json({
|
|
|
+ success: false,
|
|
|
+ message: 'Authentication failed, Wrong password'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ res.status(500).json({
|
|
|
+ success: false,
|
|
|
+ message: err.message
|
|
|
+ })
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
module.exports = router
|