This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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