Skip to content

useLiteAuth

React hook that reads auth state from the nearest <LiteAuthProvider>.

Import

ts
import { useLiteAuth } from "next-lite-auth/client";

Signature

ts
function useLiteAuth(): {
  user: PublicUser | null;
  loading: boolean;
  login: (creds: { email: string; password: string }) => Promise<{ error?: string }>;
  logout: () => Promise<void>;
}

Return values

FieldTypeDescription
userPublicUser | nullAuthenticated user, or null
loadingbooleantrue while the initial /me fetch is in flight
loginFunctionSubmits credentials and updates state
logoutFunctionClears the cookie and sets user to null

Example

tsx
"use client";
import { useLiteAuth } from "next-lite-auth/client";

export function AuthButton() {
  const { user, loading, login, logout } = useLiteAuth();

  if (loading) return null;

  if (user) {
    return <button onClick={logout}>Logout ({user.email})</button>;
  }

  return (
    <button onClick={() => login({ email: "admin@example.com", password: "secret" })}>
      Login
    </button>
  );
}

WARNING

Must be used inside <LiteAuthProvider>. Throws if called outside.

See also

Released under the MIT License.