Skip to content

Commit

Permalink
user signin and token returning
Browse files Browse the repository at this point in the history
  • Loading branch information
SummySumanth committed Jul 27, 2018
1 parent 34317ca commit a45d966
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
7 changes: 7 additions & 0 deletions models/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 19 additions & 10 deletions passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
));
6 changes: 4 additions & 2 deletions routes/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
};
3 changes: 2 additions & 1 deletion routes/helpers/routeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -16,7 +17,7 @@ module.exports = {
}
},
schemas: {
authSchema: joi.object().keys({
signinSignupSchema: joi.object().keys({
email: joi.string().email().required(),
password: joi.string().required(),
})
Expand Down
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a45d966

Please sign in to comment.