nextauthjs / nextauthjs/next-auth
How to test a route handler wrapped by auth()
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28.4k
- Forks
- 4k
- PR merge metrics
- No merged PRs in 30d
Description
What is the improvement or update you wish to see?
The documentation doesn't explain how to test a route handler protected using the auth() handler as it is suggested.
Is there any context that might help us understand?
I've implemented a test site, see the repo, to prove it.
The /api/protected route (here) is using the auth() handler and as it's seen in the test (here), needs to somehow create a request that auth() could handle.
The usual test, see it here doesn't work. auth() is neither accepting a NextRequest nor responding a NextReponse.
The documentation should add an example of how it can be done.
Does the docs page already exist? Please link to it.
https://authjs.dev/getting-started/session-management/protecting#api-routes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the API routes section of the linked protecting documentation page, then compare the example repository's src/app/api/protected/route.ts and src/app/api/protected/route.test.ts with src/app/api/public/route.test.ts. Document a working way to construct a request for a route protected by auth(), with the completed example showing how the protected handler can be tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, typescript
- Domain
- authentication, documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100