Uday Singh
12/04/2025, 9:00 AMasync function ServerDescopeComponent() {
const currentSession = await session();
export function middleware(request: NextRequest) {
// my app logic
}
// src/middleware.ts
import { authMiddleware } from '@descope/nextjs-sdk/server';
export default authMiddleware({
projectId: 'my_project_id',
publicRoutes: ['*'],
redirectUrl: '/sign-in',
});
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico',
],
};Rahul Rijhwani
12/04/2025, 9:25 AMpublicRoutes: ['*'] in your middleware configuration, which makes all routes public and prevents the middleware from setting the session.
The session() function works more efficiently when authMiddleware sets the session first. If the middleware doesn't set a session, session() attempts to retrieve and validate the token from cookies, but this may not work as expected when all routes are public.
To fix this:
1. Remove or adjust publicRoutes: ['*'] to only include routes that should be public:
typescript
export default authMiddleware({
projectId: 'my_project_id',
publicRoutes: ['/sign-in', '/about'],
redirectUrl: '/sign-in',
});
2. Alternatively, if you need custom logic, you can pass the projectId directly to `session()`:
typescript
const currentSession = await session({
projectId: 'my_project_id'
});
The middleware validates the session once and makes it available to your server components, which is more efficient than validating on every session() call.