Skip to content
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

authkit middleware not working on Next 14.2 #170

Closed
chamini2 opened this issue Jan 13, 2025 · 5 comments · Fixed by #171
Closed

authkit middleware not working on Next 14.2 #170

chamini2 opened this issue Jan 13, 2025 · 5 comments · Fixed by #171

Comments

@chamini2
Copy link

chamini2 commented Jan 13, 2025

image

my middleware.ts is similar to

import { authkit } from "@workos-inc/authkit-nextjs";
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import {
  CSRF_COOKIE_NAME
} from "./lib/csrf";

async function csrfMiddleware(request: NextRequest, response: NextResponse) {
  const signature = "...";
  response.cookies.set(CSRF_COOKIE_NAME, signature, {
    sameSite: "strict",
    httpOnly: true,
  });
  response.headers.set("X-Frame-Options", "SAMEORIGIN");
}

export async function middleware(request: NextRequest, event: NextFetchEvent) {
  // This method will automatically handle setting the cookie and refreshing the session
  const auth = await authkit(request, {
    debug: false,
  });

  // Headers need to be included in every non-redirect response to ensure that `withAuth` works as expected
  const response = NextResponse.next({
    headers: auth.headers,
  });

  await csrfMiddleware(request, response);

  return response;
}

export const config = {
  matcher: [
    "/",
    "/demos",
    "/demos/:path*",
  ],
};
@fierysolid
Copy link

Same issue

@chamini2
Copy link
Author

seeing this with debug

Session invalid. Refreshing access token that ends in qrng4KCikQ
Failed to refresh. Deleting cookie. [Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options]

@chamini2
Copy link
Author

only happens when refreshin

@chamini2
Copy link
Author

this is happening for the non composable solution too for me

export default authkitMiddleware({ debug: true });

@PaulAsjes
Copy link
Contributor

Hi all, I was able to recreate this. Will put up a fix later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants