我正在尝试在不是JavaScript的https://auth0.com/docs/quickstart/spa/react/01-login之后的打字稿中实现auth0 react。
我有以下内容,最初可能是不确定的:
const [auth0Client, setAuth0] = React.useState<Auth0Client>();
但是在安装时,我正在设置auth0Client
:
React.useEffect(() => {
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client({
domain: props.domain,
client_id: props.clientID,
redirect_uri: props.redirectURI
});
setAuth0(auth0FromHook);
if (window.location.search.includes('code=')) {
const { appState } = await auth0FromHook.handleRedirectCallback();
props.onRedirectCallback(appState);
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}
setLoading(false);
};
initAuth0();
}, []);
不幸的是,我不确定在这里做什么:
<AuthContext.Provider
value={{
isAuthenticated,
loading,
handleRedirectCallback,
getIdTokenClaims: (...p) => auth0Client.getIdTokenClaims(...p),
loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),
getTokenSilently: (...p) => auth0Client.getTokenSilently(...p),
logout: (...p) => auth0Client.logout(...p)
}}
>
{props.children}
</AuthContext.Provider>
在这里,我遇到了auth0Client
的问题:Object is possibly 'undefined'.
我应该如何处理?