Headless Email Signup
Introduction
This guide will help you let a user signup/login with email, without any of our UI components. We’ve split the guide into two tabs as you’ll see below. The first uses our SDK hooks (but none of the UI components) - we recommend this choice if you can. The second tab shows you how to handle email login without any SDK interaction at all i.e. API only.
Tutorial
Configuring the SDK
Let’s start by installing our packages. Follow our Quickstart guide for a detailed walkthrough.
When you’re done, we’ll update our App.js to include our environment ID in the setup and import the DynamicContextProvider
and EthereumWalletConnectors
.
You app.js file should look like this (note that we are only importing the EthereumWalletConnectors for now):
useConnectWithOtp
All we will need for this use case is the useConnectWithOtp hook. This exposes multiple methods, and we are interested primarily in the following:
- connectWithEmail
- verifyOneTimePassword
Once you have those available in your component, the rest is as simple as building your form!
Code Example
Configuring the SDK
Let’s start by installing our packages. Follow our Quickstart guide for a detailed walkthrough.
When you’re done, we’ll update our App.js to include our environment ID in the setup and import the DynamicContextProvider
and EthereumWalletConnectors
.
You app.js file should look like this (note that we are only importing the EthereumWalletConnectors for now):
useConnectWithOtp
All we will need for this use case is the useConnectWithOtp hook. This exposes multiple methods, and we are interested primarily in the following:
- connectWithEmail
- verifyOneTimePassword
Once you have those available in your component, the rest is as simple as building your form!
Code Example
Here we follow three simple steps, creating the email verification, verifying the OTP, and getting the JWT.
Basic UI
First let’s create a basic React component to help us handle the UI:
We’re creating a few state variables to help us keep track of the user’s email, the OTP, the verificationUUID, and the JWT. We’re also creating a couple of (currently empty) functions to handle the email verification and the OTP verification.
You might be wondering what the UUID variable is for. When you create an email verification, we return a verificationUUID that you must pass into the API along with the OTP itself so that it’s correctly verified.
Lastly but most importantly, there’s a variable called DYNAMIC_ENVIRONMENT_ID. We will need this to populate the URLs for the API calls. You can find your environment ID in the Dynamic dashboard.
OK, let’s fill in the empty functions now, going step by step:
Create an email verification
You will be interacting with the emailVerification endpoint for this. This will send an email to the user with a One Time Password so they can verify their email address.
You can find the full reference for the endpoint in the link above, but the main thing you need to pass is the email address of the user. We’ll edit our sendEmailVerification
function like so:
Verify the OTP
With the above step complete, as long as the email address is valid, the user will receive an email with an OTP. They will enter the OTP in the UI, and we will verify it once they hit the “Verify” button.
To verify the OTP, the next call will be to signIn endpoint, which you can find here. You will need to pass the OTP from the email and the verificationUUID from the previous response.
Let’s fill in our verify
function with that in mind:
Get the JWT
At this point, you’ll have a JWT returned from the verify call and saved in state as the jwt variable. This JWT will be tied to a session in Dynamic, and we return details of the user from that JWT.
Learn more about the JWT object here.
Summary
That’s it! You now have a completely headless sign-in with email, using Dynamic OTP.
Was this page helpful?