The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
import {
ClientError,
InferClientInput,
InferClientOutput,
} from "@mercurjs/client"
import { UseMutationOptions, useMutation } from "@tanstack/react-query"
import { clearAuthToken, sdk, setAuthToken } from "../../lib/client"
export const useSignInWithEmailPass = (
options?: UseMutationOptions<
InferClientOutput<typeof sdk.auth.$actorType.$authProvider.mutate>,
ClientError,
Omit<
InferClientInput<typeof sdk.auth.$actorType.$authProvider.mutate>,
"$actorType" | "$authProvider"
>
>
) => {
return useMutation({
mutationFn: async (payload) => {
const data = (await sdk.auth.$actorType.$authProvider.mutate({
$actorType: "user",
$authProvider: "emailpass",
...payload,
})) as { token: string }
setAuthToken(data.token)
return data
},
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useSignUpWithEmailPass = (
options?: UseMutationOptions<
{ token: string },
ClientError,
Omit<
InferClientInput<
typeof sdk.auth.$actorType.$authProvider.register.mutate
>,
"$actorType" | "$authProvider"
>
>
) => {
return useMutation({
mutationFn: (payload) =>
sdk.auth.$actorType.$authProvider.register.mutate({
$actorType: "user",
$authProvider: "emailpass",
...payload,
}) as Promise<{ token: string }>,
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useResetPasswordForEmailPass = (
options?: UseMutationOptions<
InferClientOutput<
typeof sdk.auth.$actorType.$authProvider.resetPassword.mutate
>,
ClientError,
{ email: string }
>
) => {
return useMutation({
mutationFn: (payload) =>
sdk.auth.$actorType.$authProvider.resetPassword.mutate({
$actorType: "user",
$authProvider: "emailpass",
identifier: payload.email,
metadata: {},
}),
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useLogout = (
options?: UseMutationOptions<
void,
ClientError
>
) => {
return useMutation({
mutationFn: async () => clearAuthToken(),
...options,
})
}
export const useUpdateProviderForEmailPass = (
token: string,
options?: UseMutationOptions<
InferClientOutput<typeof sdk.auth.$actorType.$authProvider.update.mutate>,
ClientError,
Omit<
InferClientInput<typeof sdk.auth.$actorType.$authProvider.update.mutate>,
"$actorType" | "$authProvider"
>
>
) => {
return useMutation({
mutationFn: (payload) =>
sdk.auth.$actorType.$authProvider.update.mutate({
$actorType: "user",
$authProvider: "emailpass",
...payload,
fetchOptions: {
headers: {
Authorization: `Bearer ${token}`,
},
},
}),
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context)
},
...options,
})
}