DEV Community

Cover image for Forgot password & Password reset flow in node.js

Forgot password & Password reset flow in node.js

CyberWolves on May 23, 2021

Hi guys today we gonna implement password reset via email in node.js. If you user forgot there password, we send an link to you user email account....
Collapse
 
mwafrika profile image
Mwafrika Josué

Thank you for this amazing tutorial, After watching it carefully I noticed a small bug that you can work on later. After reseting the password, The plain password is being kept in the database. I think you should concider encrypting the password before saving to the database after reseting it

Collapse
 
subhashoos profile image
subhash-oos

no you are wrong thats not a bug please try to see this schema we have no need to encrypt
const express =require("express")
const mongoose=require("mongoose")
const bcrypt = require('bcryptjs');
const schema=new mongoose.Schema({
firstName: {
type: String,
// required: true,
},
lastName: {
type: String,
// required: true,
},
image: {
type: String,
},
email:{
type:String,
required: true,
},
status: {
type: String,
enum: ['Pending', 'Active'],
default: 'Pending'
},
confirmationCode: {
type: String,
unique: true
},
password:{
type:String,
required: true,
},
phone:{
type:String,
// required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
phoneOtp:String
},
{ timestamps: true }
)
schema.pre('save', async function(next) {
try {
// check method of registration
const user = this;
if (!user.isModified('password')) next();
// generate salt
const salt = await bcrypt.genSalt(10);
// hash the password
const hashedPassword = await bcrypt.hash(this.password, salt);
// replace plain text password with hashed password
this.password = hashedPassword;
next();
} catch (error) {
return next(error);
}
});
schema.methods.matchPassword = async function (password) {
console.log(password)
try {
return await bcrypt.compare(password, this.password);
} catch (error) {
throw new Error(error);
}
};
const mens=new mongoose.model("APJ_Ecomusers",schema)
module.exports=mens

Collapse
 
stylespriley profile image
StylesPRiley

Getting this error message:
Error: connect ECONNREFUSED 127.0.0.1:587
at TCPConnectWrap.afterConnect as oncomplete {
errno: -4078,
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 587,
command: 'CONN'
} email not sent

Collapse
 
tarun080 profile image
Tarun Purohit

How to solve this error.

Collapse
 
tarun080 profile image
Tarun Purohit

@cyberwolves can u show the solution for this error

Collapse
 
tarun080 profile image
Tarun Purohit

@stylespriley if u got the solution please share. Facing the same issue here

Collapse
 
mdirshaddev profile image
Md Irshad

Awesome Article I am new to this Web Development. Just by reading this i was able to understand the flow. Thank You. Keep writing such articles.

Collapse
 
cyberwolves profile image
CyberWolves

Thank you, subscribe to my Youtube channel youtube.com/channel/UCxyo2h1uAuMT1...

Collapse
 
adesoji1 profile image
Adesoji1

Thanks, i did something similar to this using react,postgres and express.js

Collapse
 
himanshupal0001 profile image
Himanshupal0001

Can you provide the music name that you used in your video.

Collapse
 
s0fy4n002 profile image
yayan

nice share gan

iam from indonesia

Collapse
 
mohit1607 profile image
mohit1607

Arigato gozaimasu.. senpai

Collapse
 
kyzsu profile image
kyzsu

thanks mate, works fine!

Collapse
 
chadrackkyungu profile image
Chadrack kyungu

Thank you so much your blogs are amazing

Collapse
 
zany49 profile image
abdul kalam

UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "User"
please help me

Collapse
 
okumujustine profile image
okumujustine

Nice

Collapse
 
brave profile image
Brave Normakoh

This is really helpful. Thanks.

Collapse
 
stylespriley profile image
StylesPRiley

This doesn't work emails getting blocked!

Collapse
 
floatingnahid22 profile image
Nahid Hossain

did you solve this issue?

Collapse
 
stylespriley profile image
StylesPRiley

Can you please explain the .env variables?
HOST = // email host
USER = // email id
PASS = // email password
SERVICE = // email service
BASE_URL = "localhost:8080/api"

Collapse
 
loucapo profile image
Lou Capozzoli

did you ever figure this out?

Collapse
 
dhanurudra profile image
R Dhanusha

in the postman while posting ,

localhost:8080/api/password-reset/...

Here whats the token i have to enter