Skip to content

Commit 5ece813

Browse files
committed
fix(auth): update onSuccess handler to return boolean for auth flow control
- Modified the `onSuccess` callback in `useGoogleAuth` to return a boolean value, allowing for better control over the authentication flow. - Updated the `useConnectGoogle` hook to handle the new return value, ensuring proper state reset when authentication is not completed. - Added a test case to verify the behavior when the custom success handler returns false, ensuring the authentication state is reset correctly.
1 parent 827d9cb commit 5ece813

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

packages/web/src/auth/hooks/google/useConnectGoogle/useConnectGoogle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const useConnectGoogle = () => {
4747
onSuccess: async (data) => {
4848
const didSyncLocalEvents = await syncPendingLocalEvents(dispatch);
4949
if (!didSyncLocalEvents) {
50-
return;
50+
return false;
5151
}
5252

5353
const googleConnectRequest = buildGoogleConnectRequest(

packages/web/src/auth/hooks/google/useGoogleAuth/useGoogleAuth.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,56 @@ describe("useGoogleAuth", () => {
255255
);
256256
});
257257

258+
it("resets auth when a custom success handler returns false", async () => {
259+
let onSuccessCallback:
260+
| ((data: GoogleAuthConfig) => Promise<boolean | void>)
261+
| undefined;
262+
const customOnSuccess = jest.fn().mockResolvedValue(false);
263+
264+
mockUseGoogleLogin.mockImplementation(({ onSuccess }) => {
265+
onSuccessCallback = onSuccess;
266+
return {
267+
login: mockLogin,
268+
loading: false,
269+
data: null,
270+
};
271+
});
272+
273+
renderHook(() =>
274+
useGoogleAuth({
275+
onSuccess: customOnSuccess,
276+
prompt: "consent",
277+
}),
278+
);
279+
280+
if (!onSuccessCallback) {
281+
throw new Error("Expected onSuccess callback to be registered");
282+
}
283+
284+
const payload: GoogleAuthConfig = {
285+
clientType: "web",
286+
thirdPartyId: "google",
287+
redirectURIInfo: {
288+
redirectURIOnProviderDashboard: "",
289+
redirectURIQueryParams: {
290+
code: "test-auth-code",
291+
scope: "email profile",
292+
state: undefined,
293+
},
294+
},
295+
};
296+
297+
await onSuccessCallback(payload);
298+
299+
expect(customOnSuccess).toHaveBeenCalledWith(payload);
300+
expect(mockDispatchFn).toHaveBeenCalledWith(
301+
expect.objectContaining({ type: "auth/resetAuth" }),
302+
);
303+
expect(mockDispatchFn).not.toHaveBeenCalledWith(
304+
expect.objectContaining({ type: "auth/authSuccess" }),
305+
);
306+
});
307+
258308
describe("onError callback", () => {
259309
it("dispatches auth error when login fails", () => {
260310
let onErrorCallback: ((error: unknown) => void) | undefined;

packages/web/src/auth/hooks/google/useGoogleAuth/useGoogleAuth.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const resetAuthState = (dispatch: AppDispatch) => {
3737

3838
export function useGoogleAuth(
3939
options: {
40-
onSuccess?: (data: GoogleAuthConfig) => Promise<void>;
40+
onSuccess?: (data: GoogleAuthConfig) => Promise<boolean | void>;
4141
prompt?: "consent" | "none" | "select_account";
4242
shouldTryLinkingWithSessionUser?: boolean;
4343
} = {},
@@ -56,7 +56,11 @@ export function useGoogleAuth(
5656
},
5757
onSuccess: async (data) => {
5858
if (onSuccess) {
59-
await onSuccess(data);
59+
const shouldCompleteAuth = await onSuccess(data);
60+
if (shouldCompleteAuth === false) {
61+
resetAuthState(dispatch);
62+
return;
63+
}
6064
dispatch(authSuccess());
6165
return;
6266
}

0 commit comments

Comments
 (0)