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

Next.js route handler auth return type conflicts #11728

Open
SongDerrick opened this issue Aug 30, 2024 · 5 comments
Open

Next.js route handler auth return type conflicts #11728

SongDerrick opened this issue Aug 30, 2024 · 5 comments
Labels
bug Something isn't working help-needed The maintainer needs help due to time constraint/missing knowledge TypeScript Issues relating to TypeScript

Comments

@SongDerrick
Copy link

What is the improvement or update you wish to see?

import { auth } from "auth"

export const GET = auth((req) => {
  if (req.auth) {
    return Response.json({ data: "Protected data" })
  }

  return Response.json({ message: "Not authenticated" }, { status: 401 })
}) as any // TODO: Fix `auth()` return type

This code is from next-auth-example template.

without explicit type casting as any, type error occurs

such as

Type error: Route "app/api/protected/route.ts" has an invalid export:
  "unknown" is not a valid POST return type:
    Expected "void | Response | Promise<void | Response>", got "unknown".

For my case, I handled with explicit type casting by createing new type and interface to deal with it.

app/api/sample/route.ts

interface AuthRequest extends Request {
  auth?: any
}

type NextAuthAPIRouteHandler = (req: Request) => Promise<
  NextResponse<
    | {
        error: string
      }
    | {
        ok: boolean
      }
  >
>


export const POST = auth(async function POST(request: AuthRequest) {
  if (!request.auth) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }
  try {
    await businessLogic()
    return NextResponse.json({ ok: true }, { status: 200 })
  } catch (error) {
    return NextResponse.json(
      { ok: false, error: 'Sending Data Failed' },
      { status: 500 }
    )
  }
}) as NextAuthAPIRouteHandler

Therefore, there must be docs fixed since there is no explanations about explicit type casting.
Build Error always occurs without it, so i think it needs to be fixed.
Thanks.

Is there any context that might help us understand?

Reproduction environment

  System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M2
    Memory: 85.50 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.0.0 
    npm: 10.5.1 
    pnpm: 9.6.0 
  Browsers:
    Chrome: 128.0.6613.113
    Safari: 17.6

Does the docs page already exist? Please link to it.

https://authjs.dev/getting-started/session-management/protecting

@SongDerrick SongDerrick added docs Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Aug 30, 2024
@SongDerrick
Copy link
Author

Created PR regarding this issues #11729

@balazsorban44 balazsorban44 added bug Something isn't working TypeScript Issues relating to TypeScript help-needed The maintainer needs help due to time constraint/missing knowledge and removed docs Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Sep 4, 2024
Copy link

github-actions bot commented Sep 4, 2024

This issue was marked with the help needed label by a maintainer.

The issue might require some digging, so it is recommended to have some experience with the project.

Have a look at the Contributing Guide first.

This will help you set up your development environment to get started. When you are ready, open a PR, and link back to this issue in the form of adding Fixes #1234 to the PR description, where 1234 is the issue number. This will auto-close the issue when the PR gets merged, making it easier for us to keep track of what has been fixed.

Please make sure that - if applicable - you add tests for the changes you make.

If you have any questions, feel free to ask in the comments below or the PR. Generally, you don't need to @mention anyone directly, as we will get notified anyway and will respond as soon as we can)

Note

There is no need to ask for permission "can I work on this?" Please, go ahead if there is no linked PR 🙂

@fprl
Copy link

fprl commented Oct 21, 2024

Any update on this one? I see that the PR got closed

@SongDerrick
Copy link
Author

i havent got any response

@krizpoon
Copy link

krizpoon commented Nov 11, 2024

Have the same issue with version 5.0.0-beta.25 + NextJS 15.0.1. Need to cast handlers as any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help-needed The maintainer needs help due to time constraint/missing knowledge TypeScript Issues relating to TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants