Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor reset password to RQ and update ForgotPasswordForm to use it

Joshen Lim434b0ab556b4
diff --git a/studio/components/interfaces/SignIn/ForgotPasswordForm.tsx b/studio/components/interfaces/SignIn/ForgotPasswordForm.tsx
index ff75d6bf90c..08f519c3053 100644
--- a/studio/components/interfaces/SignIn/ForgotPasswordForm.tsx
+++ b/studio/components/interfaces/SignIn/ForgotPasswordForm.tsx
@@ -1,12 +1,13 @@
import HCaptcha from '@hcaptcha/react-hcaptcha'
-import { useStore } from 'hooks'
-import { isResponseOk, post } from 'lib/common/fetch'
-import { API_URL, BASE_PATH } from 'lib/constants'
import { useRouter } from 'next/router'
import { useRef, useState } from 'react'
import { Button, Form, Input } from 'ui'
import { object, string } from 'yup'
+import { useResetPasswordMutation } from 'data/misc/reset-password-mutation'
+import { useStore } from 'hooks'
+import { BASE_PATH } from 'lib/constants'
+
const forgotPasswordSchema = object({
email: string().email('Must be a valid email').required('Email is required'),
})
@@ -18,46 +19,41 @@ const ForgotPasswordForm = () => {
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const captchaRef = useRef<HCaptcha>(null)
- const onForgotPassword = async ({ email }: { email: string }) => {
- const toastId = ui.setNotification({
- category: 'loading',
- message: `Sending password reset email...`,
- })
+ const { mutate: resetPassword, isLoading } = useResetPasswordMutation({
+ onSuccess: async () => {
+ ui.setNotification({
+ category: 'success',
+ message: `If you registered using your email and password, you will receive a password reset email.`,
+ })
+ await router.push('/sign-in')
+ },
+ onError: (error) => {
+ setCaptchaToken(null)
+ captchaRef.current?.resetCaptcha()
+
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to send reset email: ${error.message}`,
+ })
+ },
+ })
+ const onForgotPassword = async ({ email }: { email: string }) => {
let token = captchaToken
if (!token) {
const captchaResponse = await captchaRef.current?.execute({ async: true })
token = captchaResponse?.response ?? null
}
- const response = await post<void>(`${API_URL}/reset-password`, {
+ resetPassword({
email,
- hcaptchaToken: token ?? undefined,
+ hcaptchaToken: token,
redirectTo: `${
process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
? location.origin
: process.env.NEXT_PUBLIC_SITE_URL
}${BASE_PATH}/reset-password`,
})
-
- if (isResponseOk(response)) {
- ui.setNotification({
- id: toastId,
- category: 'success',
- message: `If you registered using your email and password, you will receive a password reset email.`,
- })
-
- await router.push('/sign-in')
- } else {
- setCaptchaToken(null)
- captchaRef.current?.resetCaptcha()
-
- ui.setNotification({
- id: toastId,
- category: 'error',
- message: `Failed to send reset email: ${response.error.message}`,
- })
- }
}
return (
@@ -68,7 +64,7 @@ const ForgotPasswordForm = () => {
validationSchema={forgotPasswordSchema}
onSubmit={onForgotPassword}
>
- {({ isSubmitting }: { isSubmitting: boolean }) => {
+ {() => {
return (
<div className="flex flex-col pt-4 space-y-4">
<Input
@@ -77,7 +73,7 @@ const ForgotPasswordForm = () => {
type="email"
label="Email"
placeholder="[email protected]"
- disabled={isSubmitting}
+ disabled={isLoading}
autoComplete="email"
/>
@@ -102,8 +98,8 @@ const ForgotPasswordForm = () => {
form="forgot-password-form"
htmlType="submit"
size="medium"
- disabled={isSubmitting}
- loading={isSubmitting}
+ disabled={isLoading}
+ loading={isLoading}
>
Send Reset Email
</Button>
diff --git a/studio/data/misc/reset-password-mutation.ts b/studio/data/misc/reset-password-mutation.ts
new file mode 100644
index 00000000000..c9f1e6cbb12
--- /dev/null
+++ b/studio/data/misc/reset-password-mutation.ts
@@ -0,0 +1,49 @@
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+
+export type ResetPasswordVariables = {
+ email: string
+ hcaptchaToken: string | null
+ redirectTo: string
+}
+
+export async function resetPassword({ email, hcaptchaToken, redirectTo }: ResetPasswordVariables) {
+ const { data, error } = await post('/platform/reset-password', {
+ // @ts-ignore
+ body: { email, hcaptchaToken, redirectTo },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type ResetPasswordData = Awaited<ReturnType<typeof resetPassword>>
+
+export const useResetPasswordMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<ResetPasswordData, ResponseError, ResetPasswordVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<ResetPasswordData, ResponseError, ResetPasswordVariables>(
+ (vars) => resetPassword(vars),
+ {
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to reset password: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}