useAuth()
Main authentication hook for client components. Provides authentication state and methods.
Overview
The useAuth() hook provides access to authentication state including isAuthenticated, isLoading, user, and error, along with methods like signIn(), signOut(), and refreshSession() for controlling the authentication flow in client components.
Basic Usage
'use client';
import { useAuth } from 'authsafe-nextjs';
export default function Dashboard() {
const { isAuthenticated, user, signIn, signOut } = useAuth();
if (!isAuthenticated) {
return <button onClick={() => signIn()}>Sign In</button>;
}
return (
<div>
<p>Welcome, {user?.name}</p>
<button onClick={() => signOut()}>Sign Out</button>
</div>
);
}