fix: When declining OAuth return to previous page (#13189)

* Get `onErrorReturnTo` URL

* If no code return to previous page

* On invalid permissions, return to previous page

* When not enough permissions given, invalidate the credential created

* Type fix

* Stripe go back to previous page on cancel

* Clean up console.logs

* Add early returns when redirecting

---------

Co-authored-by: sean-brydon <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-01-29 05:51:53 -03:00
1 parent f33ea91ab0
commit 8bc265faaa
8 files changed
+75 -7

No files matched your search

@@ -1,5 +1,6 @@
import type { UseMutationOptions } from "@tanstack/react-query";
import { useMutation } from "@tanstack/react-query";
import { usePathname } from "next/navigation";
import type { IntegrationOAuthCallbackState } from "@calcom/app-store/types";
import { WEBAPP_URL } from "@calcom/lib/constants";
@@ -26,8 +27,20 @@ type UseAddAppMutationOptions = CustomUseMutationOptions & {
returnTo?: string;
};
type useAddAppMutationVariables = {
type?: App["type"];
variant?: string;
slug?: string;
isOmniInstall?: boolean;
teamId?: number;
onErrorReturnTo?: string;
};
function useAddAppMutation(_type: App["type"] | null, allOptions?: UseAddAppMutationOptions) {
const { returnTo, ...options } = allOptions || {};
const pathname = usePathname();
const onErrorReturnTo = `${WEBAPP_URL}${pathname}`;
const mutation = useMutation<
AddAppMutationData,
Error,
@@ -59,6 +72,8 @@ function useAddAppMutation(_type: App["type"] | null, allOptions?: UseAddAppMuta
{ variant: variables && variables.variant, slug: variables && variables.slug },
location.search
),
onErrorReturnTo,
fromApp: true,
...(type === "google_calendar" && { installGoogleVideo: options?.installGoogleVideo }),
...(teamId && { teamId }),
};