@8base/auth-provider 中文文档教程

发布于 3年前 浏览 25 更新于 3年前

8base Auth Provider

8base Auth Provider 库包含具有身份验证状态的提供程序

AuthProvider

Table of Contents

AuthProvider

Extends Component

提供对身份验证状态的访问。

AuthContextProps

身份验证上下文

Properties

  • isAuthorized boolean?
  • authState AuthState?
  • setAuthState function (AuthState): void?

withAuth

Hoc 将身份验证状态传递给组件

Parameters

  • WrappedComponent React$ComponentType<any>
  • auth AuthContextProps Auth state passed by the props.

返回 React$ComponentType

Usage

Simple Usage

import { AuthProvider, AuthConsumer } from '@8base/auth-provider';

  <AuthProvider>
    ...
      <AuthConsumer>
        {
          (auth: AuthContextProps) => (<div />)
        }
      </AuthConsumer>
    ...  
  </AuthProvider>

Usage with @8base/create-apollo-client, @8base/apollo-links and @8base/apollo-provider

import React, { Component } from 'react';
import { BatchHttpLink } from 'apollo-link-batch-http';
import { withAuth } from '@8base/auth-provider';
import { createApolloClient } from '@8base/create-apollo-client';
import { ApolloProvider } from '@8base/apollo-provider';
import { createAuthLink,  fileUploadLink } from '@8base/apollo-links';

withAuth(
  class extends Component {
    getClient: Function;

    constructor(props: AsyncApolloProviderProps) {
      super(props);

      this.getClient = createApolloClient({
        links: [
          fileUploadLink,
          createAuthLink({
            getAuthState: this.getAuthState,
            getRefreshTokenParameters: this.getRefreshTokenParameters,
            onAuthSuccess: this.onAuthSuccess,
            onAuthError: this.onAuthError,
          }),
          new BatchHttpLink({ uri: process.env.REACT_APP_SERVER_URL }),
        ],
      });
    }

    getRefreshTokenParameters = () => {
      const { auth: { authState: { email, refreshToken }}} = this.props;

      return { email, refreshToken };
    }

    onAuthSuccess = ({ refreshToken, idToken }) => {
      const { auth: { setAuthState }} = this.props;

      setAuthState({
        idToken,
        refreshToken,
      });
    }

    onAuthError = (err) => {
      const { auth: { setAuthState }} = this.props;

      setAuthState({
        idToken: '',
        refreshToken: '',
      });
    }

    getAuthState = () => {
      const { auth: { authState: { idToken, organizationId, accountId }}} = this.props;

      return {
        idToken,
        organizationId,
        accountId,
      };
    }

    render() {
      const { children } = this.props;

      return (
        <ApolloProvider
          getClient={ this.getClient }
          uri={ process.env.REACT_APP_SERVER_URL }
        >
          { ({ apolloClient, isLoading }) => (<div />) }
        </ApolloProvider>
      );
    }
  }
)

8base Auth Provider

The 8base Auth Provider library contains provider with authentication state

AuthProvider

Table of Contents

AuthProvider

Extends Component

Provides access to the authentication state.

AuthContextProps

Authentication context

Properties

  • isAuthorized boolean?
  • authState AuthState?
  • setAuthState function (AuthState): void?

withAuth

Hoc to pass auth state to the component

Parameters

  • WrappedComponent React$ComponentType<any>
  • auth AuthContextProps Auth state passed by the props.

Returns React$ComponentType<InputProps>

Usage

Simple Usage

import { AuthProvider, AuthConsumer } from '@8base/auth-provider';

  <AuthProvider>
    ...
      <AuthConsumer>
        {
          (auth: AuthContextProps) => (<div />)
        }
      </AuthConsumer>
    ...  
  </AuthProvider>

Usage with @8base/create-apollo-client, @8base/apollo-links and @8base/apollo-provider

import React, { Component } from 'react';
import { BatchHttpLink } from 'apollo-link-batch-http';
import { withAuth } from '@8base/auth-provider';
import { createApolloClient } from '@8base/create-apollo-client';
import { ApolloProvider } from '@8base/apollo-provider';
import { createAuthLink,  fileUploadLink } from '@8base/apollo-links';

withAuth(
  class extends Component {
    getClient: Function;

    constructor(props: AsyncApolloProviderProps) {
      super(props);

      this.getClient = createApolloClient({
        links: [
          fileUploadLink,
          createAuthLink({
            getAuthState: this.getAuthState,
            getRefreshTokenParameters: this.getRefreshTokenParameters,
            onAuthSuccess: this.onAuthSuccess,
            onAuthError: this.onAuthError,
          }),
          new BatchHttpLink({ uri: process.env.REACT_APP_SERVER_URL }),
        ],
      });
    }

    getRefreshTokenParameters = () => {
      const { auth: { authState: { email, refreshToken }}} = this.props;

      return { email, refreshToken };
    }

    onAuthSuccess = ({ refreshToken, idToken }) => {
      const { auth: { setAuthState }} = this.props;

      setAuthState({
        idToken,
        refreshToken,
      });
    }

    onAuthError = (err) => {
      const { auth: { setAuthState }} = this.props;

      setAuthState({
        idToken: '',
        refreshToken: '',
      });
    }

    getAuthState = () => {
      const { auth: { authState: { idToken, organizationId, accountId }}} = this.props;

      return {
        idToken,
        organizationId,
        accountId,
      };
    }

    render() {
      const { children } = this.props;

      return (
        <ApolloProvider
          getClient={ this.getClient }
          uri={ process.env.REACT_APP_SERVER_URL }
        >
          { ({ apolloClient, isLoading }) => (<div />) }
        </ApolloProvider>
      );
    }
  }
)
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文