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
26 changes: 18 additions & 8 deletions src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ type MiddlewareOptions = {
// 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 @@ -45,8 +54,9 @@ const isPublicRoute = (req: NextRequest, options: MiddlewareOptions) => {
req.nextUrl.pathname
);
const isPublic = options.publicRoutes?.includes(req.nextUrl.pathname);
const isPrivate = options.privateRoutes?.includes(req.nextUrl.pathname);

return isDefaultPublicRoute || isPublic;
return isDefaultPublicRoute || isPublic || !isPrivate;
gaokevin1 marked this conversation as resolved.
Show resolved Hide resolved
};

const addSessionToHeadersIfExists = (
Expand Down
Loading