I would like to trigger a programmatic event on my React application,
I saw on the doc it is possible to use a post-login event, which is exactly what I am looking for.
My question: is it implemented out-of-the-box by the library auth0 for react?
As of today I’m using the last version 2.0.1
Here is a code sample that could add some context:
import React, { ReactNode } from "react";
import { useNavigate } from "react-router-dom";
import { Auth0Provider, AppState } from "@auth0/auth0-react";
interface Props {
children: ReactNode;
}
function Auth0ProviderWithHistory({ children }: Props) {
const domain = process.env.REACT_APP_AUTH0_DOMAIN;
const clientId = process.env.REACT_APP_AUTH0_CLIENT_ID;
const audience = process.env.REACT_APP_API_AUDIENCE;
const navigate = useNavigate();
const onRedirectCallback = (appState?: AppState) => {
navigate(
appState?.returnTo || window.location.pathname + window.location.search
);
};
return (
<Auth0Provider
domain={domain}
clientId={clientId}
onRedirectCallback={onRedirectCallback}
authorizationParams={{
redirect_uri: window.location.origin,
audience: audience
}}
// postLoginTriggerEvent={(event) => {triggerAnalytics(event)}} Is what I am looking for
>
{children}
</Auth0Provider>
);
}
export default Auth0ProviderWithHistory;
The function onRedirectCallback
is not good enough because triggered not only after a user log in but also when he is already logged in and reload the application.