Skip to main content
useLogin is a hook that calls the login method from the authProvider under the hood. It returns a mutation result from @tanstack/react-query.

Usage

import { useLogin } from "@refinedev/core";

const { mutate: login } = useLogin();

login({
  email: "[email protected]",
  password: "123456",
});

Parameters

mutationOptions
UseMutationOptions
Options to configure the mutation from @tanstack/react-query.
mutationOptions.onSuccess
function
Callback function to be called on successful login.
mutationOptions.onError
function
Callback function to be called on login error.
mutationOptions.meta
object
Additional metadata to pass to the mutation.

Return Values

Returns a mutation result from @tanstack/react-query.
mutate
function
A function to trigger the login mutation. Accepts login credentials as a parameter.
mutateAsync
function
An async version of mutate that returns a Promise.
isPending
boolean
Indicates whether the login mutation is in progress.
isSuccess
boolean
Indicates whether the login mutation was successful.
isError
boolean
Indicates whether the login mutation encountered an error.
data
AuthActionResponse
The response data from the login mutation.
data.success
boolean
Indicates whether the login was successful.
data.redirectTo
string
The URL to redirect to after successful login.
data.error
Error | RefineError
Error object if login failed.
data.successNotification
SuccessNotificationResponse
Notification message to display on successful login.
error
Error | RefineError
Error object if the mutation failed.

Type Parameters

TVariables
object
default:"{}"
Type of the variables object passed to the login mutation. This is the shape of the data you pass when calling the mutate function.

Examples

Basic Usage

import { useLogin } from "@refinedev/core";
import { Form, Input, Button } from "antd";

interface ILoginForm {
  email: string;
  password: string;
}

const LoginPage = () => {
  const { mutate: login, isPending } = useLogin<ILoginForm>();

  const onFinish = (values: ILoginForm) => {
    login(values);
  };

  return (
    <Form onFinish={onFinish}>
      <Form.Item name="email" rules={[{ required: true }]}>
        <Input placeholder="Email" />
      </Form.Item>
      <Form.Item name="password" rules={[{ required: true }]}>
        <Input type="password" placeholder="Password" />
      </Form.Item>
      <Button type="primary" htmlType="submit" loading={isPending}>
        Sign in
      </Button>
    </Form>
  );
};

With Custom Callbacks

import { useLogin } from "@refinedev/core";

const { mutate: login } = useLogin({
  mutationOptions: {
    onSuccess: (data) => {
      console.log("Login successful", data);
    },
    onError: (error) => {
      console.error("Login failed", error);
    },
  },
});

login({
  email: "[email protected]",
  password: "123456",
});

With Google OAuth

import { useLogin } from "@refinedev/core";
import { CredentialResponse, GoogleLogin } from "@react-oauth/google";

const LoginPage = () => {
  const { mutate: login } = useLogin<CredentialResponse>();

  return (
    <GoogleLogin
      onSuccess={(response) => {
        login(response);
      }}
      onError={() => {
        console.log("Login Failed");
      }}
    />
  );
};

Build docs developers (and LLMs) love