Skip to content

Instantly share code, notes, and snippets.

@jtoar
Created March 21, 2023 01:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jtoar/5a51ef95fda92e23d06059c46fe08f42 to your computer and use it in GitHub Desktop.
"use strict";
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js/object/define-property");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports.useReauthenticate = void 0;
var _react = require("react");
var _useToken = require("./useToken");
const notAuthenticatedState = {
isAuthenticated: false,
currentUser: null,
userMetadata: null,
loading: false,
hasError: false
};
const useReauthenticate = (authImplementation, setAuthProviderState, getCurrentUser, skipFetchCurrentUser) => {
const getToken = (0, _useToken.useToken)(authImplementation);
return (0, _react.useCallback)(async () => {
setAuthProviderState(oldState => ({
...oldState,
loading: true
}));
try {
const userMetadata = await authImplementation.getUserMetadata();
if (!userMetadata) {
let loading = false;
if (authImplementation.clientHasLoaded) {
loading = !authImplementation.clientHasLoaded();
}
setAuthProviderState({
...notAuthenticatedState,
loading,
client: authImplementation.client
});
} else {
await getToken();
const currentUser = skipFetchCurrentUser ? null : await getCurrentUser();
setAuthProviderState(oldState => ({
...oldState,
userMetadata,
currentUser,
isAuthenticated: true,
loading: false,
client: authImplementation.client
}));
}
} catch (e) {
setAuthProviderState({
...notAuthenticatedState,
hasError: true,
error: e
});
}
}, [authImplementation, getToken, setAuthProviderState, skipFetchCurrentUser, getCurrentUser]);
};
exports.useReauthenticate = useReauthenticate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment