UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

91 lines 4.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PreviousLocationStorageKey = exports.useHandleAuthCallback = void 0; const react_1 = require("react"); const react_query_1 = require("@tanstack/react-query"); const routing_1 = require("../routing/index.cjs"); const useAuthProvider_1 = __importDefault(require("./useAuthProvider.cjs")); const util_1 = require("../util/index.cjs"); /** * This hook calls the `authProvider.handleCallback()` method on mount. This is meant to be used in a route called * by an external authentication service (e.g. Auth0) after the user has logged in. * By default, it redirects to application home page upon success, or to the `redirectTo` location returned by `authProvider. handleCallback`. * * @returns An object containing { isPending, data, error, refetch }. */ const useHandleAuthCallback = (options) => { const authProvider = (0, useAuthProvider_1.default)(); const redirect = (0, routing_1.useRedirect)(); const location = (0, routing_1.useLocation)(); const locationState = location.state; const nextPathName = locationState && locationState.nextPathname; const nextSearch = locationState && locationState.nextSearch; const defaultRedirectUrl = nextPathName ? nextPathName + nextSearch : '/'; const { onSuccess, onError, onSettled, ...queryOptions } = options ?? {}; let handleCallbackPromise; const queryResult = (0, react_query_1.useQuery)({ queryKey: ['auth', 'handleCallback'], queryFn: ({ signal }) => { if (!handleCallbackPromise) { handleCallbackPromise = authProvider && typeof authProvider.handleCallback === 'function' ? authProvider .handleCallback({ signal }) .then(result => result ?? null) : Promise.resolve(); } return handleCallbackPromise; }, retry: false, ...queryOptions, }); const onSuccessEvent = (0, util_1.useEvent)(onSuccess ?? ((data) => { // AuthProviders relying on a third party services redirect back to the app can't // use the location state to store the path on which the user was before the login. // So we support a fallback on the localStorage. const previousLocation = localStorage.getItem(exports.PreviousLocationStorageKey); const redirectTo = data?.redirectTo ?? previousLocation; if (redirectTo === false) { return; } redirect(redirectTo ?? defaultRedirectUrl); })); const onErrorEvent = (0, util_1.useEvent)(onError ?? noop); const onSettledEvent = (0, util_1.useEvent)(onSettled ?? noop); (0, react_1.useEffect)(() => { if (queryResult.error == null || queryResult.isFetching) return; onErrorEvent(queryResult.error); }, [onErrorEvent, queryResult.error, queryResult.isFetching]); (0, react_1.useEffect)(() => { if (queryResult.data === undefined || queryResult.isFetching) return; onSuccessEvent(queryResult.data); }, [onSuccessEvent, queryResult.data, queryResult.isFetching]); (0, react_1.useEffect)(() => { if (queryResult.status === 'pending' || queryResult.isFetching) return; onSettledEvent(queryResult.data, queryResult.error); }, [ onSettledEvent, queryResult.data, queryResult.error, queryResult.status, queryResult.isFetching, ]); return queryResult; }; exports.useHandleAuthCallback = useHandleAuthCallback; /** * Key used to store the previous location in localStorage. * Used by the useHandleAuthCallback hook to redirect the user to their previous location after a successful login. */ exports.PreviousLocationStorageKey = '@react-admin/nextPathname'; const noop = () => { }; //# sourceMappingURL=useHandleAuthCallback.js.map