@deephaven/auth-plugins
Version:
Deephaven Auth Plugins
75 lines (74 loc) • 2.94 kB
JavaScript
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); }); }; }
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 } from "react/jsx-runtime";
import { 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,
getLoginOptions
} = _ref;
var client = useClient();
var [error, setError] = useState();
var [isLoggedIn, setIsLoggedIn] = useState(false);
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