Wrapping auth() for Protected Routes and Encapsulating Logic #13000
-
Hey Auth.js folks! I'm doing a quick POC to understand if
In our current implementation we do something like this: export async function authJsRoute(routeHandler: RouteHandler): Promise<RouteHandler> {
return auth(async (req) => {
if (!req.auth) return NextResponse.json({ message: 'Not authenticated' }, { status: 401 });
// handle logging and other stuff here
return routeHandler(req);
});
}
// and usage
export const GET = authJsRoute(async (request: NextAuthRequest) => {
logger.info('AuthRequiredRoute', {
auth: request.auth,
});
return NextResponse.json(
{ message: `Hello, ${request.auth?.user?.name || 'Guest'}!` },
{ status: 200 }
);
}); For Can anyone explain how I should handle this? I tried using Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok classic brain fart here. I need to await the export const GET = await authJsRoute(async (request: NextAuthRequest) => {
logger.info('AuthRequiredRoute', {
auth: request.auth,
});
return NextResponse.json(
{ message: `Hello, ${request.auth?.user?.name || 'Guest'}!` },
{ status: 200 }
);
}); |
Beta Was this translation helpful? Give feedback.
Ok classic brain fart here. I need to await the
authJsRoute()
call: