Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

robot mascot smiling

Forgot Password & Password Reset

In this lesson, we'll walk through setting up the complete forgot password flow including, creating a password reset token with time-expiry, sending an email notification with a password reset link, verifying the token, and resetting the users password.

Published
Oct 21
Duration
29m 42s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Chapters

00:00 - Creating Our Forgot Password Page & Form
03:32 - Defining Our Forgot Password Routes
04:50 - Password Reset Send Validator
05:40 - Try Send Password Reset Email Action
08:36 - Expire Password Reset Tokens Action
09:54 - Continuing the Try Send Action
12:30 - Installing & Configuring AdonisJS Mail
14:38 - Sending Our Password Reset Email
17:14 - Forgot Password Send Route Handler
17:39 - Forgot Password Index Route Handler
19:13 - Testing Our Forgot Password Flow
19:46 - Creating Our Reset Password Page & Form
22:18 - Reset Password Route Handler
22:44 - Verify Password Reset Token Action
24:35 - Continuing Our Reset Password Handler
25:28 - Password Reset Validator
26:30 - Reset Password Action
27:27 - Reset Password Update Route Handler
28:38 - Testing Our Reset Password Flow

Forgot Password Email

You can find the complete forgot password email from this lesson, on GitHub. This email was created using maily.to.

Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

Join The Discussion! (4 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 25 days ago

    Hello Tom,
    First of all, thank you for all this amazing work.
    I have an warning that I can't fix; in my console, I see the warning message below, even though I do have an authenticated user.
    Warning message:
    [Vue warn]: Invalid prop: type check failed for prop "user". Expected Object, got Undefined
    at <AppLayout user=undefined errors= {} exceptions= {} ... >
    at <Inertia initialPage= {
    component: 'home',
    version: '1',
    props: {
    user: undefined,
    errors: {},
    exceptions: {},
    messages: {},
    version: 6
    },
    If I do a console log of user, I indeed get undefined:
    sharedData: {
    user: async (ctx) => {
    const user = ctx.auth.use('web').user
    console.log(user)
    return user && new UserDto(user)
    },
    However, if I retrieve the user this way:
    const user = await ctx.auth.use('web').authenticate()
    I do have my user connected. On the other hand, if I log out and return to the login page, I get the message: "The page isn't redirecting properly."
    Do you have any idea what the problem is?

    1

    Please sign in or sign up for free to reply

    1. Commented 25 days ago

      For the authenticated user to be populated you must inform AdonisJS to check for it. This saves the roundtrip to populate the user in cases where it isn't needed.

      To populate the user, you have two options

      1. authenticate - Requires an authenticated user. If an authenticated user is not found, an exception is thrown.

      2. check - Will check to see if a user is authenticated, and populate that user if so. The request goes on as usual if an authenticated user is not found.

      In terms of middleware, you have three options

      1. auth - Will call authenticate and redirect the user to the login page by default if an authenticated user is not found.

      2. guest - Will call check and redirect the user, I believe to the home page, by default if an authenticated user is found.

      3. silent_auth - Will call check and progress with the request as usual.

      So, you're most likely getting "the page isn't redirecting properly" because your authenticate on the login page is attempting to redirect the user to the login page, resulting in an infinite redirect loop.

      You most likely will want to replace your authenticate on the login page with the guest middleware and that should fix the redirect loop. Then, for internal pages, you can either use authenticate if the user must be authenticated to access the page, check if the user may or may not be authenticated to access the page, or one of the corresponding middleware.

      1

      Please sign in or sign up for free to reply

      1. Commented 25 days ago

        Thank you for this quick response. So if I understand correctly, at the current stage of the course (5.6), it's normal to receive this warning message since our 'home' route doesn't have any middleware.

        1

        Please sign in or sign up for free to reply

        1. Commented 25 days ago

          Anytime! Yes, sorry, we'll move our home page into our group protected by the auth middleware in the next lesson (6.0). So, that warning specifically on the home page will go away in the next lesson.

          1

          Please sign in or sign up for free to reply