UNPKG

@deephaven/auth-plugins

Version:
84 lines (83 loc) 4.59 kB
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } import React, { useEffect, useState } from 'react'; import { LoadingOverlay } from '@deephaven/components'; import { useClient } from '@deephaven/jsapi-bootstrap'; import Log from '@deephaven/log'; import { CanceledPromiseError, getErrorMessage } from '@deephaven/utils'; import AuthenticationError from "./AuthenticationError.js"; import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime"; var log = Log.module('AuthPluginBase'); /** * Base AuthPlugin that gets passed a function for retrieving the login options, and then attempting to login with them. * @param getLoginOptions Function that returns a promise for the login options */ function AuthPluginBase(_ref) { var children = _ref.children, getLoginOptions = _ref.getLoginOptions; var client = useClient(); var _useState = useState(), _useState2 = _slicedToArray(_useState, 2), error = _useState2[0], setError = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isLoggedIn = _useState4[0], setIsLoggedIn = _useState4[1]; useEffect(() => { var isCanceled = false; function verifyNotCanceled() { if (isCanceled) { throw new CanceledPromiseError('Login canceled.'); } } function login() { return _login.apply(this, arguments); } function _login() { _login = _asyncToGenerator(function* () { try { var loginOptions = yield getLoginOptions(); verifyNotCanceled(); log.info('Logging in...'); yield client.login(loginOptions); verifyNotCanceled(); setIsLoggedIn(true); } catch (e) { if (!isCanceled) { var _getErrorMessage; log.error('Unable to login:', e); var message = (_getErrorMessage = getErrorMessage(e)) !== null && _getErrorMessage !== void 0 ? _getErrorMessage : 'Unable to login. Verify credentials.'; setError(new AuthenticationError(message)); setIsLoggedIn(false); } } }); return _login.apply(this, arguments); } login(); return () => { isCanceled = true; }; }, [client, getLoginOptions]); if (!isLoggedIn) { return /*#__PURE__*/_jsx(LoadingOverlay, { "data-testid": "auth-base-loading", isLoading: error == null, isLoaded: false, errorMessage: getErrorMessage(error) }); } // eslint-disable-next-line react/jsx-no-useless-fragment return /*#__PURE__*/_jsx(_Fragment, { children: children }); } export default AuthPluginBase; //# sourceMappingURL=AuthPluginBase.js.map