useForgotPassword
useForgotPassword
calls forgotPassword
method from authProvider
under the hood.
It returns the result of react-query
's useMutation which includes many properties, some of which being isSuccess and isError.
Data that is resolved from forgotPassword
will be returned as the data
in the query result with the following type:
type AuthActionResponse = {
success: boolean;
redirectTo?: string;
error?: Error;
[key: string]: unknown;
};
success
: A boolean indicating whether the operation was successful. Ifsuccess
is false, a notification will be shown.- When
error
is provided, the notification will contain the error message and name. Otherwise, a generic error message will be shown with the following values{ name: "Forgot Password Error", message: "Invalid credentials" }
.
- When
redirectTo
: If has a value, the app will be redirected to the given URL.error
: If has a value, a notification will be shown with the error message and name.[key: string]
: Any additional data you wish to include in the response, keyed by a string identifier.
Usageβ
Normally refine provides a default forgot password page. If you prefer to use this default forgot password page, there is no need to handle forgot password flow manually.
If we want to build a custom forgotPassword page instead of the default one that comes with refine, useForgotPassword
can be used like this:
import { useForgotPassword } from "@refinedev/core";
type forgotPasswordVariables = {
email: string;
};
export const forgotPasswordPage = () => {
const { mutate: forgotPassword } =
useForgotPassword<forgotPasswordVariables>();
const onSubmit = (values: forgotPasswordVariables) => {
forgotPassword(values);
};
return (
<form onSubmit={onSubmit}>
<label>Email</label>
<input name="email" value="test@refine.com" />
<button type="submit">Submit</button>
</form>
);
};
mutate
acquired from useForgotPassword
can accept any kind of object for values since forgotPassword
method from authProvider
doesn't have a restriction on its parameters.
A type parameter for the values can be provided to useForgotPassword
.
import { useForgotPassword } from "@refinedev/core";
const { mutate: forgotPassword } = useForgotPassword<{ email: string }>();
Redirection after forgotPasswordβ
A custom URL can be given to mutate the function from the useForgotPassword
hook if you want to redirect yourself to a certain URL.
import { useForgotPassword } from "@refinedev/core";
const { mutate: forgotPassword } = useForgotPassword();
forgotPassword({ redirectPath: "/custom-url" });
Then, you can handle this URL in your forgotPassword
method of the authProvider
.
import type { AuthBindings } from "@refinedev/core";
const authProvider: AuthBindings = {
// ---
forgotPassword: async ({ redirectPath }) => {
// ---
return {
success: true,
redirectTo: redirectPath,
};
},
};
This hook can only be used if authProvider
is provided.