@ackee/petrus 中文文档教程

发布于 3年前 浏览 18 项目主页 更新于 3年前

ackee|Petrus

GitHub licenseCI StatusPRs WelcomeDependency Statusbundlephobiabundlephobia

Petrus

该库使用基于令牌的流程处理身份验证逻辑。

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

该库依赖于 reactreduxreact-reduxredux-sagareselect 打包为对等依赖项。


Table of contents


Installing

使用 yarn:

$ yarn add @ackee/petrus

使用 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()])
}

要通过示例查看默认值和可用配置,请转到此处

Usage with @ackee/antonio

HTTP 客户端 @ackee/antonio 的最低要求配置还需要将 applyAccessTokenExternally 选项设置为 true。 否则 Authorization 标头将不会被设置,因此每个身份验证。 请求将导致 401 错误。

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 支持以下 OAuth2 流程:

查看如何为这些流程设置 @ackee/petrus 此处< /a>。

ackee|Petrus

GitHub licenseCI StatusPRs WelcomeDependency Statusbundlephobiabundlephobia

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:

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

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文