UNPKG

authrix

Version:

Lightweight, flexible authentication library for Node.js and TypeScript.

260 lines (257 loc) 8.45 kB
export { a as authConfig, g as getAuthrixStatus, i as initAuth, b as isAuthrixInitialized } from './index-Bwhj_bJW.js'; export { A as AuthDbAdapter, a as AuthUser } from './db-R-GJxQGc.js'; /** * Get information about the detected Next.js environment * Useful for debugging Next.js detection issues */ declare function getNextJsEnvironmentInfo(): { isNextJsAvailable: boolean; context: "app-router" | "pages-router" | "middleware" | "unknown"; hasAppRouterSupport: boolean; hasPagesRouterSupport: boolean; hasMiddlewareSupport: boolean; detectionComplete: boolean; runtimeInfo: { hasRequire: boolean; hasGlobalThis: boolean; hasProcess: boolean; nextRuntime: string | undefined; hasNextData: boolean; }; }; /** * Sign up a user in Next.js App Router */ declare function signupNextApp(email: string, password: string): Promise<{ id: string; email: string; }>; /** * Sign in a user in Next.js App Router */ declare function signinNextApp(email: string, password: string): Promise<{ id: string; email: string; }>; /** * Log out a user in Next.js App Router */ declare function logoutNextApp(): { message: string; }; /** * Get current user in Next.js App Router */ declare function getCurrentUserNextApp(): Promise<{ id: string; email: string; createdAt: Date | undefined; } | null>; /** * Check if user is authenticated in Next.js App Router */ declare function isAuthenticatedNextApp(): Promise<boolean>; /** * Sign up a user in Next.js Pages Router API */ declare function signupNextPages(email: string, password: string, res: any): Promise<{ id: string; email: string; }>; /** * Sign in a user in Next.js Pages Router API */ declare function signinNextPages(email: string, password: string, res: any): Promise<{ id: string; email: string; }>; /** * Log out a user in Next.js Pages Router API */ declare function logoutNextPages(res: any): { message: string; }; /** * Get current user in Next.js Pages Router API */ declare function getCurrentUserNextPages(req: any): Promise<{ id: string; email: string; createdAt: Date | undefined; } | null>; /** * Check if user is authenticated in Next.js Pages Router API */ declare function isAuthenticatedNextPages(req: any): Promise<boolean>; /** * Check authentication in Next.js middleware (Edge Runtime Compatible) * This version only performs basic token structure validation and expiration checks * without verifying the JWT signature or making database calls */ declare function checkAuthMiddleware(request: any, options?: { cookieName?: string; }): Promise<{ isAuthenticated: boolean; user: { id: any; email: any; createdAt: Date | undefined; } | null; reason: string; }>; /** * Edge Runtime compatible middleware function that validates tokens server-side * This version makes an API call to validate the token properly with signature verification */ declare function checkAuthMiddlewareSecure(request: any, options?: { validationEndpoint?: string; timeout?: number; cookieName?: string; }): Promise<{ isAuthenticated: boolean; user: any; reason: string; }>; declare function withAuth<T extends Record<string, any>, U extends Record<string, any>>(handler: (req: T & { user: { id: string; email: string; createdAt?: Date; }; }, res: U) => Promise<void> | void): (req: T, res: U) => Promise<any>; /** * Flexible Next.js signup that works in both App Router and Pages Router * Automatically detects the context and uses the appropriate method */ declare function signupNextFlexible(email: string, password: string, res?: any): Promise<{ id: string; email: string; }>; /** * Flexible Next.js signin that works in both App Router and Pages Router */ declare function signinNextFlexible(email: string, password: string, res?: any): Promise<{ id: string; email: string; }>; /** * Flexible Next.js get current user that works in both App Router and Pages Router */ declare function getCurrentUserNextFlexible(req?: any): Promise<{ id: string; email: string; createdAt: Date | undefined; } | null>; /** * Create an authenticated NextResponse with user info */ declare function createAuthenticatedResponse(response: any, user: { id: string; email: string; createdAt?: Date; }): any; /** * Helper function to create API validation endpoint for secure middleware * Use this in your API route to validate tokens server-side * * Example usage in pages/api/auth/validate.ts or app/api/auth/validate/route.ts: * * ```typescript * import { createTokenValidationHandler } from 'authrix/nextjs'; * * export const POST = createTokenValidationHandler(); * ``` */ declare function createTokenValidationHandler(): (request: Request) => Promise<Response>; /** * Pages Router version of token validation handler * * Example usage in pages/api/auth/validate.ts: * * ```typescript * import { createTokenValidationHandlerPages } from 'authrix/nextjs'; * * export default createTokenValidationHandlerPages(); * ``` */ declare function createTokenValidationHandlerPages(): (req: any, res: any) => Promise<any>; /** * Manually re-run Next.js environment detection * Useful when the environment might have changed or for debugging detection issues */ declare function redetectNextJsEnvironment(): { isNextJsAvailable: boolean; context: "app-router" | "pages-router" | "middleware" | "unknown"; hasAppRouterSupport: boolean; hasPagesRouterSupport: boolean; hasMiddlewareSupport: boolean; detectionComplete: boolean; runtimeInfo: { hasRequire: boolean; hasGlobalThis: boolean; hasProcess: boolean; nextRuntime: string | undefined; hasNextData: boolean; }; }; /** * Force Next.js availability (use with caution) * This can be used to override detection in environments where automatic detection fails */ declare function forceNextJsAvailability(available: boolean, context?: 'app-router' | 'pages-router' | 'middleware' | 'unknown'): void; /** * Simple Next.js signup for App Router - uses dynamic import * Call this from Server Components, Server Actions, or Route Handlers */ declare function signupNext(email: string, password: string): Promise<{ id: string; email: string; }>; /** * Simple Next.js signin for App Router - uses dynamic import * Call this from Server Components, Server Actions, or Route Handlers */ declare function signinNext(email: string, password: string): Promise<{ id: string; email: string; }>; /** * Simple Next.js logout for App Router - uses dynamic import * Call this from Server Components, Server Actions, or Route Handlers */ declare function logoutNext(): { message: string; }; /** * Simple Next.js get current user for App Router - uses dynamic import * Call this from Server Components, Server Actions, or Route Handlers */ declare function getCurrentUserNext(): Promise<{ id: string; email: string; createdAt: Date | undefined; } | null>; /** * Production-ready authentication check that always works * Returns false instead of throwing errors for better UX */ declare function isAuthenticatedNext(): Promise<boolean>; /** * Helper to create cookie string for manual setting * Use this when you want to handle cookie setting manually */ declare function createAuthCookieString(token: string, options?: { secure?: boolean; maxAge?: number; sameSite?: string; path?: string; }): string; /** * Helper to create logout cookie string for manual clearing */ declare function createLogoutCookieString(options?: { secure?: boolean; sameSite?: string; path?: string; }): string; export { checkAuthMiddleware, checkAuthMiddlewareSecure, createAuthCookieString, createAuthenticatedResponse, createLogoutCookieString, createTokenValidationHandler, createTokenValidationHandlerPages, forceNextJsAvailability, getCurrentUserNext, getCurrentUserNextApp, getCurrentUserNextFlexible, getCurrentUserNextPages, getNextJsEnvironmentInfo, isAuthenticatedNext, isAuthenticatedNextApp, isAuthenticatedNextPages, logoutNext, logoutNextApp, logoutNextPages, redetectNextJsEnvironment, signinNext, signinNextApp, signinNextFlexible, signinNextPages, signupNext, signupNextApp, signupNextFlexible, signupNextPages, withAuth };