Skip to content

Feathers Cloud Auth

Feathers Cloud Auth adds secure authentication to any JavaScript and TypeScript application. It works with NodeJS, Cloudflare Workers, Deno or BunJS and even on websites with no server at all.

Frontend

The following example adds Feathers Cloud Auth on a client. It makes authenticated request to an API and redirects the user to the login screen if they need to log in.

It can be used with any frontend framework like React, VueJS, Angular etc.

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

const auth = createClient({
  appId: '<app-did>'
})

try {
  // Create the authorization header for each request
  const authorization = await auth.getBearerToken()
  // Get data with authentication from your server
  const response = await fetch('http://localhost:3030', {
    headers: { authorization }
  })
  console.log(await response.json())
} catch (error: unknown) {
  // Redirect to the login page when login is required
  if (error instanceof LoginRequiredError) {
    window.location.href = error.loginUrl
  } else {
    throw error
  }
}

Server

Once the login is successful, the request can be authenticated in a request handler on the server like this:

ts
import { createVerifier } from '@featherscloud/auth'

const verifier = createVerifier({
  appId: '<app-did>'
})

const { user } = verifier.verifyHeader(request.header.authorization)
// User has user.email and user.id which is the Feathers Cloud Auth user id

Last updated: