Sign In with Email/Phone Number
Our JavaScript SDK offers the easiest way to integrate Cotter 's email/phone verification. You can simply embed Cotter's Login Form and it will do the heavy lifting for you.
Concepts: Learn about how Sign in with Email/Phone Number works.
What you're building
Try it out
Quickly try out how it works in our Hello World Example! 🎉
Overview
Verifying email and phone number in your website using our JavaScript SDK consists of the following steps:
Embed Cotter in your website
Receive a Callback with user's data and a
token
from CotterSend the
token
to your backend server
Steps
Setup Cotter with your
API_KEY_ID
and some configSend the Payload containing the user's information to your backend server
Step 1: Setup Cotter
Include Javascript SDK
To use our Javascript SDK, include the script below in your HTML page or use the npm package.
Make sure you check for the latest version at https://www.npmjs.com/package/cotter
Initialize Cotter
Initialize Cotter in your Login page. If using HTML, put this script at the bottom of your <body>
. If you're using React, put this in useEffect
or componentDidMount
. We want this script to run right after the page is loaded.
Adding more fields
You can also add more fields, customize the styles, and intercept the authentication request before it's sent. Check out how to Customize the Form.
Send Code via WhatsApp
Instead of using SMS, you can also send the code via WhatsApp. Go to the Dashboard > Branding and chose "Phone" on top of the preview.
To send code/link via SMS or WhatsApp, you'll need to add some balance to you project in the Dashboard.
Step 2: Show Cotter Form
Add the <div>
container with id "cotter-form-container"
What we have so far
Step 3: Sending the Payload to your Backend Server
You can get the authentication response in the then
callback function and send it to your server. For example:
The payload
that you receive from the promise is a JSON Object with the following format:
Please use the identifier (email/phone number) as your main way to identify users, user.ID is deprecated.
You should verify the access token to ensure this is valid and it comes from Cotter's server.
Handling the response in your backend
Backend: Handling Response🎉 You're done!
Securing your Project
Since you'll be using your API Key from a front-end website or mobile app, your API_KEY_ID
is exposed to anyone inspecting your code. Here are some ways to prevent abuse:
What's Next
Styling the Form: You can add Styling from the dashboard or add custom CSS
Add Additional Fields: Add fields like Name, Address, etc to the login form
Check the email/phone before logging in: Useful for employees-only portals, RSVP, waitlists, checking if the user is registered, etc.
Last updated