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
  • Steps
  • Step 1. Import Cotter as dependency
  • Step 2. Initialize Cotter with your API Key
  • Step 3. Call Cotter's login function
  • Step 4: Receive Token
  • Validating Cotter's Access Token
  • 🎉 You're done!
  • Securing your Project
  1. SDK Reference
  2. iOS

Sign In with Email/Phone Number

Our iOS SDK offers the easiest way to verify email/phone numbers in your Swift app. You can simply call a function and it does most of the heavy lifting and authentication for you.

PreviousiOSNextSign In with Device

Last updated 4 years ago

Concepts: Learn about how works.

Overview

Verifying email and phone number in your mobile app using our iOS SDK consists of the following steps:

  1. Import Cotter

  2. Call Cotter's Login function

  3. Receive user's email or phone number, and whether or not it's verified

What you're building

Steps

Step 1. Import Cotter as dependency

We use Cocoapods as our SDK host. If you're using Cocoapods, add this to your Podfile

pod 'Cotter'

Then simply run pod install

Step 2. Initialize Cotter with your API Key

You will have to do import Cotter on the file that will use Cotter. Then do initialization as follows

import Cotter

...

let cotter = Cotter(
    apiSecretKey: <your-api-secret-key>,
    apiKeyID: <your-api-key-id>,
    cotterURL: "https://www.cotter.app/api/v0",
    userID: <your-user-id>, // user's id that will be created later
    configuration: <your-cotter-config>
)

example:

import Cotter

...

let cotter = Cotter(
    apiSecretKey: "<API_SECRET_KEY>",
    apiKeyID: "<API_KEY_ID>",
    cotterURL: "https://www.cotter.app/api/v0",
    userID: "hello@example.com",
    configuration: [:]
  );

Step 3. Call Cotter's login function

cotter.startPasswordlessLogin(
            parentView: <your-view-controller>,
            input: <your-input-text>,
            identifierField: <identifier>,
            type: <PHONE-or-EMAIL>,
            directLogin: <true-or-false>,
            cb: <your-callback>
        )

example:

@available(iOS 12.0, *)
class LoginViewController: UIViewController {
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var phoneInput: UITextField!

    ...

    @IBAction func login(_ sender: Any) {
        // get the text input
        let textInput = self.phoneInput.text ?? ""

        func authCb(accessToken: String, error: Error?) -> Void{
            guard let error = error else {
                print("error logging in!")
                return
            }

            // error handling
            print("success!")
        }

        let cotter = Cotter(
          apiSecretKey: "<API_SECRET_KEY>",
          apiKeyID: "<API_KEY_ID>",
          cotterURL: "https://www.cotter.app/api/v0",
          userID: "hello@example.com",
          configuration: [:]
        );

        cotter.startPasswordlessLogin(
            parentView: self,
            input: textInput,
            identifierField: "phone",
            type: "PHONE",
            directLogin: true,
            cb: authCb
        )
    }
}

To login and enter email or phone number in Cotter's window, simply set the directLogin to false and set the input to empty string

cotter.startPasswordlessLogin(
    parentView: self,
    input: "",
    identifierField: "phone",
    type: "PHONE",
    directLogin: false,
    cb: authCb
)

Step 4: Receive Token

The token will be received in the callback function. The token will be in the form as the following:

"token": {
  "identifier": "+12345678910",
  "identifier_type": "PHONE",
  "receiver": "<your API_KEY_ID>",
  "expire_at": "1584687591",
  "signature": "G8dOKR6qLj+GiB0pD2aggVVdYddFoyy..."
}

The token contains the user's phone number, your API_KEY_ID in the receiver field, and a signature to ensure this is from Cotter. The token tells you that this identifier is verified.

Validating Cotter's Access Token

Checkout how to verify the OAuth Tokens from Cotter here:

🎉 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:

To send code/link via SMS or WhatsApp, you'll need to add some balance to you project in the .

You should include this JSON Object into your call to your backend for Login or Registration. Your backend should then verify that the is valid.

📘
Import Cotter as dependency
Initialize Cotter with your API Key
Call Cotter's Login Function
Receive Token
Dashboard
signature of the token
Verifying JWT Tokens
Only allow your website/app to use your API Key
Rate Limit the number of authentication requests
Enable reCAPTCHA to prevent automated abuse
Sign in with Email/Phone Number
Cotter's iOS SDK