This repository was archived by the owner on Oct 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Forgot password and Reset password feature #112
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d162cd
add controller for forgot password and reset password
iamrahulrnair 352b36b
add password change specific route
iamrahulrnair 15af4f0
add instance method
iamrahulrnair 54d4e81
add route for password change specific routers
iamrahulrnair fbcd943
Update pwd.controllers.js
iamrahulrnair cd5bc90
Update server.js
iamrahulrnair e84a0a2
Update pwd.route.js
iamrahulrnair eb74a96
Create ratelimiter.middleware.js
iamrahulrnair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
| const asyncHandler = require('express-async-handler'); | ||
| const User = require('../models/user.modal'); | ||
| const nodemailer = require('nodemailer'); | ||
| const jwt = require('jsonwebtoken'); | ||
| const bcrypt = require('bcryptjs'); | ||
|
|
||
| module.exports.forgotPwdController = asyncHandler(async (req, res, next) => { | ||
| const { email } = req.body; | ||
| const user = await User.findOne({ email }); | ||
|
|
||
| if (!user) { | ||
| res.status(400).send({ status: 'user not found' }); | ||
| throw new Error('User not found'); | ||
| } | ||
|
|
||
| const token = await user.createPWDresetToken(); | ||
| const resetURL = `${req.protocol}://${req.get( | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'host' | ||
| )}/api/_reset_password/${token}`; | ||
|
|
||
| var transporter = nodemailer.createTransport({ | ||
| service: 'gmail', | ||
| auth: { | ||
| user: process.env.OUR_EMAIL, | ||
| pass: process.env.EMAIL_PASS, | ||
| }, | ||
| from: 'codesync.live', | ||
| }); | ||
|
|
||
| var mailOptions = { | ||
| from: process.env.OUR_EMAIL, | ||
| to: user.email, | ||
| cc: 'codedeeper.work@gmail.com', | ||
| subject: 'Codedeeper password reset Token', | ||
| html: `<p>Hello , ${user.name} here is your token link. <a href="${resetURL}">${resetURL}</a> to reset the password</p>`, | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| text: `<p>Hello , ${user.name} here is your token link. <a href="${resetURL}">${resetURL}</a> to reset the password</p>`, | ||
| }; | ||
|
|
||
| // transporter.sendMail(mailOptions, function (error, info) { | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // if (error) { | ||
| // console.log(error); | ||
| // res.status(200).send({ | ||
| // status: 'failed', | ||
| // }); | ||
| // } else { | ||
| // console.log('Email sent: ' + info.response); | ||
| // if (process.env.NODE_ENV == 'development') { | ||
| // res.status(200).send({ | ||
| // status: 'success', | ||
| // resetURL, | ||
| // }); | ||
| // } else { | ||
| // res.status(200); | ||
| // } | ||
| // } | ||
| // }); | ||
|
|
||
| /** | ||
| * @TODO: check nodemailer authentication and | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * test this in development before building for production | ||
| */ | ||
| res.status(200).send({ | ||
| resetURL, | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
|
|
||
| module.exports.resetPasswordController = asyncHandler(async (req, res) => { | ||
| const reset_token = req.params.JWT_TOKEN; | ||
| try { | ||
| const payload = jwt.verify(reset_token, process.env.JWT_SECRET_KEY); | ||
| const { password, confirmpassword } = req.body; | ||
| if (!password || !confirmpassword) | ||
| throw new Error('Password should be provided!'); | ||
|
|
||
| if (password !== confirmpassword) throw new Error("Password doesn't match"); | ||
|
|
||
| const user = await User.findById(payload.id); | ||
| if (!user) throw new Error('No user exists with this id'); | ||
|
|
||
| if (user.passwordChangedAt && user.passwordChangedAt > payload.iat * 1000) | ||
| throw new Error( | ||
| 'Please raise a new Reset Request,this token was used earlier' | ||
| ); | ||
|
|
||
| const newPassword = await bcrypt.hash(password, 12); | ||
| user.set({ password: newPassword, passwordChangedAt: new Date() }); | ||
| await user.save(); | ||
|
|
||
| return res.send({ | ||
| payload, | ||
codewithvk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| user, | ||
| elapsed_time: payload.exp - parseInt(Date.now() / 1000), | ||
| }); | ||
| } catch (err) { | ||
| res.status(400).send({ | ||
| err: err.message, | ||
| }); | ||
| } | ||
| }); | ||
This file contains hidden or 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,29 +1,39 @@ | ||
| const mongoose = require("mongoose"); | ||
| const mongoose = require('mongoose'); | ||
| const jwt = require('jsonwebtoken'); | ||
|
|
||
| const User = mongoose.model("User", { | ||
| name: { | ||
| type: "String", | ||
| required: true, | ||
| }, | ||
| email: { | ||
| type: "String", | ||
| required: true, | ||
| }, | ||
| password: { | ||
| type: "String", | ||
| required: true, | ||
| }, | ||
| id: { | ||
| type: "String", | ||
| }, | ||
| googleLogin: { | ||
| type: "Boolean", | ||
| default: false, | ||
| }, | ||
| imageUrl: { | ||
| type: "String", | ||
| } | ||
| }); | ||
| const userSchema = new mongoose.Schema({ | ||
| name: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| email: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| password: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| passwordChangedAt: Date, | ||
| id: { | ||
| type: String, | ||
| }, | ||
| googleLogin: { | ||
| type: Boolean, | ||
| default: false, | ||
| }, | ||
| imageUrl: { | ||
| type: String, | ||
| }, | ||
| }); | ||
|
|
||
| userSchema.methods.createPWDresetToken = async function () { | ||
| return jwt.sign( | ||
| { email: this.email, id: this._id }, | ||
| process.env.JWT_SECRET_KEY, | ||
| { expiresIn: 600 } | ||
| ); | ||
| }; | ||
|
|
||
| const User = mongoose.model('User', userSchema); | ||
| module.exports = User; |
This file contains hidden or 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,12 @@ | ||
| const express = require('express'); | ||
| const { | ||
| forgotPwdController, | ||
| resetPasswordController, | ||
| } = require('../controllers/pwd.controllers'); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get('/_reset_password/:JWT_TOKEN', resetPasswordController); | ||
| router.post('/_forgot_password', forgotPwdController); | ||
|
|
||
| module.exports = router; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.