@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
207 lines • 6.15 kB
TypeScript
import { Session, User, UserType } from '@frank-auth/client';
import { AuthSDK } from '@frank-auth/sdk';
import { NextRequest, NextResponse } from 'next/server';
import { FrankAuthConfig } from '../types';
export interface MiddlewareConfig extends Omit<FrankAuthConfig, "enableDevMode"> {
storageKeyPrefix?: string;
sessionCookieName?: string;
userType?: UserType;
projectId?: string;
secretKey?: string;
/**
* Paths that are publicly accessible without authentication
* @default []
*/
publicPaths?: string[];
/**
* Paths that require authentication (when allPathsPrivate is false)
* @default []
*/
privatePaths?: string[];
/**
* Authentication paths that redirect authenticated users away
* These paths are accessible to unauthenticated users but will
* redirect authenticated users to afterSignInPath
* @default ['/sign-in', '/sign-up', '/forgot-password', '/verify-email', '/reset-password']
*/
authPaths?: string[];
/**
* Whether all paths are private by default (recommended)
* @default true
*/
allPathsPrivate?: boolean;
/**
* Path to redirect to for sign in
* @default '/sign-in'
*/
signInPath?: string;
/**
* Path to redirect to for sign up
* @default '/sign-up'
*/
signUpPath?: string;
/**
* Path to redirect to after successful sign in
* @default '/dashboard'
*/
afterSignInPath?: string;
/**
* Path to redirect to after successful sign up
* @default '/dashboard'
*/
afterSignUpPath?: string;
/**
* Path to redirect to after sign out
* @default '/'
*/
afterSignOutPath?: string;
/**
* Organization selection path for multi-tenant apps
* @default '/select-organization'
*/
orgSelectionPath?: string;
/**
* Custom matcher function for protected routes
*/
matcher?: (path: string) => boolean;
/**
* Enable debug logging
* @default false
*/
debug?: boolean;
/**
* Custom domain for organization detection
*/
customDomain?: string;
/**
* Enable organization-based routing
* @default false
*/
enableOrgRouting?: boolean;
/**
* Ignore paths (will not be processed by middleware)
* @default ['/api', '/_next', '/favicon.ico']
*/
ignorePaths?: string[];
/**
* Cookie options for session management
*/
cookieOptions?: {
secure?: boolean;
httpOnly?: boolean;
sameSite?: "strict" | "lax" | "none";
domain?: string;
maxAge?: number;
};
/**
* Custom hooks for middleware lifecycle
*/
hooks?: MiddlewareHooks;
/**
* Skip API calls on network errors (useful for development)
* @default false
*/
skipApiCallOnNetworkError?: boolean;
/**
* Maximum number of retries for API calls
* @default 2
*/
maxRetries?: number;
/**
* Timeout for API calls in milliseconds
* @default 5000
*/
apiTimeout?: number;
/**
* Fallback to local token validation on network errors
* @default true
*/
fallbackToLocalTokens?: boolean;
/**
* Custom API endpoint override for testing
*/
customApiEndpoint?: string;
/**
* Enable offline mode (skip all API calls)
* @default false
*/
offlineMode?: boolean;
}
export interface MiddlewareHooks {
/**
* Called before authentication check
*/
beforeAuth?: (req: NextRequest) => Promise<NextRequest | NextResponse>;
/**
* Called after authentication check
*/
afterAuth?: (req: NextRequest, res: NextResponse, auth: AuthResult) => Promise<NextRequest | NextResponse>;
/**
* Called when user is authenticated
*/
onAuthenticated?: (req: NextRequest, user: User, session: Session) => Promise<NextRequest | NextResponse>;
/**
* Called when user is not authenticated
*/
onUnauthenticated?: (req: NextRequest) => Promise<NextRequest | NextResponse>;
/**
* Called when organization is required but not selected
*/
onOrganizationRequired?: (req: NextRequest, user: User) => Promise<NextRequest | NextResponse>;
/**
* Called on authentication error
*/
onError?: (req: NextRequest, error: Error) => Promise<NextRequest | NextResponse>;
}
export interface AuthResult {
isAuthenticated: boolean;
user: User | null;
session: Session | null;
organizationId: string | null;
error: Error | null;
tokenInfo?: {
accessTokenExpired: boolean;
refreshTokenExpired: boolean;
canRefresh: boolean;
};
}
export interface MiddlewareContext {
req: NextRequest;
config: Required<MiddlewareConfig>;
auth: AuthResult;
authSDK: AuthSDK;
path: string;
isPublicPath: boolean;
isPrivatePath: boolean;
isAuthPath: boolean;
response: NextResponse;
}
/**
* Create Frank Auth middleware for Next.js
*/
export declare function createFrankAuthMiddleware(userConfig: MiddlewareConfig): (req: NextRequest) => Promise<NextResponse>;
/**
* Create a custom matcher function for complex routing logic
*/
export declare function createMatcher(patterns: {
include?: string[];
exclude?: string[];
custom?: (path: string) => boolean;
}): (path: string) => boolean;
/**
* Utility to check if user has specific permission in middleware
*/
export declare function checkPermission(req: NextRequest, permission: string, config: MiddlewareConfig): Promise<boolean>;
/**
* Utility to get organization from request
*/
export declare function getOrganizationFromRequest(req: NextRequest, config: MiddlewareConfig): string | null;
/**
* Utility to get AuthSDK instance in middleware context
*/
export declare function getAuthSDKFromRequest(req: NextRequest, config: MiddlewareConfig): AuthSDK;
/**
* Utility to check authentication status without redirecting
*/
export declare function checkAuthStatus(req: NextRequest, config: MiddlewareConfig): Promise<AuthResult>;
//# sourceMappingURL=index.d.ts.map