Skip to content

How to access user / session data in nextjs app directory api routes #365

@NuoWenLei

Description

@NuoWenLei

Describe the bug
I can't seem to get withAuth() to work in API routes. I want to be able to access the user object and organizationId etc in protected API routes through withAuth() but when I add the api routes to the middleware configs, I seem to get 404 html errors when calling the api endpoints.

To Reproduce
Steps to reproduce the behavior:

  1. Middleware.ts set up authkitMiddleware() as usual
  2. Add /api/something to the middleware config
  3. In /api/something/route.ts, create a GET handler and call withAuth()
  4. See error

Expected behavior
When I add /api/something as a protected route, I should be able to call withAuth() or some other function in the route.ts to access the user, organizationId, etc in the same way as in a protected page.

Screenshots
After adding the route to middleware config
Image

Without adding the route to middleware config
Image

Versions:

  • OS [MacOS]
  • Browser [Chrome]
  • authkit-nextjs version [2.13.0]
  • Next.js version [16.0.1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions