SimpleAuth
ReactComponents

ResetPasswordForm

Set a new password using the token from the reset email.

Overview

ResetPasswordForm reads the reset token from your app (usually a query string on /reset-password) and calls useResetPassword to set a new password. After success it can link or navigate back to sign-in.

Preview

Interactive preview requires JavaScript.

Install

npx @simpleauthjs/react add reset-password --style minimal

The install snippet uses --style minimal. You can also use --style modern. Style variants compares those two in the live preview. Run npx @simpleauthjs/react add --help for every --style slug the CLI accepts.

Props

NameTypeRequiredDescription
tokenstringYesPass the token your app reads from the reset URL.
onSuccess() => voidNoCalled after a successful reset.
signInHrefstringNoOn success, shows a “Back to sign in” link when onSwitchToSignIn is not set.
onSwitchToSignIn() => voidNoOn success, shows a button that runs this callback instead of an anchor.

Usage

In the App Router, read the token from searchParams in a small client wrapper or pass it from a server page into a client component.

"use client"

import { ResetPasswordForm } from "@/components/simpleauth"

export function ResetPasswordClient({ token }: { token: string }) {
  return <ResetPasswordForm token={token} signInHref="/login" />
}
// app/reset-password/page.tsx
import { ResetPasswordClient } from "./reset-password-client"

export default function Page({ searchParams }: { searchParams: { token?: string } }) {
  const token = searchParams.token ?? ""
  if (!token) {
    return <p>Invalid or missing reset link.</p>
  }
  return <ResetPasswordClient token={token} />
}

Source

components/simpleauth/hooks/use-reset-password.ts
"use client"

import { useState } from "react"
import { SimpleAuthError, type SuccessResponse } from "@simpleauthjs/core"
import { useSimpleAuthClient } from "../provider"

function getErrorMessage(error: unknown) {
  if (error instanceof SimpleAuthError) {
    return error.message
  }

  if (error instanceof Error) {
    return error.message
  }

  return "Unable to reset password."
}

export function useResetPassword() {
  const auth = useSimpleAuthClient()
  const [isLoading, setIsLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [data, setData] = useState<SuccessResponse | null>(null)

  async function resetPassword(input: { token: string; password: string }) {
    setIsLoading(true)
    setError(null)

    try {
      const response = await auth.resetPassword(input)
      setData(response)
      return response
    } catch (requestError) {
      setData(null)
      setError(getErrorMessage(requestError))
      throw requestError
    } finally {
      setIsLoading(false)
    }
  }

  function reset() {
    setData(null)
    setError(null)
    setIsLoading(false)
  }

  return {
    resetPassword,
    data,
    error,
    isError: Boolean(error),
    isLoading,
    isSuccess: Boolean(data?.success),
    reset,
  }
}

Customisation

Owned by you

Files live at components/simpleauth/ui/reset-password-form.tsx and components/simpleauth/styles/reset-password-form.css.

On this page