diff --git a/models/user/index.js b/models/user/index.js index 18393f7..eea3192 100644 --- a/models/user/index.js +++ b/models/user/index.js @@ -29,6 +29,13 @@ userSchema.pre('save', async function(next){ } }); +userSchema.methods.isValidPassword = async function(newPassword){ + try{ + return await bcrypt.compare(newPassword, this.password); + }catch(error){ + throw new Error(error); + } +} const user = mongoose.model('user', userSchema); module.exports = user; \ No newline at end of file diff --git a/passport.js b/passport.js index d9bfbb5..64a4caf 100644 --- a/passport.js +++ b/passport.js @@ -32,17 +32,26 @@ passport.use(new JwtStrategy({ passport.use(new LocalStrategy({ usernameField: 'email' }, async(email, password, done)=>{ - // Find the user given the email - const user = await User.findOne({ email }); + try{ + // Find the user given the email + const user = await User.findOne({ email }); - // If not found, handle it - if(!user){ - return done(null, false); + // If not found, handle it + if(!user){ + return done(null, false); + } + // If found, Check whether password is correct + const isMatch = await user.isValidPassword(password); + + // If not, handle it + if(!isMatch){ + return done(null,false); + } + // Otherwise return the user + return done(null,user); + }catch(error) { + return done(error,false); } - // If found, Check whether password is correct - - // If not, handle it - - // Otherwise return the user + } )); \ No newline at end of file diff --git a/routes/api/user.js b/routes/api/user.js index 0025446..0c39e9a 100644 --- a/routes/api/user.js +++ b/routes/api/user.js @@ -29,7 +29,9 @@ module.exports = { }, signinUser: async(req, res, next) =>{ // Generate token - - res.end('Responding from Signin User'); + logger.info(`signing in User : ${req.email} `); + + const token = signToken(req.user, JWT_SECRET); + res.status(200).json({ token }); } }; \ No newline at end of file diff --git a/routes/helpers/routeHelper.js b/routes/helpers/routeHelper.js index e75b255..3b610ea 100644 --- a/routes/helpers/routeHelper.js +++ b/routes/helpers/routeHelper.js @@ -4,6 +4,7 @@ module.exports = { validateBody : (schema) =>{ return (req,res,next) =>{ const result = joi.validate(req.body, schema ); + if(result.error){ return res.status(400).json(result.error) } @@ -16,7 +17,7 @@ module.exports = { } }, schemas: { - authSchema: joi.object().keys({ + signinSignupSchema: joi.object().keys({ email: joi.string().email().required(), password: joi.string().required(), }) diff --git a/routes/index.js b/routes/index.js index 8bb9da9..4b12340 100755 --- a/routes/index.js +++ b/routes/index.js @@ -14,7 +14,7 @@ router.get('/', (request, response, next) =>{ }); // For Signin -router.get('/signin',validateBody(schemas.signinSignupSchema), user.signinUser); +router.post('/signin',validateBody(schemas.signinSignupSchema), passport.authenticate('local',{ session: false}),user.signinUser); // For Signup router.post('/signup', validateBody(schemas.signinSignupSchema),user.createUser);