SimpleAuth
ReactHooks

useResetPassword

Complete password reset with token and new password.

Overview

useResetPassword wraps auth.resetPassword({ token, password }) with loading, success, and error helpers.

Signature

export function useResetPassword()

Parameters

This hook takes no parameters.

Returns

FieldTypeDescription
resetPassword(input: { token: string; password: string }) => Promise<unknown>Submits the new password with the reset token.
dataunknownLast successful response when present.
errorstring | nullHuman-readable error when the request fails.
isErrorbooleanConvenience flag from error.
isLoadingbooleanTrue while the reset request runs.
isSuccessbooleanTrue after a successful reset.
reset() => voidClears hook state.

Example

"use client"

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

export function FinishReset({ token }: { token: string }) {
  const { resetPassword, isLoading, error } = useResetPassword()

  async function onSubmit(password: string) {
    await resetPassword({ token, password })
  }

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault()
        const fd = new FormData(e.currentTarget)
        void onSubmit(String(fd.get("password") ?? ""))
      }}
    >
      <input name="password" type="password" required minLength={8} />
      {error ? <p>{error}</p> : null}
      <button type="submit" disabled={isLoading}>
        {isLoading ? "Updating…" : "Update password"}
      </button>
    </form>
  )
}

How it works

  • Uses useSimpleAuthClient() and normalizes failures into error.

Shipped by

npx @simpleauthjs/react add reset-password

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,
  }
}

On this page