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
  • What you're building
  • Key Features
  • Overview
  • Steps
  • Setting Up Social Login Provider
  • Receiving the User Response after successful login
  • How it works
  • Connecting a Social Account to an existing user
  • Getting Access Tokens from the Social Login Provider
  • UI Considerations
  1. SDK Reference
  2. Web

Sign in with Social Login

Sign in with Social Login allows you to authenticate users using their social accounts like Google, Github, etc.

PreviousCustomize the FormNextGetting Access Tokens from Social Login Providers

Last updated 4 years ago

What you're building

Allow users to sign in with a Social Account, without getting duplicate accounts. With Cotter, we automatically link the user's existing account with their Social Account.

Key Features

  • Simple integration, no additional code.

  • Auto-linking with existing accounts, users can either log in with their email or with Github and it will go to the same user account.

  • Connect Social Provider Account from inside your Settings page to allow easy API integrations. (For example, a user never uses the "Sign in with Github", but you need their Github access token to enable deployment: you can ask them to press a button to connect their Github account).

Overview

2. You can then add options for the user to sign in using a Social Login Provider in the Dashboard.

Available Providers

  • Github

  • Google

Steps

Setting Up Social Login Provider

Make sure you are using the JS Package version >= 0.3.16

4. That's it, you should now see the "Sign in with Github" button below the email/phone number input.

Receiving the User Response after successful login

var cotter = new Cotter(API_KEY_ID); // Specify your API KEY ID here
cotter
  .withFormID("form_default") // Use customization for form "form_default"
  .signInWithLink()
  .showEmailForm()
  .then((response) => {
    setpayload(response); // 👈 You'll receive the response here, as usual
  })
  .catch((err) => console.log(err));

How it works

  • Case 1 – If a user with Github username user1 has logged-in before and already associated with a user (userID: 123), then the user can automatically log in.

Connecting a Social Account to an existing user

You can alternatively connect the user's Github/Google account when the user is already logged in. For example, your website may have a Settings page, and the user can press a button "Connect Github Login" to allow them to connect their Github Account and use it to log in.

Add a button on your website, and call this function to connect the user's Github Account with the currently logged in user.

const connectToGithub = async () => {
  var cotter = new Cotter(apiKey)
  const accessToken = await cotter.tokenHandler.getAccessToken()
  cotter.connectSocialLogin("GITHUB", accessToken?.token) // pass in the provider's name
}

Getting Access Tokens from the Social Login Provider

When your users sign in with Github, Github returns an access_token that you can use to call their API. We store this information for you. To get the access_token for each user, follow the instructions below:

UI Considerations

Logging-in to social providers requires us to redirect the user to the provider's page to login when necessary. When the login process is done, the users will be redirected back to the page where you embedded the form:

For example:

  • Login Page that contains the form: example.com/login

    • User login to Github: redirect to github.com/oauth/....

    • User finish login: redirect back to example.com/login?code=xyz&state=abc

As you can see, we pass in a code and state and other parameters when redirecting back to your login page. Cotter's Login Form automatically handles this query parameters for you and will resolve the Promise with the usual Cotter user information response.

With that said, make sure your UI takes into account that the user will be redirected away and then back to your login page.

To enable this feature, you need to first configure as the primary login method.

Integrate Cotter's Login Form following as the primary login method.

.

that didn't use Sign in with a Social Provider from a button.

to use their API.

You will need to create an "App" for the Social Login Provider. Follow the instructions for each login provider. You will need to enter the credentials to > Social Login

Go to > Project > Form & Fields, check the login provider that you want to enable under "Social Login Providers" and press Save.

You will receive responses the same way as , inside the then callback that you provided.

If a user signed-in with Github and the Github username is user1 and the Github email is :

Case 2 – If a user with email doesn't exist: It will create a new user with email , and the user can log in both with their Github account or with email using a magic link or OTP.

Case 3 – If a user with email already exists: It will ask the user if they want to link the account for with the Github account. If they agree, the user can always login both with their Github account or with email using a magic link or OTP.

📘
Sign in with Email/Phone Number
Sign In with Email/Phone Number
Let us know what you need
Sign in with Email/Phone Number
Dashboard
Github Instructions
Google Instructions
Dashboard
Sign in with email/phone
user1@gmail.com
user1@gmail.com
user1@gmail.com
user1@gmail.com
user1@gmail.com
user1@gmail.com
user1@gmail.com
Getting Access Tokens from Social Login Providers
Set up your Social Login
Connecting a Social Account to an existing user
Getting the Access Token from the Social Provider
Sign in with Github and Google using Cotter's SDK
Case 3: Prompt for the user to connect their existing account with the Github account