Cotter
  • 🚀Getting Started
  • Features & Concepts
    • 💬Sign In with Email/Phone Number
    • 🔐Sign In with Device
      • How it works
    • 🧬Sign In with WebAuthn
  • 📌Quickstart Guides
    • All Guides & Tutorials
    • HTML – Sign in with Email/Phone
    • React – Sign in with Email/Phone
    • React – WebAuthn
    • ▲ Next.js
    • Angular
    • Webflow
    • Bubble.io
    • Python SDK for a CLI
    • React Native – Sign in with Device
    • iOS – Sign in with Device
    • Flutter – Sign in with Device
  • 📘SDK Reference
    • Web
      • Sign In with Email/Phone Number
        • Customize the Form
        • Checking the email or phone before sending a verification code
        • Sending Code or Link via WhatsApp
        • Styling
        • Older SDK
          • Customize the Form
      • Sign in with Social Login
        • Getting Access Tokens from Social Login Providers
        • Github Instructions
        • Google Instructions
      • Sign In with WebAuthn
        • Register WebAuthn for a logged-in user
      • Sign In with Device
        • Steps for Pop Up Authentication Prompt
        • Advanced Customization for Login Form
        • Advanced Customization for Pop Up Authentication Prompt
      • Getting Access Token and Logged-In User Info
      • Sending Successful Form Submission
      • FAQ & Troubleshooting
    • React Native
      • Installation
      • Sign In with Device
        • Add Email/Phone Verification
        • Authenticate from a Non-Trusted Device
        • Add a new Trusted Device
        • Remove Trusted Device
      • Sign In with Email/Phone Number
      • Getting Stored OAuth Tokens and User Information
      • FAQ
      • Older SDK Versions
        • Sign in with Email/Phone
        • Sending Code via WhatsApp
        • Sign In with Device
          • Authenticate from a Non-Trusted Device
          • Add a new Trusted Device
          • Customization
    • Flutter
      • Sign In with Device
        • Add Email/Phone Verification
        • Authenticate from a Non-Trusted Device
      • Sign in with Email/Phone Number
      • Getting the Logged-in User
      • Getting OAuth Tokens
      • Signing a User Out
    • iOS
      • Sign In with Email/Phone Number
      • Sign In with Device
        • Authenticate from a Non-Trusted Device
        • Push Notification
        • Check if Trusted Device is Enrolled
        • Add a New Trusted Device
        • Remove Trusted Device
      • Older Versions
        • Biometric/Pin
    • Android
      • Sign In with Device
        • Authenticate from a Non-Trusted Device
        • Check if Trusted Device is Enrolled
        • Add a new Trusted Device
        • Remove Trusted Device
        • Customization
      • Sign In with Email/Phone Number
      • Biometric/Pin
        • Advanced Methods
        • Customization
        • Setting Strings
        • Styling
      • Older SDK Version
        • Sign In with Device
          • Authenticate from a Non-Trusted Device
    • Python (for CLI)
    • API for Other Mobile Apps or CLI
      • Verify Email/Phone Number
        • Handling URL Scheme
    • Backend: Handling Response
  • 🛡️ Protecting Your Account
    • Only Allow Your Website/App to Use Your API Key
    • Rate Limit
    • Enable reCAPTCHA to Protect Against Automated Abuse
  • 🗝️ Getting Access Token
    • Cotter's OAuth 2.0 Tokens Specification
    • Getting the Tokens
      • Get Tokens during Authentication
      • Using the Refresh Token
    • Storing and Removing Tokens
    • Renewing Expired Tokens
    • Verifying JWT Tokens
    • Requesting Custom Fields on your JWT Token
    • Older API
      • Using HTTP Requests
      • Getting the Tokens
        • During Authentication
          • During Email/Phone Verification
        • During enrolling Trusted Devices
  • 🔌API Reference
    • User API
      • User Object
    • OAuth Tokens API
      • Verify JWT Token using API (serverless)
      • Requesting Custom Claims on your Access Token
      • Older API
    • OAuth Tokens from Social Login
    • Event Object
    • Reset PIN API
  • Older API
    • Validating Cotter's Identity Token
    • Validating Cotter's Event Response
