@tidecloak/nextjs
Version:
TideCloak nextjs SDK
78 lines • 2.58 kB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
import { ProtectedRoutesMap } from './routerMatcher';
interface JWK {
kid: string;
kty: string;
alg: string;
use: string;
crv: string;
x: string;
}
export interface TidecloakConfig {
realm: string;
"auth-server-url": string;
"ssl-required": string;
resource: string;
"public-client": boolean;
"confidential-port": number;
jwk: {
keys: JWK[];
};
[key: string]: unknown;
}
/**
* Configuration options for TideCloak middleware.
*
* - `config`: Your Tidecloak client adapter JSON.
* - `protectedRoutes`: Map of path patterns to arrays of required roles.
* - `onRequest`, `onSuccess`, `onFailure`, `onError`: Lifecycle hooks for custom logic.
*/
export interface TideMiddlewareOptions {
/** Tidecloak client adapter JSON (downloaded from your Tidecloak realm settings) */
config: TidecloakConfig;
/** Routes requiring a verified token and specific roles */
protectedRoutes?: ProtectedRoutesMap;
/** Called before any auth logic; return a Response to short‑circuit */
onRequest?: (ctx: {
token: string | null;
}, req: NextRequest) => NextResponse | void;
/** Called after successful auth and role checks; return a Response to override */
onSuccess?: (ctx: {
payload: Record<string, any>;
}, req: NextRequest) => NextResponse | void;
/** Called when auth or role check fails; return a Response to override */
onFailure?: (ctx: {
token: string | null;
}, req: NextRequest) => NextResponse | void;
/** Fallback for unhandled errors in middleware logic */
onError?: (err: any, req: NextRequest) => NextResponse;
}
/**
* Returns a Next.js Edge Middleware function enforcing TideCloak auth.
*
* Example usage in your `middleware.ts`:
*
* ```ts
* import tidecloakConfig from './tidecloakAdapter.json'
* import { createTideMiddleware } from 'tidecloak-nextjs/server/tidecloakMiddleware'
*
* export default createTideMiddleware({
* config: tidecloakConfig,
* protectedRoutes: {
* '/admin/*': ['admin'],
* '/api/private/*': ['user']
* }
* })
*
* export const config = {
* matcher: [
* '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico)).*)',
* '/(api|trpc)(.*)'
* ],
* runtime: 'edge'
* }
* ```
*/
export declare function createTideCloakMiddleware(opts: TideMiddlewareOptions): (req: NextRequest) => Promise<NextResponse<unknown>>;
export {};
//# sourceMappingURL=tidecloakMiddleware.d.ts.map