refine icon indicating copy to clipboard operation
refine copied to clipboard

[BUG] `mutationOptions` are overriding Refine's behavior unexpectedly

Open mohammadxali opened this issue 10 months ago • 2 comments

Describe the bug

Consider this:

import { useLogin } from "@refinedev/core";

const { mutateAsync: login, isLoading } = useLogin({
    mutationOptions: {
        onSuccess(data) {
         console.log("Hey!");
        },
    },
});

And authProvider.ts as follow:

"use client";

import { AuthBindings, HttpError } from "@refinedev/core";

export const authProvider: AuthBindings = {
    login: async ({ username, password }) => {
        try {
            // Preform login
            return { success: true, redirectTo: "/" };
        } catch (error) {
            return {
                success: false,
                error: { message: "Username or password is incorrect", statusCode: (error as HttpError).statusCode },
            };
        }
    },
};

Then onSuccess callback on useLogin will override the Refine's behavior for onSuccess causing the redirectTo to not work.

Steps To Reproduce

  1. Create a login component
  2. Create an authProvider
  3. Add a custom onSuccess callback in useLogin({ mutationOptions: onSuccess() { /* callback */ }});
  4. Redirect doesn't happen after successful login

Expected behavior

mutationOptions should work along side the Refine's default behavior, redirect should occur.

Packages

  • @refinedev/core

Additional Context

No response

mohammadxali avatar Apr 24 '24 19:04 mohammadxali

As this can be seen from this line: https://github.com/refinedev/refine/blob/e5cad5183f4e973027ac40ebd2414daa2f303249/packages/core/src/hooks/auth/useLogin/index.ts#L33-L36

We're omitting onSuccess handler from the mutationOptions on purpose.

On all other Refine hooks, we've omitted onSuccess, onSettled, onError methods if we're using them in our implementation. If we're going to make them work without breaking the current implementation, we need to apply similar changes to all Refine hooks like in the PR #5889

aliemir avatar May 29 '24 07:05 aliemir

@aliemir How can one provide redirect after successful mutate if there's no onSuccess anymore?

rners01 avatar May 30 '24 09:05 rners01

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 30 '24 20:07 stale[bot]

@aliemir How can one provide redirect after successful mutate if there's no onSuccess anymore?

Hey @rners01 sorry for the late reply. This can be done by using mutateAsync or redirect can be customized through props of useLogin().

aliemir avatar Aug 06 '24 12:08 aliemir