Build a password reset form with Vue
In this section, we create a custom password reset form that allows a user to reset their password and includes:
#Example: custom password reset form with Vue.js
You can clone the example password reset form on CodePen(opens new window) and make edits, or follow along below.
#The password reset flow is as follows:
- A user requests a password reset email. Userfront sends the user an email. This email contains a link to reset the user's password.
- The user clicks the link. The link has a special
token
anduuid
in the URL. - The user submits their new password. The user submits their new password with the password reset form on the page. The form updates the user's password and then logs in the user.
The example form has the Userfront Core JS library added to the document, as described in the section below.
AND
The token
and uuid
from the password reset email must be present in the URL. Thus, this example form does not reset an actual password.
#Add the Userfront Core JS library
Start by adding the Userfront Core JS library via CDN or using npm.
You only need to do one of these.
#Requesting a password reset email
In order to request a password reset email, call the sendResetLink() method with the user's email address.
Userfront will email the user with a link to your account's Password reset path.
The link will look something like:
https://www.example.com/reset?
token=
...&
uuid=
...
#Set up the form
Create your password reset form with the elements you want.
In this case, we've added:
password
- required to update the passwordpasswordVerify
- optional, for checking the password before updating it
#Password reset form Vue code
#<form> element
In the <form>
element, we bind our variables password
and passwordVerify
to the inputs using v-model
.
We also set the form to call checkAndResetPassword()
when it is submitted.
#checkAndResetPassword()
When the form is submitted, this function checks that the passwords match and then calls Userfront.resetPassword()
with the current password
value.
#Userfront.resetPassword()
The resetPassword() method allows the user to submit a new password.
Userfront then does the following:
- Verifies the user's credentials
- Updates the user's password
- Adds the user's access token as a cookie named
access.demo1234
- Redirects the page to the After-login path
#Error handling
In this example, we use a <div>{{ alert }}</div>
element to display the alert message.
We first clear the alert message whenever the form is submitted.
If the passwords do not match, we set the alert message and return without submitting the password reset.
Whenever the Userfront.resetPassword()
method fails, we catch
its error in the promise chain. This error contains a message
property with what went wrong, and we set that as the alert message.
#Password verification
Userfront will verify that the password is the correct length and format, and we can additionally verify that the user has typed what they intended by having them type it twice.
This "passwords match" verification is performed before sending the information to Userfront.