HelloJohn / docs
SDKsReact SDK

useAuth

React hook for authentication operations — sign in, sign up, sign out, and OAuth flows with the HelloJohn React SDK.

useAuth

The useAuth hook provides methods for authentication operations: sign in, sign up, sign out, password reset, and social OAuth flows.

import { useAuth } from "@hellojohn/react";

const {
  signIn,
  signUp,
  signOut,
  signInWithOAuth,
  sendPasswordReset,
  sendMagicLink,
  isLoading,
} = useAuth();

Methods

signIn

Authenticate with email and password:

const { error } = await signIn({
  email: "jane@example.com",
  password: "SecurePass123!",
});

Parameters:

ParameterTypeRequiredDescription
emailstringUser email
passwordstringUser password
redirectTostringNoURL to redirect after sign-in

Returns: Promise<{ error: HelloJohnError | null }>

If MFA is required, returns { error: null, mfaRequired: true, challengeId: "..." }. Proceed with useMFA().verify().

signUp

Create a new user account:

const { error } = await signUp({
  email: "jane@example.com",
  password: "SecurePass123!",
  firstName: "Jane",
  lastName: "Doe",
});

Parameters:

ParameterTypeRequiredDescription
emailstringUser email
passwordstringPassword
firstNamestringNoFirst name
lastNamestringNoLast name
redirectTostringNoRedirect after email verification
metadataobjectNoCustom user metadata

signOut

Sign out the current user:

await signOut();

// Sign out all sessions (across all devices)
await signOut({ scope: "global" });

Parameters:

ParameterTypeDefaultDescription
scope"local" | "global""local"Revoke current session only, or all sessions

signInWithOAuth

Redirect to a social login provider:

await signInWithOAuth({
  provider: "google",
  redirectTo: "/dashboard",
});

Parameters:

ParameterTypeRequiredDescription
provider"google" | "github" | "microsoft" | "apple" | "discord" | "linkedin"OAuth provider
redirectTostringNoPost-authentication redirect

Send a passwordless magic link to an email:

const { error } = await sendMagicLink({
  email: "jane@example.com",
  redirectTo: "/dashboard",
});

sendPasswordReset

Send a password reset email:

const { error } = await sendPasswordReset({
  email: "jane@example.com",
});

State

PropertyTypeDescription
isLoadingbooleantrue while an auth operation is in progress

Full Sign-In Example

import { useAuth } from "@hellojohn/react";
import { useState } from "react";

function SignInForm() {
  const { signIn, isLoading } = useAuth();
  const [error, setError] = useState<string | null>(null);

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    const form = new FormData(e.currentTarget);
    setError(null);

    const { error } = await signIn({
      email: form.get("email") as string,
      password: form.get("password") as string,
    });

    if (error) setError(error.message);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input name="email" type="email" required />
      <input name="password" type="password" required />
      {error && <p className="error">{error}</p>}
      <button type="submit" disabled={isLoading}>
        {isLoading ? "Signing in..." : "Sign in"}
      </button>
    </form>
  );
}

On this page