Skip to content

Commit

Permalink
BE #2: User login done with creating a json web token.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atiqul Alam Rocky committed May 16, 2020
1 parent 6231668 commit 335277b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ require('dotenv').config();

module.exports = {
DB: process.env.APP_DB,
PORT: process.env.PORT || 3000
PORT: process.env.PORT || 3000,
SECRET: process.env.APP_SECRET
}
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"consola": "2.12.1",
"dotenv": "^8.2.0",
"express": "4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "5.9.13",
"morgan": "1.10.0",
"passport": "0.4.1"
Expand Down
21 changes: 19 additions & 2 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const router = require('express').Router();
const { info, error } = require('consola');
const { userRegistration } = require('../utils/Auth')
const { userRegistration, userLogin } = require('../utils/Auth')

router.post('/register', async (req, res)=>{
info({
Expand All @@ -18,7 +18,24 @@ router.post('/register', async (req, res)=>{
return res.status(400).json({message: err.message});
}

})
});

router.post('/login', async (req, res) => {
info({
message: 'try to login the user',
badge: true
});
try{
let user = await userLogin(req.body, "editor", res);
return res.status(200).json(user)
}catch(err){
error({
message: err,
badge: true
})
return res.status(400).json({message: err.message});
}
});


module.exports = router;
44 changes: 43 additions & 1 deletion utils/Auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const User = require('../models/User');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { success, error, info } = require('consola');

const { SECRET } = require('../config/application');

/**
* @DESC To register the user
*/
Expand Down Expand Up @@ -36,11 +39,50 @@ const { success, error, info } = require('consola');

}

const userLogin = async (userCreds, role, res) => {
let {userName, password} = userCreds;

//check is userName exists
const user = await User.findOne({userName});
if(!user){
throw new Error(`No user found. Invalid login credentials.`)
}

//check role
if(user.role !== role){
throw new Error(`Please make sure you are loging in from the right portal`);
}

//matche password
let passMatch = await bcrypt.compare(password, user.password);
if(passMatch){
//sign the token and issue it to the user
let token = jwt.sign({
userid: user._id,
role: user.role,
name: user.userName,
email: user.email
}, SECRET, {expiresIn: '5 min'});

return {
userName: user.userName,
role: user.role,
email: user.email,
token: `Bearer ${token}`,
expiresIn: 5
}

}else{
throw new Error(`Incorrect password.`)
}

}

const validateUsername = async(userName) => {
let user = await User.findOne({userName});
return user ? true : false;
};



module.exports = { userRegistration}
module.exports = { userRegistration, userLogin }

0 comments on commit 335277b

Please sign in to comment.