KO
베타 번역

@yohangel / auth-next

1.0.0 ▾
MIT
GitHub

Next.js App Router용 Better Auth: 라우트 핸들러, proxy 리디렉션, 서버 세션

코드파일 5개컨텍스트약 545토큰검사 통과

.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로 로그인하세요.