ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
117 lines • 4.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCheckAuth = void 0;
const react_1 = require("react");
const useAuthProvider_1 = __importStar(require("./useAuthProvider.cjs"));
const useLogout_1 = __importDefault(require("./useLogout.cjs"));
const notification_1 = require("../notification/index.cjs");
const routing_1 = require("../routing/index.cjs");
const useCreatePath_1 = require("../routing/useCreatePath.cjs");
/**
* Get a callback for calling the authProvider.checkAuth() method.
* In case of rejection, redirects to the login page, displays a notification,
* and throws an error.
*
* This is a low level hook. See those more specialized hooks
* for common authentication tasks, based on useCheckAuth.
*
* @see useAuthenticated
* @see useAuthState
*
* @returns {Function} checkAuth callback
*
* @example
*
* import { useCheckAuth } from 'react-admin';
*
* const MyProtectedPage = () => {
* const checkAuth = useCheckAuth();
* useEffect(() => {
* checkAuth().catch(() => {});
* }, []);
* return <p>Private content: EZAEZEZAET</p>
* } // tip: use useAuthenticated() hook instead
*
* const MyPage = () => {
* const checkAuth = useCheckAuth();
* const [authenticated, setAuthenticated] = useState(true); // optimistic auth
* useEffect(() => {
* checkAuth({}, false)
* .then(() => setAuthenticated(true))
* .catch(() => setAuthenticated(false));
* }, []);
* return authenticated ? <Bar /> : <BarNotAuthenticated />;
* } // tip: use useAuthState() hook instead
*/
const useCheckAuth = () => {
const authProvider = (0, useAuthProvider_1.default)();
const notify = (0, notification_1.useNotify)();
const logout = (0, useLogout_1.default)();
const basename = (0, routing_1.useBasename)();
const loginUrl = (0, useCreatePath_1.removeDoubleSlashes)(`${basename}/${useAuthProvider_1.defaultAuthParams.loginUrl}`);
const checkAuth = (0, react_1.useCallback)(async (params = {}, logoutOnFailure = true, redirectTo = loginUrl) => {
// The authProvider is optional in react-admin
if (!authProvider) {
return checkAuthWithoutAuthProvider();
}
try {
return await authProvider.checkAuth(params);
}
catch (error) {
if (logoutOnFailure) {
logout({}, error && error.redirectTo != null
? error.redirectTo
: redirectTo);
const shouldSkipNotify = error && error.message === false;
!shouldSkipNotify &&
notify(getErrorMessage(error, 'ra.auth.auth_check_error'), { type: 'error' });
}
throw error;
}
}, [authProvider, logout, notify, loginUrl]);
return checkAuth;
};
exports.useCheckAuth = useCheckAuth;
const checkAuthWithoutAuthProvider = async () => undefined;
const getErrorMessage = (error, defaultMessage) => typeof error === 'string'
? error
: typeof error === 'undefined' || !error.message
? defaultMessage
: error.message;
//# sourceMappingURL=useCheckAuth.js.map