How to Handle Password Reset in ExpressJS
Tags: javascript, node, express, webdev
Published: 2021-01-01 01:28:31

---
canonical_url: https://kelvinmwinuka.com/how-to-handle-password-reset-in-expressjs/
---

No authentication system is complete without a password reset feature. I would personally never ship a product that did not have this feature included. It is necessary to provide a way for users to recover access to their accounts/data in case of a lost or forgotten password. In this article, I will be demonstrating how to handle password resets in ExpressJS.

<!-- wp:paragraph -->
<p>In the last 2 articles, I wrote about <a href="https://kelvinmwinuka.com/how-to-set-up-mongoose-with-expressjs/" target="_blank" rel="noreferrer noopener">how to connect ExpressJS application to MongoDB database</a> and <a href="https://kelvinmwinuka.com/how-to-create-registration-authentication-with-express-passportjs/" target="_blank" rel="noreferrer noopener">building a user registration and authentication system</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Both of these articles tie into today's article. We're going to use mongoose and our saved user data to enable password resets.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you've read those articles, or already have your own authentication system, read on. Even if you're using a different tech stack, you may still gain some valuable ideas from this approach.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As always, this project is hosted on <a href="https://github.com/kelvinmwinuka/express-tutorial" target="_blank" rel="noreferrer noopener">Github</a>. Feel free to clone the project to get access to the source code I use in this article.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The password reset flow</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we dive into the code, let's first establish what the password reset flow will look like from the user's perspective and then design the implementation of this flow.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>User's perspective</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>From the user's perspective, the process should go as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Click on the 'Forgot password' link in the login page.</li><li>Redirected to a page which requires an email address.</li><li>Receive password reset link in an email.</li><li>Link redirects to a page that requires a new password and password confirmation.</li><li>After submission,  redirected to the login page with a success message.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Reset system characteristics</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We also need to understand some characteristics of a good password reset system:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Unique password reset link should be generated for the user such that when the user visits the link, they are instantly identified. This means including a unique token in the link.</li><li>Password reset link should have an expiry time (e.g. 2 hours) after which it is no longer valid and cannot be used to reset the password.</li><li>The reset link should expire once the password has been reset to prevent the same link from being used to reset the password several times.</li><li>If the user requests to change password multiple times without following through on the whole process, each generated link should invalidate the previous one. This prevents having multiple active links from which the password can be reset.</li><li>If the user chooses to ignore the password reset link sent to their email, their current credentials should be left intact and valid for future authentication.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Implementation steps</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We now have a clear picture of the reset flow from the user's perspective and the characteristics of a password reset system. Here are the steps we will take in the implementation of this system:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Create a mongoose model called 'PasswordReset' to manage active password reset requests/tokens. The records set here should expire after a specified time period.</li><li>Include the 'Forgot password' link in the login form that leads to a route that contains an email form.</li><li>Once the email is submitted to a post route, check whether a user with the provided email address exists.</li><li>If the user does not exist, redirect back to the email input form and notify the user that no user with provided email was found.</li><li>If the user exists, generate a password reset token and save it to PasswordReset collection in a document that references the user. If there already is a document in this collection associated with this user, update/replace the current document (there can only be one per user).</li><li>Generate a link that includes the password reset token within it, email the link to the user.</li><li>Redirect to the login page with success message prompting the user to check their email address for the reset link.</li><li>Once the user clicks the link, it should lead to a GET route that expects the token as one of the route params.</li><li>Within this route, extract the token and query the PasswordReset collection for this token. If the document is not found, alert the user that the link is invalid/expired.</li><li>If the document is found, load a form to reset the password. The form should have 2 fields (new password &amp; confirm password fields).</li><li>When the form is submitted, its post route will update the user's password to the new password.</li><li>Delete the password reset document associated with this user in the PasswordReset collection.</li><li>Redirect the user to the login page with a success message.</li></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Implementation</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>The setup</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, we'll have to set up the project. Install the <a href="https://www.npmjs.com/package/uuid" target="_blank" rel="noreferrer noopener">uuid</a> package for generating a unique token, and the <a href="https://www.npmjs.com/package/nodemailer" target="_blank" rel="noreferrer noopener">nodemailer</a> package for sending emails.</p>
<!-- /wp:paragraph -->

`npm install uuid nodemailer`

Add the full domain to the environment variables.  We'll need this to generate a link to email to the user.

`DOMAIN=http://localhost:8000`

