一键导入
authentication
Implement authentication with NextAuth v5. Use when adding login/logout, checking sessions, protecting API routes, server actions, or pages.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Implement authentication with NextAuth v5. Use when adding login/logout, checking sessions, protecting API routes, server actions, or pages.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Create robust, error-proof Prisma seed scripts with comprehensive error handling and validation.
Create Next.js 16 API Route Handlers. Use when building REST endpoints (GET, POST, PUT, DELETE), implementing CRUD operations, or creating authenticated APIs with Zod validation.
Create React/Next.js 16 components. Use when building pages, client/server components, forms with useActionState, or UI with shadcn/ui. ALWAYS activate with frontend-design together.
Unit tests with Jest + React Testing Library. CRITICAL - Uses Jest (NOT Vitest).
Write integration tests with Jest for API routes and database operations. Use when testing backend logic, API endpoints, or data layer.
Review code for quality, security, performance, and best practices. Use when reviewing changes before commit, auditing code for issues, or suggesting improvements.
| name | authentication |
| description | Implement authentication with NextAuth v5. Use when adding login/logout, checking sessions, protecting API routes, server actions, or pages. |
This skill guides implementation of authentication using NextAuth v5, which is pre-configured in the boilerplate.
The user needs to protect routes, verify sessions, implement login/logout, or add role-based access control.
The authentication system is already configured:
src/auth.ts - Contains NextAuth configurationCRITICAL: Always check session before sensitive operations. Never trust client-side auth state for security decisions.
Use auth() from @/auth for all server-side auth checks:
import { auth } from '@/auth';
import { ApiErrors } from '@/lib/api-response';
// In API routes
const session = await auth();
if (!session) throw ApiErrors.unauthorized();
const userId = session.user.id;
// In Server Actions
'use server';
import { auth } from '@/auth';
const session = await auth();
if (!session) return { success: false, error: 'Unauthorized' };
// In Pages (Server Component)
import { auth } from '@/auth';
import { redirect } from 'next/navigation';
const session = await auth();
if (!session) redirect('/login');
Use useSession() from next-auth/react for client components:
'use client';
import { useSession, signIn, signOut } from 'next-auth/react';
export function AuthButton() {
const { data: session, status } = useSession();
if (status === 'loading') return <div>Loading...</div>;
if (session) {
return (
<div>
<span>{session.user.username}</span>
<button onClick={() => signOut()}>Sign Out</button>
</div>
);
}
return <button onClick={() => signIn()}>Sign In</button>;
}
'use client';
import { signIn } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
export function LoginForm() {
const router = useRouter();
const [error, setError] = useState('');
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const result = await signIn('credentials', {
username: formData.get('username'),
password: formData.get('password'),
redirect: false,
});
if (result?.error) {
setError('Invalid credentials');
} else {
router.push('/dashboard');
}
}
return (
<form onSubmit={handleSubmit}>
<input name="username" placeholder="Username" required />
<input name="password" type="password" placeholder="Password" required />
{error && <p className="text-destructive">{error}</p>}
<button type="submit">Login</button>
</form>
);
}
Check user roles for authorization:
// In API Route
const session = await auth();
if (!session) throw ApiErrors.unauthorized();
if (session.user.role !== 'ADMIN') throw ApiErrors.forbidden();
// In Server Component
const session = await auth();
if (session?.user.role !== 'ADMIN') {
return <div>Access denied</div>;
}
return <AdminPanel />;
session.user.idif (!session) throw ApiErrors.unauthorized()session.user.role === 'ADMIN'if (!session) redirect('/login')if (status === 'loading') return <Spinner />NEVER:
useSession in Server ComponentsIMPORTANT: The SessionProvider is already configured in the boilerplate layout. You don't need to add it again.