Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Added Private Route Array #69

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,25 @@ import { authMiddleware } from '@descope/nextjs-sdk/server'
export default authMiddleware({
// The Descope project ID to use for authentication
// Defaults to process.env.DESCOPE_PROJECT_ID
projectId: 'your-descope-project-id'
projectId: 'your-descope-project-id',

// The URL to redirect to if the user is not authenticated
// Defaults to process.env.SIGN_IN_ROUTE or '/sign-in' if not provided
// NOTE: In case it contains query parameters that exist in the original URL, they will override the original query parameters. e.g. if the original URL is /page?param1=1&param2=2 and the redirect URL is /sign-in?param1=3, the final redirect URL will be /sign-in?param1=3&param2=2
redirectUrl?: string
redirectUrl?: string,

// An array of public routes that do not require authentication
// All routes are private by default, this is where you will defined which routes are public.
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
// In addition to the default public routes:
// - process.env.SIGN_IN_ROUTE or /sign-in if not provided
// - process.env.SIGN_UP_ROUTE or /sign-up if not provided
publicRoutes?: string[]
publicRoutes?: string[],

// An array of private routes that require authentication
// If this is defined, then the default behavior of all routes being private will be changed, and all routes will be public by default. Private routes will therefore have to be controlled by this array
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
privateRoutes?: string[],

// If you having privateRoutes and publicRoutes defined at the same time, privateRoutes by default will be ignored.
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
})

export const config = {
Expand Down
44 changes: 36 additions & 8 deletions src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@
// Defaults to process.env.SIGN_IN_ROUTE or '/sign-in' if not provided
// NOTE: In case it contains query parameters that exist in the original URL, they will override the original query parameters. e.g. if the original URL is /page?param1=1&param2=2 and the redirect URL is /sign-in?param1=3, the final redirect URL will be /sign-in?param1=3&param2=2
redirectUrl?: string;

// An array of public routes that do not require authentication
// In addition to the default public routes:
// - process.env.SIGN_IN_ROUTE or /sign-in if not provided
// - process.env.SIGN_UP_ROUTE or /sign-up if not provided
publicRoutes?: string[];
};
} & (
| {
// An array of public routes that do not require authentication
// In addition to the default public routes:
// - process.env.SIGN_IN_ROUTE or /sign-in if not provided
// - process.env.SIGN_UP_ROUTE or /sign-up if not provided
publicRoutes?: string[];
privateRoutes?: never;
}
| {
publicRoutes?: never;
// An array of private routes that require authentication
// If privateRoutes is defined, routes not listed in this array will default to public routes
privateRoutes?: string[];
}
);

const getSessionJwt = (req: NextRequest): string | undefined => {
let jwt = req.headers?.get('Authorization')?.split(' ')[1];
Expand All @@ -46,7 +55,19 @@
);
const isPublic = options.publicRoutes?.includes(req.nextUrl.pathname);

return isDefaultPublicRoute || isPublic;
// If both publicRoutes and privateRoutes are provided, we prioritize publicRoutes
if (options.publicRoutes && options.privateRoutes) {
return isDefaultPublicRoute || isPublic;
}

// If only publicRoutes are provided
if (options.publicRoutes) {
return isDefaultPublicRoute || isPublic;
}

// If only privateRoutes are provided
const isPrivate = options.privateRoutes?.includes(req.nextUrl.pathname);
return isDefaultPublicRoute || !isPrivate;
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
};

const addSessionToHeadersIfExists = (
Expand All @@ -72,6 +93,13 @@
async (req: NextRequest) => {
console.debug('Auth middleware starts');

if (options.publicRoutes && options.privateRoutes) {
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
console.warn(
'Both publicRoutes and privateRoutes are defined. Ignoring privateRoutes.'
);
options.privateRoutes = undefined;

Check failure on line 100 in src/server/authMiddleware.ts

View workflow job for this annotation

GitHub Actions / 🪥 Lint

Assignment to property of function parameter 'options'
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
}

const jwt = getSessionJwt(req);

// check if the user is authenticated
Expand Down
Loading