<!-- wp:paragraph -->
<p>Make some changes to the app entry file in the following areas:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Set 'useCreateIndex' to 'true' in the mongoose connection options. This makes mongoose's default index build use createIndex instead of ensureIndex and prevents MongoDB deprecation warnings.</li><li>Import a new route file that will contain all the reset routes called 'password-reset'. We will create these routes later.</li></ol>
<!-- /wp:list -->

```javascript
const connection = mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true
})

...

app.use('/', require('./routes/password-reset'))
```

<!-- wp:heading {"level":3} -->
<h3>Models</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We need to have a dedicated model to handle the password reset records. In the models folder, create a model called 'PasswordReset' with the following code:</p>
<!-- /wp:paragraph -->

```javascript
const { Schema, model } = require('mongoose')

const schema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  token: {
    type: Schema.Types.String,
    required: true
  }
}, {
  timestamps: true
})

schema.index({ 'updatedAt': 1 }, { expireAfterSeconds: 300 })

const PasswordReset = model('PasswordReset', schema)

module.exports = PasswordReset
```

<!-- wp:paragraph -->
<p>We have two properties in this model, the user that's requested the password reset, and the unique token assigned to the particular request.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Make sure to set the timestamps option to true in order to include 'createdAt' and 'updatedAt' fields in the document.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After defining the schema, create an index on the updatedAt field with an expiry time of 300 seconds (5 minutes). I've set it this low for testing purposes. In production, you can increase this to something more practical like 2 hours. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the User model we created in <a href="https://kelvinmwinuka.com/how-to-set-up-mongoose-with-expressjs/" target="_blank" rel="noreferrer noopener">this article</a> (or the user model you currently have), update the pre save hook to the following:</p>
<!-- /wp:paragraph -->

```javascript
userSchema.pre('save', async function(next){
  if (this.isNew || this.isModified('password')) this.password = await bcrypt.hash(this.password, saltRounds)
  next()
})
```

<!-- wp:paragraph -->
<p>Do this to make sure the password field is hashed whether the document is new or the password field has been changed in an existing document.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Routes</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Create a new file in the route's folder called 'password-reset.js'. This is the file we import in the app entry file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this file, import the User and PasswordReset models. Import the v4 function from the uuid package for token generation.</p>
<!-- /wp:paragraph -->

```javascript
const router  = require('express').Router()
const { User, PasswordReset } = require('../models')
const { v4 } = require('uuid')

/* Create routes here */

module.exports = router
```

Create the first 2 routes. These routes are associated with the form which accepts the user's email address.

```javascript
router.get('/reset', (req, res) => res.render('reset.html'))

router.post('/reset', async (req, res) => {
  /* Flash email address for pre-population in case we redirect back to reset page. */
  req.flash('email', req.body.email)

  /* Check if user with provided email exists. */
  const user = await User.findOne({ email: req.body.email })
  if (!user) {
    req.flash('error', 'User not found')
    return res.redirect('/reset')
  }

  /* Create a password reset token and save in collection along with the user. 
     If there already is a record with current user, replace it. */
  const token = v4().toString().replace(/-/g, '')
  PasswordReset.updateOne({ 
    user: user._id 
  }, {
    user: user._id,
    token: token
  }, {
    upsert: true
  })
  .then( updateResponse => {
    /* Send email to user containing password reset link. */
    const resetLink = `${process.env.DOMAIN}/reset-confirm/${token}`
    console.log(resetLink)

    req.flash('success', 'Check your email address for the password reset link!')
    return res.redirect('/login')
  })
  .catch( error => {
    req.flash('error', 'Failed to generate reset link, please try again')
    return res.redirect('/reset')
  })
})
```

<!-- wp:paragraph -->
<p>The first is a GET route to '/reset'. In this route, render the 'reset.html' template. We will create this template later.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The second route is a POST route for '/reset'. This route expects the user's email in the request body. In this route:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Flash email back for pre-population in case we redirect back to the email form.</li><li>Check if the user with the email provided exists. If not, flash an error and redirect back to '/reset'.</li><li>Create a token using v4.</li><li>Update PasswordReset document associated with the current user. Set upsert to true in options to create a new document if there isn't one already.</li><li>If update is successful, mail the link to the user, flash a success message and redirect to the login page.</li><li>If update is unsuccessful, flash an error message and redirect back to the email page.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>At the moment, we're only logging the link to the console. We will implement the email logic later.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Create the 2 routes that come into play when the user visits the link generated link above.</p>
<!-- /wp:paragraph -->