Powered by GitBook
On this page
  • Overview
  • What you're building
  • Required Steps
  • Step 1: Setting Authentication Methods
  • Step 2: Show the Login Form or Popup Prompt
  • Steps for Login Form
  • Step 1: Setup Cotter
  • Step 2: Show Cotter Form
  • What we have so far
  • Step 3: Receive the Response in your OnSuccess function
  • Validate Cotter's Event Response
  • 🎉 You're done!
  • Next Steps
  1. SDK Reference
  2. Web

Sign In with Device

Our JavaScript SDK offers the easiest way to add "Sign in with device" into your website. You can simply call a function and it does most of the heavy lifting and authentication for you.

PreviousRegister WebAuthn for a logged-in userNextSteps for Pop Up Authentication Prompt

Last updated 4 years ago

Concepts: Learn about how works.

There are major updates planned for this feature. so we can help you prepare for it.

Overview

Authenticating users using Trusted Devices with Cotter's JavaScript SDK consists of the following steps:

  1. Embed Cotter in your website

  2. Receive a Callback with user's authentication event and a token from Cotter

  3. Validate the token and proceed with logins or transactions.

Keep in mind that this works if you have a companion app that

What you're building

There are 2 ways to present Cotter's authentication prompt from your website:

  • In the Login form: after entering their email/phone number, if there's a trusted device, it will automatically prompt the user to approve the login from their device.

  • As a popup: When a user triggers an action that requires authentication, like doing a transaction or opening sensitive information, you can open a popup that will ask the user to approve the transaction from their device.

