UNPKG

@asgardeo/nextjs

Version:

Next.js implementation of Asgardeo JavaScript SDK.

101 lines (100 loc) 3.73 kB
/** * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { NextRequest, NextResponse } from 'next/server'; import { AsgardeoNextConfig } from '../../models/config'; import { SessionTokenPayload } from '../../utils/SessionManager'; export type AsgardeoMiddlewareOptions = Partial<AsgardeoNextConfig>; export type AsgardeoMiddlewareContext = { /** * Protect a route by redirecting unauthenticated users. * Redirect URL fallback order: * 1. options.redirect * 2. resolvedOptions.signInUrl * 3. resolvedOptions.defaultRedirect * 4. referer (if from same origin) * If none are available, throws an error. */ protectRoute: (options?: { redirect?: string; }) => Promise<NextResponse | void>; /** Check if the current request has a valid Asgardeo session */ isSignedIn: () => boolean; /** Get the session ID from the current request */ getSessionId: () => string | undefined; /** Get the session payload from JWT session if available */ getSession: () => Promise<SessionTokenPayload | undefined>; }; type AsgardeoMiddlewareHandler = (asgardeo: AsgardeoMiddlewareContext, req: NextRequest) => Promise<NextResponse | void> | NextResponse | void; /** * Asgardeo middleware that integrates authentication into your Next.js application. * Similar to Clerk's clerkMiddleware pattern. * * @param handler - Optional handler function to customize middleware behavior * @param options - Configuration options for the middleware * @returns Next.js middleware function * * @example * ```typescript * // middleware.ts - Basic usage * import { asgardeoMiddleware } from '@asgardeo/nextjs'; * * export default asgardeoMiddleware(); * ``` * * @example * ```typescript * // With route protection * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs'; * * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']); * * export default asgardeoMiddleware(async (asgardeo, req) => { * if (isProtectedRoute(req)) { * await asgardeo.protectRoute(); * } * }); * ``` * * @example * ```typescript * // Advanced usage with custom logic * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs'; * * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']); * const isAuthRoute = createRouteMatcher(['/sign-in', '/sign-up']); * * export default asgardeoMiddleware(async (asgardeo, req) => { * // Skip protection for auth routes * if (isAuthRoute(req)) return; * * // Protect specified routes * if (isProtectedRoute(req)) { * await asgardeo.protectRoute({ redirect: '/sign-in' }); * } * * // Check authentication status * if (asgardeo.isSignedIn()) { * console.log('User is authenticated with session:', asgardeo.getSessionId()); * } * }, { * defaultRedirect: '/sign-in' * }); * ``` */ declare const asgardeoMiddleware: (handler?: AsgardeoMiddlewareHandler, options?: AsgardeoMiddlewareOptions | ((req: NextRequest) => AsgardeoMiddlewareOptions)) => ((request: NextRequest) => Promise<NextResponse>); export default asgardeoMiddleware;