Hello, I'm want to utiilize nextjs SSR feature and...
# ask-a-descoper
u
Hello, I'm want to utiilize nextjs SSR feature and my currentSession is returning null but client side implementations works fine, i have also configured the middleware
Copy code
async function ServerDescopeComponent() {
    const currentSession = await session();
Copy code
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',
  ],
};
r
Hi Uday, Your issue is likely that you have
publicRoutes: ['*']
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:
Copy code
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()`:
Copy code
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.