```javascript
router.get('/reset-confirm/:token', async (req, res) => {
  const token = req.params.token
  const passwordReset = await PasswordReset.findOne({ token })
  res.render('reset-confirm.html', { 
    token: token,
    valid: passwordReset ? true : false
  })
})

router.post('/reset-confirm/:token', async (req, res) => {
  const token = req.params.token
  const passwordReset = await PasswordReset.findOne({ token })
  
  /* Update user */
  let user = await User.findOne({ _id: passwordReset.user })
  user.password = req.body.password
  
  user.save().then( async savedUser =>  {
    /* Delete password reset document in collection */
    await PasswordReset.deleteOne({ _id: passwordReset._id })
    /* Redirect to login page with success message */
    req.flash('success', 'Password reset successful')
    res.redirect('/login')
  }).catch( error => {
    /* Redirect back to reset-confirm page */
    req.flash('error', 'Failed to reset password please try again')
    return res.redirect(`/reset-confirm/${token}`)
  })
})
```

<!-- wp:paragraph -->
<p>The first route is a get route that expects the token in the url. The token is extracted and then validated. Validate the token by searching the PasswordReset collection for a document with the provided token.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the document is found, set the 'valid' template variable to true, otherwise, set it to false. Be sure to pass the token itself to the template. We will use this in the password reset form.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Check the validity of the token by searching the PasswordReset collection by token.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The second route is a POST route that accepts the password reset form submission. Extract the token from the url and then retrieve the password reset document associated with it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Update the user associated with this particular password reset document. Set the new password and save the updated user.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once the user is updated, delete the password reset document to prevent it from being reused to reset the password.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Flash a success message and redirect the user to the login page where they can log in with their new password.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the update is unsuccessful, flash an error message and redirect back to the same form.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Templates</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once we've created the routes, we need to create the templates</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the views folder, create a 'reset.html' template file with the following content:</p>
<!-- /wp:paragraph -->

```html
{% extends 'base.html' %}

{% set title = 'Reset' %}

{% block styles %}
{% endblock %}

{% block content %}
  <form action='/reset' method="POST">
    {% if messages.error %}
      <div class="alert alert-danger" role="alert">{{ messages.error }}</div>
    {% endif %}
    <div class="mb-3">
      <label for="name" class="form-label">Enter your email address</label>
      <input 
        type="text" 
        class="form-control {% if messages.error %}is-invalid{% endif %}" 
        id="email" 
        name="email"
        value="{{ messages.email or '' }}"
        required>
    </div>
    <div>
      <button type="submit" class="btn btn-primary">Send reset link</button>
    </div>
  </form>
{% endblock %}
```

<!-- wp:paragraph -->
<p>Here we have one email field that is pre-populated with an email value if one was flashed in the previous request.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Include an alert that displays an error message if one has been flashed from the previous request.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Create another template in the same folder named 'reset-confirm.html' with the following content:</p>
<!-- /wp:paragraph -->

```html
{% extends 'base.html' %}

{% set title = 'Confirm Reset' %}

{% block content %}
  {% if not valid %}
    <h1>Oops, looks like this link is expired, try to <a href="/reset">generate another reset link</a></h1>
  {% else %}
    <form action='/reset-confirm/{{ token }}' method="POST">
      {% if messages.error %}
        <div class="alert alert-danger" role="alert">{{ messages.error }}</div>
      {% endif %}
      <div class="mb-3">
        <label for="name" class="form-label">Password</label>
        <input 
          type="password" 
          class="form-control {% if messages.password_error %}is-invalid{% endif %}" 
          id="password" 
          name="password">
        <div class="invalid-feedback">{{ messages.password_error }}</div>
      </div>
      <div class="mb-3">
        <label for="name" class="form-label">Confirm password</label>
        <input 
          type="password" 
          class="form-control {% if messages.confirm_error %}is-invalid{% endif %}" 
          id="confirmPassword" 
          name="confirmPassword">
        <div class="invalid-feedback">{{ messages.confirm_error }}</div>
      </div>
      <div>
        <button type="submit" class="btn btn-primary">Confirm reset</button>
      </div>
    </form>
  {% endif %}
{% endblock %}
```

<!-- wp:paragraph -->
<p>In this form, check for the value of the 'valid' variable that we set in the GET route, if false, render the expired token message. Otherwise, render the password reset form.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Include an alert that displays an error message if one was flashed in the previous request.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Go to the login form that we created in the <a href="https://kelvinmwinuka.com/how-to-create-registration-authentication-with-express-passportjs/" target="_blank" rel="noreferrer noopener">registration &amp; authentication</a> article and add the following code to the top of the form:</p>
<!-- /wp:paragraph -->

```html
{% if messages.success %}
    <div class="alert alert-success" role="alert">{{ messages.success }}</div>
{% endif %}
```

<!-- wp:paragraph -->
<p>This renders the success messages that we flash when we create/send the reset link and when we update the user's password before redirecting to the login page.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Mail</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In the previous routes section, we logged the reset link in the console. Ideally, we should send an email to the user when they've requested a password reset link.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For this example, I've used <a href="https://ethereal.email/" target="_blank" rel="noreferrer noopener">ethereal.email</a> to generate a test email account for development purposes. Head over there and create one (it's a one-click process).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you've created the test account, add the following variables to your environment variables:</p>
<!-- /wp:paragraph -->

```
EMAIL_HOST=smtp.ethereal.email
EMAIL_NAME=Leanne Zulauf
EMAIL_ADDRESS=leanne.zulauf@ethereal.email
EMAIL_PASSWORD=aDhwfMry1h3bbbR9Av
EMAIL_PORT=587
EMAIL_SECURITY=STARTTLS
```

<!-- wp:paragraph -->
<p>These are my values at the time of writing, plug in your own values here.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Create a 'helpers.js' file in the root of the project. This file will have a bunch of useful functions that are likely to be reused across the entire project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Define these functions here so that we can import them when they're needed rather than repeating similar logic all over our application.</p>
<!-- /wp:paragraph -->

```javascript
const nodemailer = require('nodemailer')

module.exports = {
  sendEmail: async ({ to, subject, text }) => {
    /* Create nodemailer transporter using environment variables. */
    const transporter = nodemailer.createTransport({
      host: process.env.EMAIL_HOST,
      port: Number(process.env.EMAIL_PORT),
      auth: {
        user: process.env.EMAIL_ADDRESS,
        pass: process.env.EMAIL_PASSWORD
      }
    })
    /* Send the email */
    let info = await transporter.sendMail({
      from: `"${process.env.EMAIL_NAME}" <${process.env.EMAIL_ADDRESS}>`,
      to,
      subject,
      text
    })
    /* Preview only available when sending through an Ethereal account */
    console.log(`Message preview URL: ${nodemailer.getTestMessageUrl(info)}`)
  }
}
```

<!-- wp:paragraph -->
<p>Export an object with various functions. The first being the 'sendEmail' function. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This function takes the recipient's address, email subject and email text. Create the NodeMailer transporter, using the environment variables defined previously in the options. Send the email using the arguments passed to the function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The last line of the function logs the message url in the console so you can view the message on Ethereal mail. The test account does not actually send the email.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Go back to the 'password-reset.js' routes and add the email functionality. First, import the function:</p>
<!-- /wp:paragraph -->

```javascript
const { sendEmail } = require('../helpers')
```

In the '/reset' POST route, instead of logging the reset link on the console, add the following code:

```javascript
sendEmail({
      to: user.email, 
      subject: 'Password Reset',
      text: `Hi ${user.name}, here's your password reset link: ${resetLink}. 
      If you did not request this link, ignore it.`
    })
```

Send an additional email to notify the user of a successful password change in the '/reset-confirm' POST route once the user is successfully updated:

```javascript
user.save().then( async savedUser =>  {
    /* Delete password reset document in collection */
    await PasswordReset.deleteOne({ _id: passwordReset._id })
    /* Send successful password reset email */
    sendEmail({
      to: user.email, 
      subject: 'Password Reset Successful',
      text: `Congratulations ${user.name}! Your password reset was successful.`
    })
    /* Redirect to login page with success message */
    req.flash('success', 'Password reset successful')
    res.redirect('/login')
  }).catch( error => {
    /* Redirect back to reset-confirm page */
    req.flash('error', 'Failed to reset password please try again')
    return res.redirect(`/reset-confirm/${token}`)
  })
```

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this article, I demonstrated how to implement a password reset feature in ExpressJS using NodeMailer.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the next article, I will write about <a href="https://kelvinmwinuka.com/how-to-verify-users-in-expressjs/" target="_blank" rel="noreferrer noopener">implementing a user email verification system</a> in your Express application. I will use a similar approach to the one used in this article, with NodeMailer being the email package of choice.</p>
<!-- /wp:paragraph -->

The post [How to Handle Password Reset in ExpressJS](https://kelvinmwinuka.com/how-to-handle-password-reset-in-expressjs/) appeared first on [Kelvin Mwinuka](https://kelvinmwinuka.com).

If you enjoyed this article, consider following my [website](https://kelvinmwinuka.com) for early access to my content before it gets published here (don’t worry, it’s still free with no annoying pop-up ads!). Also, feel free to comment on this post. I’d love to hear your thoughts!