Skip to content

Auth Client

The Feathers Cloud Auth client in the @featherscloud/auth package is used to manage device identities and access tokens as well as verifying them.

Installation

It can be installed with

sh
npm i @featherscloud/auth
pnpm add @featherscloud/auth
yarn add @featherscloud/auth
bun add @featherscloud/auth

Initialization

In a web application using a module loader and with any framework, the client can be initialized like this:

ts
import { LoginRequiredError, createClient } from '@featherscloud/auth'

const appId = '<your-app-id>'
const auth = createClient({ appId })

Usage

It can then be used to make authenticated requests to a server running one of the supported platforms like this:

ts
try {
  // Get data with authentication from your server
  const response = await fetch('http://localhost:3030', {
    headers: {
      // Set the authorization header using Feathers Cloud Auth
      Authorization: await auth.getHeader()
    }
  })
  console.log(await response.json())
}
catch (error: unknown) {
  // Redirect to the login page when login is required
  if (error instanceof LoginRequiredError) {
    window.location.href = await auth.getLoginUrl(error)
  }
  else {
    throw error
  }
}

This example uses the fetch API to make a request and sets the Authorization header to the Cloud Auth user access token.

When a login is required, we redirect to the application login page. Once successful, the user gets redirected to the original page and the request will succeed.

Frameworks

For more complete examples for different frontend frameworks see the following pages: