Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

@ackee/petrus

4.0.0 • Public • Published

ackee|Petrus

GitHub license CI Status PRs Welcome Dependency Status bundlephobia bundlephobia

Petrus

The library handles authentication logic with token based flow.

Main features

  • automatically refresh access token before it expires
  • persisting tokens state in local or session storage (optional)
  • automatically fetching an authorized user after successful tokens retrieval from a persistent storage
  • simple API for auth state management: getAuthStateChannel, withAuthSession
  • authorizable HOC - render content (a firewall, an authorized content or a loader) based on current auth. state

Requirements

The library relies on react, redux, react-redux, redux-saga and reselect packages as peer dependencies.


Table of contents


Installing

Using yarn:

$ yarn add @ackee/petrus

Using npm:

$ npm i -S @ackee/petrus

Usage

Minimal required configuration

import { configure } from '@ackee/petrus';
 
// 1. Provide authenticate, refreshTokens and getAuthUser methods
const { saga, reducer } = configure({
    handlers: {
        authenticate,
        refreshTokens,
        getAuthUser,
    },
});
 
// 2. Add auth reducer
const rootReducer = combineReducers({
    auth: reducer
});
 
// 3. And launch the saga
function*() {
    yield all([saga()])
}

To see defaults and available configurations with examples, go here.

Usage with @ackee/antonio

Minimal required configuration with HTTP client @ackee/antonio requires additionally to set applyAccessTokenExternally option to true. Otherwise Authorization header won't be set and thus every auth. request will result in 401 error.

import { configure } from '@ackee/petrus';
 
// 1. Provide authenticate, refreshTokens and getAuthUser methods
const { saga, reducer } = configure({
    handlers: {
        authenticate,
        refreshTokens,
        getAuthUser,
    },
    tokens: {
        applyAccessTokenExternally: true,
    },
});
 
// 2. Add auth reducer
const rootReducer = combineReducers({
    auth: reducer
});
 
// 3. And launch the saga
function*() {
    yield all([saga()])
}

With OAuth2

@ackee/petrus supports following OAuth2 flows:

  • Implicit grant flow
    • Matches with the default configuration.
    • origin property is required
  • Web application flow
    • Additionally to the Implicit grant flow, you have to provide the fetchAccessToken method.

See how to setup @ackee/petrus for these flows here.

Keywords

none

Install

npm i @ackee/petrus

DownloadsWeekly Downloads

80

Version

4.0.0

License

MIT

Unpacked Size

254 kB

Total Files

239

Last publish

Collaborators

  • avatar
  • avatar
  • avatar