UNPKG

ra-core

Version:

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

115 lines 4.35 kB
"use strict"; 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const react_query_1 = require("@tanstack/react-query"); const notification_1 = require("../notification/index.cjs"); const routing_1 = require("../routing/index.cjs"); const useAuthProvider_1 = __importStar(require("./useAuthProvider.cjs")); const useCreatePath_1 = require("../routing/useCreatePath.cjs"); /** * Get a callback for calling the authProvider.login() method * and redirect to the previous authenticated page (or the home page) on success. * * @see useAuthProvider * * @returns {Function} login callback * * @example * * import { useLogin } from 'react-admin'; * * const LoginButton = () => { * const [loading, setLoading] = useState(false); * const login = useLogin(); * const handleClick = { * setLoading(true); * login({ username: 'john', password: 'p@ssw0rd' }, '/posts') * .then(() => setLoading(false)); * } * return <button onClick={handleClick}>Login</button>; * } */ const useLogin = () => { const authProvider = (0, useAuthProvider_1.default)(); const queryClient = (0, react_query_1.useQueryClient)(); const location = (0, routing_1.useLocation)(); const locationState = location.state; const navigate = (0, routing_1.useNavigate)(); const basename = (0, routing_1.useBasename)(); const { resetNotifications } = (0, notification_1.useNotificationContext)(); const nextPathName = locationState && locationState.nextPathname; const nextSearch = locationState && locationState.nextSearch; const afterLoginUrl = (0, useCreatePath_1.removeDoubleSlashes)(`${basename}/${useAuthProvider_1.defaultAuthParams.afterLoginUrl}`); const login = (0, react_1.useCallback)((params = {}, pathName) => { if (authProvider) { return authProvider.login(params).then(ret => { resetNotifications(); queryClient.invalidateQueries({ queryKey: ['auth', 'getPermissions'], }); if (ret && ret.hasOwnProperty('redirectTo')) { if (ret) { navigate(ret.redirectTo); } } else { const redirectUrl = pathName ? pathName : nextPathName + nextSearch || afterLoginUrl; navigate(redirectUrl); } return ret; }); } else { resetNotifications(); navigate(afterLoginUrl); return Promise.resolve(); } }, [ authProvider, queryClient, navigate, nextPathName, nextSearch, resetNotifications, afterLoginUrl, ]); return login; }; exports.default = useLogin; module.exports = exports.default; //# sourceMappingURL=useLogin.js.map