0.0.1 • Published 1 year ago

@octo-pi/auth v0.0.1

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
1 year ago

Octopod Auth

pipeline status

Package for managed authentication to Octopod

1. Installation

Install the package with NPM :

yarn add @octo-pi/auth

You must have react@17 installed s a peer requirement.

2. Getting started

Simply import the Authenticator context from the package and wrap it around the part of your code which needs authentication to Octopod :

import { AuthenticatorProvider } from '@octo-pi/auth'

export const MyComponent = ({ prop1, prop2 }) => (
  <div>
    <span>This part does not need authentication</span>
    <AuthenticatorProvider>
      <MyComponentWithAuthentication />
    </AuthenticatorProvider>
  </div>
)

Then, in your component, consume this context :

import { useAuthenticatorContext } from '@octo-pi/auth'

export const MyComponentWithAuthentication = ({ prop1, prop2 }) => {
  const { isAuthenticated } = useAuthenticatorContext()

  return isAuthenticated ? <div>You are authenticated !</div><div>You are not yet authenticated.</div>
}

Upon mounting, AuthenticatorProvider will automatically try to validate your stored refresh key if you have one, or redirect you to the Octopod authentication if you have none. Once done, you can call fetchFromOctopod to begin working with the Octopod API.

3. Documentation

Several methods are available from the context :

errors: string[]

Contains all registered errors in the authentication process. Possible errors are :

  • could not connect to Octopod if the Octopod server could not be reached (possibly Octopod authentication server is down)
  • could not parse tokens from Octopod: body could not be parsed as JSON if the Octopod authentication server is up but responded with no parseable content.
  • could not parse tokens from Octopod: at least the access or refresh key are missing if the Octopod authentication server is up, responded with parseable content but misses at least one of the two required keys.
  • Any other exception will be included.

fetchFromOctopod: function(method: HTTPMethod, route: string)

Fetches data from Octopod API at the given route. Please refer to the Octopod API documentation about the exposed routes.

Parameters:

  • method (required) must be "GET", "POST", "PATCH" or "DELETE"
  • route (required) must be any string

Return value :

  • the context is not authenticated : null
  • the context is authenticated and the response has no body : {}
  • the context is authenticated and the response has a body : the body parsed from JSON to an hash

isAuthenticated: boolean

True if the authentication flow is finished and successful, false if not.

4. License

All rights reserved to OCTO Technology (France). Credit goes to the Produits Internes team. Use for other businesses is strictly prohibited.