Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1.02 KB

File metadata and controls

40 lines (27 loc) · 1.02 KB
title TypeScript

TypeScript

npm create feathersdev@latest app --framework vanilla --app-id "<your-app-id>"

Initialization

In any web application, create an auth.ts file that allows to make authenticated requests to the backend like this:

<<< @/examples/frontend/auth.ts

This file exports an authFetch function that can be used just like the normal fetch API and will redirect to the login page if the user needs to log in.

Usage

Authenticated requests to a server running one of the supported platforms can be made like this:

import { authFetch } from './auth.js'

async function getMessage() {
  // Get data with authentication from your server
  const response = await authFetch('http://localhost:3030/message', {
    method: 'GET'
  })

  if (response.status >= 400) {
    throw new Error(`Failed to load message: ${response.statusText}`)
  }

  const body = await response.json()

  return body
}