Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamalkun75 committed May 28, 2022
1 parent 17d9994 commit 235e62c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
69 changes: 69 additions & 0 deletions controllers/user.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const bcrypt = require("bcryptjs");
const userServices = require("../services/user.services");

/**
* 1. To secure the password, we are using the bcryptjs, It stores the hashed password in the database.
* 2. In the SignIn API, we are checking whether the assigned and retrieved passwords are the same or not using the bcrypt.compare() method.
* 3. In the SignIn API, we set the JWT token expiration time. Token will be expired within the defined duration.
*/

exports.register = (req, res, next) => {
const { password } = req.body;

const salt = bcrypt.genSaltSync(10);

req.body.password = bcrypt.hashSync(password, salt);

userServices.register(req.body, (error, results) => {
if (error) {
return next(error);
}
return res.status(200).send({
message: "Success",
data: results,
});
});
};

exports.login = (req, res, next) => {
const { username, password } = req.body;

userServices.login({ username, password }, (error, results) => {
if (error) {
return next(error);
}
return res.status(200).send({
message: "Success",
data: results,
});
});
};

exports.userProfile = (req, res, next) => {
return res.status(401).json({ message: "Authorized User!!" });
};


exports.otpLogin = (req, res, next) => {
userServices.createNewOTP(req.body, (error, results) => {
if (error) {
return next(error);
}
return res.status(200).send({
message: "Success",
data: results,
});
});
};

exports.verifyOTP = (req, res, next) => {
userServices.verifyOTP(req.body, (error, results) => {
if (error) {
return next(error);
}
return res.status(200).send({
message: "Success",
data: results,
});
});
};
26 changes: 26 additions & 0 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const jwt = require("jsonwebtoken");

function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]

if (token == null) return res.sendStatus(401);

jwt.verify(token, "process.env.TOKEN_SECRET", (err, user) => {
console.log(err);
if (err) return res.sendStatus(403);
req.user = user;
next();
});
}

function generateAccessToken(username) {
return jwt.sign({ data: username }, "process.env.TOKEN_SECRET", {
expiresIn: "1h",
});
}

module.exports = {
authenticateToken,
generateAccessToken,
};
23 changes: 23 additions & 0 deletions middlewares/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function errorHandler(err, req, res, next) {
if (typeof err === "string") {
// custom application error
return res.status(400).json({ message: err });
}

if (err.name === "ValidationError") {
// mongoose validation error
return res.status(400).json({ message: err.message });
}

if (err.name === "UnauthorizedError") {
// jwt authentication error
return res.status(401).json({ message: "Token not valid" });
}

// default to 500 server error
return res.status(500).json({ message: err.message });
}

module.exports = {
errorHandler,
};

0 comments on commit 235e62c

Please sign in to comment.