Skip to content

PetrusConfig

Jiří Čermák edited this page Dec 6, 2022 · 10 revisions

Interface: PetrusConfig

Table of contents

Properties

Methods

Properties

handlers

handlers: Object

Type declaration

Name Type
authenticate? (credentails: void) => HandlerReturnValue<{ tokens: Tokens ; user?: any }>
getAuthUser (tokens: Tokens) => any
refreshTokens (tokens: Required<Tokens>) => HandlerReturnValue<Tokens>

Defined in

src/types/index.ts:232


logger

logger: PetrusLogger

Defined in

src/types/index.ts:69


mapStorageDriverToTokensPersistence

mapStorageDriverToTokensPersistence: Object

Type declaration

Name Type
local { get: <Key>(key: Key) => Promise<any> ; remove: <Key>(key: Key) => Promise<void> ; set: <Key, Value>(key: Key, val: Value) => Promise<void | IDBValidKey> }
local.get [object Object]
local.remove [object Object]
local.set [object Object]
none { get: () => null ; set: () => void ; remove: <Key>(key: Key) => Promise<void> }
none.get () => null
none.set () => void
none.remove [object Object]
session { get: <Key>(key: Key) => any ; remove: <Key>(key: Key) => void ; set: <Key, Value>(key: Key, values: Value) => void }
session.get [object Object]
session.remove [object Object]
session.set [object Object]

Defined in

src/types/index.ts:254


oAuth

oAuth: Object

Implicit grant flow

example

  import { configure } from '@ackee/petrus';

  // Minimal required setup:
  const { saga, reducer } = configure({
      oAuth: {
          origin: 'http://myapp.com',
      },
      handlers: {
          refreshTokens,
          getAuthUser,
      },
  });

Web application flow

example

  import { configure } from '@ackee/petrus';

  // Minimal required setup:
  const { saga, reducer } = configure({
     oAuth: {
          origin: 'http://myapp.com',
          *fetchAccessToken(searchParams) {
               const { code } = searchParams;

              // the actuall API request:
              const { accessToken, refreshToken, expiresIn } = yield* api.get('...');

             return {
                  accessToken,
                  refreshToken,
                  expiresIn,
              };
          },
      },
      handlers: {
          refreshTokens,
          getAuthUser,
      },
  });

Type declaration

Name Type Description
enabled boolean -
fetchAccessToken (searchParams: Record<string, any>) => HandlerReturnValue<{ accessToken: string ; expiresIn?: string | number ; refreshToken?: string }> | () => void This method is called after 'parseRedirectUrlParams', but only if those search params don't include accessToken property.
origin string Origin of your app: window.location.origin default ''
redirectPathname string Pathname of redirect URL default '/oauth/redirect'
enforceAccessTokenScheme (searchParams: Record<string, any>) => HandlerReturnValue<{ expiration?: null | string ; token: string }> - It creates and accessToken object from provided searchParams. - This method is called when access token is available. default 'src/modules/oAuth/config/enforceAccessTokenScheme'
enforceRefreshTokenScheme (searchParams: Record<string, any>) => HandlerReturnValue<undefined | { token: string }> - It creates and refreshToken object from provided searchParams. - This method is called when access token is available. default 'src/modules/oAuth/config/enforceRefreshTokenScheme'
parseRedirectUrlParams (location: Location) => HandlerReturnValue<Record<string, any>> Parse search params from URL. It must handle both location.search and location.hash: - /oauth/redirect?access_token=123 - /oauth/redirect#access_token=123 default 'src/modules/oAuth/config/getSearchParams'
processTokens (accessToken: { expiration?: null | string ; token: string }, refreshToken: undefined | { token: string }) => HandlerReturnValue<null | Tokens> This is final OAuth method in this custom flow that combines the results of enforceAccessTokenScheme and enforceRefreshTokenScheme to the PetrusTokens object or null if accessToken isn't available (for example due to authentication error). default 'src/modules/oAuth/config/processTokens'
validateRedirectUrl (oAuth: { enabled: boolean; origin: string; redirectPathname: string; validateRedirectUrl: (oAuth: ..., location: Location) => boolean; parseRedirectUrlParams: (location: Location) => HandlerReturnValue<...>; fetchAccessToken: ((searchParams: Record<...>) => HandlerReturnValue<...>) | (() => void); enforceAccessTokenScheme:..., location: Location) => boolean Validate the current URL on initialization, if the URL is valid, the 'parseRedirectUrlParams' method is called. default 'src/modules/oAuth/config/validateRedirectUrl'

Defined in

src/types/index.ts:126


tokens

tokens: Object

Type declaration

Name Type Description
applyAccessTokenExternally boolean If true, anytime valid non-expired tokens becomes available the applyAccessTokenRequest is dispatch. Until the applyAccessTokenResolve is dispatched by any external service, the auth. flow is paused. This gives you the option to do something with access token externally, e.g. injected to the Authorization header. default false deprecated
autoStartTokensRetrieval boolean If false, petrus won't start tokens retrieval saga automatically but it's up to you to call the retrieveTokens saga. By calling retrieveTokens saga, petrus starts the authentication flow. Either it signs-in user with avail. access token or it won't if the access token is expired and couldn't be refreshed. default true
checkTokenExpirationOnTabFocus boolean Check if access token is expired when document visibility changes from 'hidden' to 'visibile'. And if it's expired, then refresh access token. default true
minRequiredExpiration number default 60_000 // 1 minute
requestDurationEstimate number Refresh tokens ${requestDurationEstimate}ms before token expires. default 500 // ms

Defined in

src/types/index.ts:195

Methods

selector

selector(state): CombinedState<Object>

Parameters

Name Type
state any

Returns

CombinedState<Object>

Defined in

src/types/index.ts:71