In both cases, you will receive an Event Response (the same event response that you'll receive in mobile app) that you can pass in your backend server during the login/transaction function and check if the user has authenticated and approved the event.

Required Steps

Step 1: Setting Authentication Methods

Remember to set the correct Project in the dropdown list

Step 2: Show the Login Form or Popup Prompt

We will cover the Login Form here, and the Pop Up in the next guide.

Steps for Login Form

Step 1: Setup Cotter

Include Javascript SDK

To use our Javascript SDK, include the script below in your HTML page.

<script src="https://js.cotter.app/lib/cotter.js" type="text/javascript"></script>

Initialize Cotter

Initialize Cotter in your HTML page, below the script you imported above. For React apps, initialize this in index.html. You can use it later in any of your pages.

<script>
  var cotter = new CotterLogin(config);
</script>

Configuration

Here's a basic configuration example:

var config = {
  ApiKeyID: "<your-API-KEY-ID>",
  
  // Type of identity your want to collect: EMAIL or PHONE
  Type: "PHONE",
  
  // div id of the container for Cotter Form
  ContainerID: "cotter-container-login",
  SkipRedirectURL: true,
  
  // the JSON key for the phone number or email to be posted to RedirectURL
  // Read more on RedirectURL JSON Object below
  IdentifierField: "phone",
  
  // OnBegin here is MANDATORY
  // You need to return the Cotter User ID. (If you used client_user_id, 
  // use that instead).
  OnBegin: async payload => {      
    var { userID, err } = await getUserIDFromIdentifier(
      payload.identifier
    );
    return {
      userID: userID,
      err: err
    };
  },
  
  // After the user successfully logged-in, the SDK will return a payload
  // about the login request into this OnSuccess function.
  OnSuccess: payload => {
    console.log(payload)
  },
  
  // (optional) Allow login using OTP sent to email or phone number
  // if user doesn't have Trusted Device set up
  // or if user choose to use OTP instead of trusted device
  // (To enable this, you have to set OTP true in the dashboard)
  AllowOTPFallback: true,
};

// Getting the User ID usnig Cotter's API
const getUserIDFromIdentifier = async (identifier) => {
  try {
    const resp = await fetch(
      `https://www.cotter.app/api/v0/user?identifier=${encodeURIComponent(
        identifier
      )}`,
      { headers: { API_KEY_ID: apiKey } }
    );
    const respJSON = await resp.json();
    const nullUUID = "00000000-0000-0000-0000-000000000000";
    return {
      userID: respJSON.ID === nullUUID ? null : respJSON.client_user_id,
      err: respJSON.ID === nullUUID ? "User not found" : null
    };
  } catch (e) {
    return {
      userID: null,
      err: e.message
    };
  }
};

Advanced Customization

Step 2: Show Cotter Form

To show the cotter form,

1. Add the <div> container.

Put a <div> with the id you specified above as ContainerID inside your page. For example, if your "ContainerID": "cotter-container-login", put this in your html:

<div
  id="cotter-container-login"
  style="width: 300px; height: 300px;"
></div>

2. Show the form

<script>
  cotter.showForm();
</script>

What we have so far

index.html
<script
  src="https://js.cotter.app/lib/cotter.js"
  type="text/javascript"
></script>

<div id="cotter-container-login" style="width: 300px; height: 300px;"></div>

<script>
  var config = {
    ApiKeyID: "API_KEY_ID",
    SkipRedirectURL: true,
    Type: "PHONE", // EMAIL or PHONE
    ContainerID: "cotter-container-login",
    IdentifierField: "phone",
    OnSuccess: payload => {
      console.log(payload)
    },
    AllowOTPFallback: true,
    OnBegin: async payload => {      
      var { userID, err } = await getUserIDFromIdentifier(
        payload.identifier
      );
      return {
        userID: userID,
        err: err
      };
    },
  };
  var cotter = new CotterLogin(config);
  cotter.showForm();
  
  
  // Getting the User ID usnig Cotter's API
  const getUserIDFromIdentifier = async (identifier) => {
    try {
      const resp = await fetch(
        `https://www.cotter.app/api/v0/user?identifier=${encodeURIComponent(
          identifier
        )}`,
        { headers: { API_KEY_ID: apiKey } }
      );
      const respJSON = await resp.json();
      const nullUUID = "00000000-0000-0000-0000-000000000000";
      return {
        userID: respJSON.ID === nullUUID ? null : respJSON.client_user_id,
        err: respJSON.ID === nullUUID ? "User not found" : null
      };
    } catch (e) {
      return {
        userID: null,
        err: e.message
      };
    }
  };
</script>

Step 3: Receive the Response in your OnSuccess function

We will send a JSON Object describing the Authentication Event to your OnSuccess function. The JSON Object will have the following format:

{
  "ID": 2397,
  "CreatedAt": "2020-04-01T05:28:12.540127Z",
  "UpdatedAt": "2020-04-01T05:28:16.599833Z",
  "DeletedAt": null,
  "client_user_id": "<YOUR USER ID>",
  "issuer": "<YOUR API KEY ID>",
  "event": "LOGIN_WEB",
  "ip": "123.347.26.236",
  "location": "San Mateo",
  "timestamp": "1585718892",
  "method": "TRUSTED_DEVICE",
  "new": false,
  "approved": true,
  "signature": "reBafVsmpxO7km5jpRQ3xA..."
}

Validate Cotter's Event Response

Learn how to validate Cotter's event response here:

🎉 You're done!

Next Steps

You need to set allowed methods for authenticating your users. To allow TRUSTED DEVICES (and OTP if you want to allow it as a fallback method), go to

with your API_KEY_ID and some config

in your OnSuccess function the authentication event and a signature from Cotter in your backend server

.

You should in the event response to ensure this is valid and it comes from Cotter's server.

📘
https://dev.cotter.app/rules
Steps for Pop Up Prompt
Check out how you can customize the form and change the configuration
Validating Cotter's Event Response
Steps for Pop Up Authentication Prompt
Advanced Customization for Login Form
Allow Trusted Devices and OTP methods in your dashboard
Show Login Form or Pop Up Prompt
Steps for Login Form
Setup Cotter
Show Cotter form
Receive a Callback
verify the signature
Sign in with Device
Contact us in Slack
allows users to register their device as a Trusted Device.
Cotter's Authentication Request Modal
Allow Trusted Devices (and OTP)
Cotter's Login Form using Trusted Device