Server-Authentifizierung
Serverseitige Helfer für die Authentifizierung in Server Components, Server Actions und API-Routen.
Überblick
Das serverseitige Authentifizierungsmodul bietet Funktionen zum Zugriff auf Benutzersitzungen, zum Schutz von Routen und zur Verwaltung von Tokens in Server-Kontexten. Funktionen umfassen initAuthSafe(), getAuth(), requireAuth(), currentUser(), getAccessToken(), hasScope() und hasScopes(). Alle Funktionen arbeiten nahtlos mit React Server Components, Server Actions und dem Next.js-Caching-System.
Grundlegende Verwendung
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');
}