サーバー認証
サーバーコンポーネント、サーバーアクション、およびAPIルートにおける認証のためのサーバーサイドヘルパー。
概要
サーバーサイド認証モジュールは、サーバーコンテキスト内でユーザーセッションへのアクセス、ルートの保護、トークンの管理を行うための機能を提供します。これらの機能には、initAuthSafe()、getAuth()、requireAuth()、currentUser()、getAccessToken()、hasScope()、hasScopes()などがあります。これらの機能はすべて、React Server Components、Server Actions、およびNext.jsのキャッシュシステムとシームレスに連携します。
基本的な使い方
import { getAuth, requireAuth } from 'authsafe-nextjs/server';
// In a Server Component
export default async function Dashboard() {
const auth = await requireAuth();
return <p>Welcome, {auth.name}</p>;
}
// In a Server Action
async function updateProfile(formData) {
'use server';
const auth = await getAuth();
if (!auth) throw new Error('Unauthorized');
}