useAuth()
クライアントコンポーネントの主要な認証フックです。認証状態と認証方法を提供します。
概要
useAuth() フックは、isAuthenticated、isLoading、user、error などの認証状態へのアクセスを提供するとともに、クライアントコンポーネントでの認証フローを制御するための signIn()、signOut()、refreshSession() などのメソッドを提供します。
基本的な使い方
'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>
);
}