Auth0 Javascript SPA SDK is not maintaining the isAuthenticated value true, after refreshing the page

Hi

We are using Javascript SPA SDK. https://cdn.auth0.com/js/auth0-spa-js/1.9/auth0-spa-js.production.js.

Issue: We want to use loginWithRedirect with the Javascript SPA sdk, It is working fine when we login with this option and try to redirect the URL with parameter code and state. After that we put some code if these parameters are available then we again initialize the Auth0 connection and check authentication for that user to maintain the last stage of the URL. then It shows invalid state for the user every time.
Can you please help us to resolve this issue?

Below is the code for your reference.

export async function updateAuth0OnRefresh(domain, client_id) {
await init(domain, client_id);

const isAuth0Authenticated = await isAuthenticated();

if (!isAuth0Authenticated) {        
    return;
}

const query = window.location.search;
if (query.includes("code=") && query.includes("state=")) {

   
    try {
        const result = await handleRedirectCallback();

        if (result.appState && result.appState.targetUrl) {
            onRedirectCallback(result.appState);
        }
    }
    catch (err) {
        console.log("Error parsing redirect:", err);
    }
    // Use replaceState to redirect the user away and remove the querystring parameters
    window.history.replaceState({}, document.title, window.location.href);
    
}

}

function onRedirectCallback(appState) {

window.history.replaceState(
    {},
    document.title,
    appState && appState.targetUrl
        ? appState.targetUrl
        : window.location.pathname
);

}