Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem refetching after login/logout #579

Open
marcospassos opened this issue Dec 12, 2020 · 1 comment
Open

Problem refetching after login/logout #579

marcospassos opened this issue Dec 12, 2020 · 1 comment

Comments

@marcospassos
Copy link

marcospassos commented Dec 12, 2020

I've read all issues regarding this topic but I couldn't find any solution for forcing a refetch after login/logout.

const authenticated: RouteRenderMethod = ({Component, props, match}): ResolvedElement | undefined => {
    if (!Component || !props) {
        return undefined;
    }
    
    const required = !match.routes.some(route => route.public === true);

    if (((props as any).me?.user !== null) === required) {
        return <Component {...props} />;
    }

    if (!required) {
        throw new RedirectException(
            match.location.state?.previousLocation ?? routes.home,
            301,
        );
    }

    throw new RedirectException({
        pathname: routes.signin,
        state: {
            previousLocation: match.location,
        },
    }, 301);
};

export const routeConfig: RouteConfig = [
    {
        path: '/',
        query: AppUserQuery,
        Component: App,
        render: authenticated,
        children: [
            {
                path: 'signin',
                public: true,
                name: routes.signin,
                Component: AuthLayout,
                children: [
                    {
                        Component: SignInPage,
                    },
                ],
            },
            {
                path: 'home',
                name: routes.home,
                Component: AdminLayout,
                children: [
                    {
                        query: UserHomePageQuery,
                        Component: HomePage,
                    },
                ],
            },
        ],
    },
];

The problem is that after logging in, the user is redirected to the homepage. However, the render method is called with the previous query result, and the user gets redirected back to the login page.

I've tried the following solutions:

  • Adding a nonce in the prepareVariabes based on a location state flag. Although it forces fetching, the query is executed twice
  • Tried using the retry method based on a location state flag, but it looks like there is no way to call retry in the render method without ending in an infinite loop

Is there any other alternative solution?

@itajaja
Copy link
Contributor

itajaja commented Dec 14, 2020

the way to handle this is to create a shiny new context every time you log in/out, which will discard all cache previously collected. basically, you'd call new Environment again and use that in the context provider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants