ZH
测试版翻译

@yohangel / auth-next

1.0.0 ▾
MIT
GitHub

适用于 Next.js App Router 的 Better Auth:路由处理器、proxy 重定向和服务端会话

代码5 个文件上下文约 545 个 token扫描通过

应用 .genpmignore 后将被注入的确切目录树。固定于

src/lib/auth-next/server.ts只读 · 7145b9d
// Servidor (App Router): instancia de la app con `nextCookies()` (para que las Server Actions puedan fijar cookies),
// handler de las rutas /api/auth/* y helpers de sesión para Server Components, Route Handlers y Server Actions.
import { nextCookies, toNextJsHandler } from 'better-auth/next-js';
import { headers } from 'next/headers.js';
import { redirect } from 'next/navigation.js';
import { type AuthSession, type CreateAuthOptions, createAuth } from '../auth/index.js';

/** Plugins extra van antes de `nextCookies()`, que debe ser el último. */
export function createNextAuth(opts: CreateAuthOptions = {}) {
  return createAuth({ ...opts, plugins: [...(opts.plugins ?? []), nextCookies()] });
}

export const auth = createNextAuth();

/** app/api/auth/[...all]/route.ts → `export const { GET, POST } = authRouteHandlers;` */
export const authRouteHandlers = toNextJsHandler(auth);

/** Sesión de la petición actual (null si no hay). Comprobación real contra la BD (o su caché firmada). */
export async function currentSession(): Promise<AuthSession | null> {
  return auth.api.getSession({ headers: await headers() });
}

/** Exige sesión: si no la hay, redirige a `signInPath` con `?next=` para volver después. */
export async function requireSession(signInPath = '/sign-in', next?: string): Promise<AuthSession> {
  const s = await currentSession();
  if (!s) redirect(next ? `${signInPath}?next=${encodeURIComponent(next)}` : signInPath);
  return s;
}

举报 @yohangel/auth-next

使用 GitHub 登录后才能举报包。