-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17d9994
commit 235e62c
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |