Skip to main content

useLogin

The useLogin hook handles the login mutation.

import { useLogin } from '@archibald/auth';

const { login, isLoading, error } = useLogin(OPTIONS);

Parameters

NameTypeRequiredDescription
optionsMutateOptionsOptions that define the behavior of the useMutation hook used internally.

Return value

PropertyTypeDescription
loginFunctionFunction to trigger the login. Receives credentials as parameters.
isLoadingbooleanIndicates if the login request is in progress.
errorError | nullError object if the login failed.
isDonebooleanReturns true when the login request finished.
resetFunctionReset the mutation state.

Usage Example

import { useLogin } from '@archibald/auth';

function LoginForm() {
const { login, isLoading, error } = useLogin();

const onSubmit = async (e) => {
e.preventDefault();
const { email, password } = e.target.elements;
await login({ email: email.value, password: password.value });
};

return (
<form onSubmit={onSubmit}>
<input name="email" type="email" required />
<input name="password" type="password" required />
<button type="submit" disabled={isLoading}>
{isLoading ? 'Logging in...' : 'Log In'}
</button>
{error && <p className="error">{error.message}</p>}
</form>
);
}

Relates to

  • SessionClient: Uses sessionClient.logIn() to perform the authentication.
  • useMutation: Internally uses @archibald/client's useMutation to handle the request state.
  • useUser: A successful login will automatically trigger a state update that useUser reflects.