Skip to content

Web-SDK

Installation

Setup

SDK exposes a lot of methods & values. However just the two methods is all you would need for a simple implementation: decide & exchange. You need to start by importing the adapter for your framework.

ts
// React: exposes `useAuthState` hook
// import { createWebviewRedirect } from '@trustycars/web-auth/redirect/react'

// Vanilla: exposes core functionalities
import { WebviewRedirect } from '@trustycars/web-auth/redirect'

const Instance = new WebviewRedirect({
  client_id,
  oauth_webview_url,
  auth_service_url,
  app_service_url
})

export const { decide, exchange, state } = Instance

Read the full API on the package documentation.

Instance Options

  • client_id: This is your app's service ID, you can get it from the Auth Dashboard.
  • app_service_url: Your app's backend service.
  • oauth_webview_url: URL of your authenticator. SSO Webview in this case.
  • auth_service_url: URL of Auth service. Auth Server in this case.

General implementation

  1. On the non-anonymous routes, invoke decide. It will:
  • Checks if user already logged in from token existence.
  • If yes, will try to validate the token to make sure it's still valid.
  • If no, redirect will happen.
  • If existing token is invalid/outdated kick out the session. - Configurable
  1. After the authentication success, webview will redirect back to your application's callback url(by default /authorized - Configurable).
  • You'll need to create a success callback route, if you didn't configured this use /authorized.
  • Invoke exchange on that callback route. After that Instance will hold the exchanged token and other states.
  • You may watch is_alive of the state reactively and continue your workflow once it turns true.