-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling getSession inside middleware always return { user: null } #115
Comments
Hi there, would you mind enabling debug mode in your middleware and posting the logs? |
@PaulAsjes late to the party, but I'm facing the same error - just doing the default custom middleware example from the readme with a console.log of the session to reproduce OPs example. With But |
Where are you calling |
@PaulAsjes as part of the |
If in the middleware, be sure to pass in a const response = await authkitMiddleware()(request, event);
const session = await getSession(response); |
Here's the full example which I made just combining the two examples in the README - then added the cookie parsing when // authkitMiddleware will handle refreshing the session if the access token has expired
const response = await authkitMiddleware({
debug: true,
redirectUri: `${process.env.FRONTEND_URL}/callback`,
middlewareAuth: {
enabled: true,
unauthenticatedPaths: [],
},
})(request, event);
// If session is undefined, the user is not authenticated
const session = await getSession(response);
console.log("sess", session);
const cookieStore = await cookies();
const authCookie = await cookieStore.get('wos-session');
if (!authCookie?.value) {
// If the cookie is not present, the user is not authenticated
// and we should return the workos Authkit response
return response;
}
const workos = new WorkOS(process.env.WORKOS_API_KEY ?? '');
const cookieSession = await workos.userManagement.getSessionFromCookie({
sessionData: authCookie.value,
cookiePassword: process.env.WORKOS_COOKIE_PASSWORD,
});
console.log("session from cookie", cookieSession);
return response;
|
I ran into a similar issue with the session user being null. In my case I think I was trying to access the session user on a path that was included in the I was also trying to setup the middleware so that FYI a short version of the app structure
and the middleware function looks like export default async function middleware(
request: NextRequest,
event: NextFetchEvent
) {
const url = request.nextUrl;
const response = await authkitMiddleware({
middlewareAuth: {
enabled: true,
unauthenticatedPaths: [
"/home",
],
},
})(request, event);
const session = await getSession(response as NextResponse);
if (!session?.user && url.pathname === "/") {
return NextResponse.rewrite(new URL("/home", request.url));
}
return response;
} |
Immediately after posting this I went back to the project and things stopped working. After a little more time and looking at the source code of authkit-nextjs, I found that the
|
Having exact same issue, following example in readme does not work. Debug logs will say there is a session, but when checking |
I was facing the same error, yet i noticed that not passing response on getSession made it work. I'm using
logs with response as parameter
quick update based on @overra comment
logs
|
Has anyone managed to find a workaround? How do you live without authorization for several months... |
in the comments you'll find the workaround. there's no need to live without auth.
instead of
|
Hello,
I'm using the following middleware:
The
console.log('sess')
always return{ user: null }
.On a client comportent app/(landing-page)/page.tsx, I put the following code to get the current user information:
This time, I observe the expected behaviour:
null
is the user is NOT logged inThe text was updated successfully, but these errors were encountered: