import { MutationOptions, useMutation } from "@tanstack/react-query" import { useCreateSession } from "./useCreateSession" import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce" import type { AuthenticationProviderRedirectResponse } from "../api" function useSignInWithDiscord( mutationOptions: Omit< MutationOptions, "mutationFn" | "mutationKey" > = {}, ) { const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce() const { createSessionAsync } = useCreateSession() const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({ mutationFn: async (): Promise => { await ensureNoSessionAndFetchNonce() return (await createSessionAsync({ type: "discord", })) as AuthenticationProviderRedirectResponse // ^ By passing type: "discord" we know for sure the return type is // AuthenticationProviderRedirectResponse }, ...mutationOptions, }) return { signInWithDiscord: mutate, signInWithDiscordAsync: mutateAsync, ...signInMutationRestParameters, } } export { useSignInWithDiscord }