UNPKG

@reactit/auth

Version:

The ultimate token basted authentication solution for React

102 lines (94 loc) 4 kB
/// <reference types="react" /> import * as react from 'react'; import { ReactNode } from 'react'; import { StorageTypes } from '@reactit/hooks'; declare type Token = string; interface TokenBundle { token: Token; expiresAt?: number; } declare function coerceExpiration(exp: number | Date | undefined): number | undefined; declare function isTokenBundleValid(bundle: TokenBundle): boolean; interface AuthState<U> { initialized: boolean; auth?: TokenBundle; renew?: TokenBundle; user?: U; } interface AuthActionResult<U> { token: Token; tokenExpiration: number | Date | undefined; renew?: Token; renewExpiration?: number | Date | undefined; user?: U; } interface AuthActions<U, SI = any, RF = void, SO = void> { signIn: (input?: SI) => void; renewToken: (input?: RF) => void; signOut: (input?: SO) => void; signInAsync: (input?: SI) => Promise<AuthActionResult<U>>; renewTokenAsync: (input?: RF) => Promise<AuthActionResult<U>>; signOutAsync: (input?: SO) => Promise<void>; setAuth: (value: AuthActionResult<U>) => void; } interface AuthContext<U, SI = any, RF = void, SO = void> extends AuthActions<U, SI, RF, SO>, AuthState<U> { } declare const _AuthContext: react.Context<AuthContext<any, any, any, any> | undefined>; declare function useAuthContext<U, SI = any, RF = void, SO = void>(): AuthContext<U, SI, RF, SO>; interface AuthGuardProps { showAuth?: boolean; showUnAuth?: boolean; children: ReactNode; } /** * A simple guard component to conditionally render the content * in relation to the authentication state. */ declare function AuthGuard({ showAuth, showUnAuth, children }: AuthGuardProps): JSX.Element; /** * Provide access to all the authentication functionalities */ interface UseAuthReturn<U, SI = any, RF = void, SO = void> extends Omit<AuthContext<U, SI, RF, SO>, 'auth' | 'renew'> { isAuthenticated: boolean; isRenewEnabled: boolean; } declare function useAuth<U, SI = any, RF = void, SO = void>(): UseAuthReturn<U, SI, RF, SO>; /** * Returns true if there is an active authentication ongoing */ declare function useIsAuthenticated(): boolean; /** * Returns the authenticated user (as returned by the last * doSignIn or doRenew */ declare function useAuthUser<U>(): U | undefined; /** * Returns the latest authentication token if present */ declare function useAuthToken(): Token | undefined; /** * Effect called when the authentication state changes due to * any reason. */ declare function useAuthChangeEffect<U, SI = any, RF = void, SO = void>(effect: (context: AuthContext<U, SI, RF, SO>) => (void | (() => void))): void; interface AuthProviderProps<U, SI = any, RF = void, SO = void> { authStorage?: StorageTypes; authPrefix?: string; devToken?: string; devSignedIn?: boolean; devUser?: U; doSignIn?: (ctx: AuthState<U>, input: SI | undefined) => Promise<AuthActionResult<U>>; doRenew?: (ctx: AuthState<U>, input: RF | undefined) => Promise<AuthActionResult<U>>; doSignOut?: (ctx: AuthState<U>, input: SO | undefined) => Promise<void>; onTokenChange?: (ctx: AuthState<U>, token: Token | undefined) => void; renewOnMount?: boolean; } /** * Authentication provider to wrap the entire application. This * component does contain all the behavioural logic for the entire * authentication workflow. */ declare function AuthProvider<U, SI = any, RF = void, SO = void>({ authStorage, authPrefix, devToken, devSignedIn, devUser, doSignIn, doRenew, doSignOut, onTokenChange, renewOnMount, children, }: AuthProviderProps<U, SI, RF, SO> & { children: ReactNode; }): JSX.Element; export { type AuthActionResult, type AuthActions, type AuthContext, AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, type AuthState, type Token, type TokenBundle, type UseAuthReturn, _AuthContext, coerceExpiration, isTokenBundleValid, useAuth, useAuthChangeEffect, useAuthContext, useAuthToken, useAuthUser, useIsAuthenticated };