Passwordless Authentication with React and Auth0

Editor’s Note: For years now, I’ve been telling people that passwords are obsolete. When I wrote the password section for “Programming JavaScript Applications”, I strongly recommended password alternatives such as multi-factor authentication. In the time since, there have been many breaches that revealed millions upon millions of usernames and passwords. Chances are good that some of your passwords have been stolen.
Once in a while we find products or services that we highly recommend and invite the company behind them to participate in a sponsored post.
That’s why I’m very excited to bring you this sponsored post for Auth0 written by Ryan Chenkie. I strongly suggest you read it and incorporate passwordless authentication in your next app.
~ Eric Elliott
Learn how going password-free can benefit your users and your organization.
In today’s article we will be building a React application with passwordless authentication through Auth0. For brevity, we will use the Auth0 React Starter as the foundation for our application. We’ll examine the benefits of passwordless authentication and see how it can provide better security over traditional authentication methods.
What is Passwordless all About?
Passwordless authentication, as the name might imply, is an authentication system where the user does not enter a password. Instead, they provide an identifier such as an email address or telephone number, and the authentication system sends out a one-time passcode or link to this identifier that the user must then provide or click to successfully authenticate. The passcode or link is active for a limited time, often around five minutes. Each login request generates a new passcode or link and invalidates any previous passcodes or links. In a sense, passwordless authentication also provides two-factor authentication out-of-the-box, since a user must have access to either the email account or phone number they provide.
Passwordless authentication is beneficial for both users and developers. Users tend to choose weak passwords and reuse the same password for multiple services. From this, we can deduce that users tend to value convenience over security. Many developers make the logical decision to enforce stricter password requirements, but that often leads to user annoyance. From a developer standpoint, passwords have to be safely encrypted and stored, and even if all the best practices are followed, a user that reuses the password on a third party service that gets compromised is also vulnerable in your application.
Who is Using Passwordless?
Many companies have already embraced passwordless authentication including Medium, Slack, and WhatsApp. Passwordless authentication can be found in both web and mobile applications and is especially well-suited for mobile, as the user is commonly on their device which will receive the one-time passcode, making for a very intuitive workflow.
What We’ll Build
In this article, we’re going to get an introduction to passwordless and see how it provides an intuitive and secure workflow for user authentication. We’ll cover how passwordless authentication works, the benefits it provides, and to put theory into practice, we’ll implement passwordless authentication in a React application.
Going Passwordless with React and Auth0
To demonstrate how this all works, we’ll build a passwordless React application that uses Auth0’s Passwordless library. To get started, sign up for a free Auth0 account and check out the management dashboard. We’ll use the React seed project so we can save some time as we get going. It’s assumed that you have some basic knowledge of React, as we’ll be skipping over basic React idioms.
The seed project gives us a good starting point for our application. We have basic routing, a couple of components, some helpers, and most importantly the build process in place to transpile and serve our code. Run `npm install` to get the project dependencies. To make sure the seed project works with traditional authentication, edit the `.env` file with your Auth0 credentials:
cp .env.example .envLaunch the app by running `npm start`. Next, navigate to `localhost:3000` to see the app in action. You should be able to login/register by providing an email and password.

Passwordless Authentication Service
Now let’s implement passwordless authentication. Stop the application and run:
npm install auth0-lock-passwordless --saveThis will pull down the Auth0 Passwordless library and save it in our `package.json` file. Next, open `AuthService.js` located in the utils directory. This is where the bulk of our passwordless implementation will take place.
Feel free to strip out all of the existing code here — we’ll reimplement the functionality and explain it line by line.













