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

fix(core): protect useLogin default success handler from custom onSuccess overrides #5889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/selfish-needles-join.md
@@ -0,0 +1,7 @@
---
"@refinedev/core": patch
---

Protect `useLogin` default success handler from custom `onSuccess` overrides

Resolves: #5888
41 changes: 41 additions & 0 deletions packages/core/src/hooks/auth/useLogin/index.spec.ts
Expand Up @@ -382,6 +382,47 @@ describe("useLogin Hook", () => {
expect(mockGo).toBeCalledWith({ to: "/", type: "replace" });
});

it("succeed login with custom `onSuccess` handler", async () => {
const { result } = renderHook(
() => useLogin({ mutationOptions: { onSuccess: () => undefined } }),
{
wrapper: TestWrapper({
authProvider: {
login: ({ email }: any) => {
if (email === "test") {
return Promise.resolve({
success: true,
redirectTo: "/",
});
}

return Promise.resolve({
success: false,
error: new Error("Wrong email"),
});
},
check: () => Promise.resolve({ authenticated: true }),
onError: () => Promise.resolve({}),
logout: () => Promise.resolve({ success: true }),
},
routerProvider,
}),
},
);

const { mutate: login } = result.current ?? { mutate: () => 0 };

await act(async () => {
login({ email: "test" });
});

await waitFor(() => {
expect(result.current.data?.success).toBeTruthy();
});

expect(mockGo).toBeCalledWith({ to: "/", type: "replace" });
});

it("should successfully login with no redirect", async () => {
const { result } = renderHook(() => useLogin(), {
wrapper: TestWrapper({
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/hooks/auth/useLogin/index.ts
Expand Up @@ -144,7 +144,10 @@ export function useLogin<TVariables = {}>({
>({
mutationKey: keys().auth().action("login").get(preferLegacyKeys),
mutationFn: loginFromContext,
onSuccess: async ({ success, redirectTo, error, successNotification }) => {
...(v3LegacyAuthProviderCompatible === true ? {} : mutationOptions),
onSuccess: async (data, variables, context) => {
const { success, redirectTo, error, successNotification } = data;

if (success) {
close?.("login-error");

Expand Down Expand Up @@ -176,11 +179,14 @@ export function useLogin<TVariables = {}>({
}

await invalidateAuthStore();

if (!v3LegacyAuthProviderCompatible) {
mutationOptions?.onSuccess?.(data, variables, context);
}
},
onError: (error: any) => {
open?.(buildNotification(error));
},
...(v3LegacyAuthProviderCompatible === true ? {} : mutationOptions),
meta: {
...(v3LegacyAuthProviderCompatible === true ? {} : mutationOptions?.meta),
...getXRay("useLogin", preferLegacyKeys),
Expand Down