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

Authenticating server-side with v5 docs #10229

Closed
telng opened this issue Mar 6, 2024 · 4 comments
Closed

Authenticating server-side with v5 docs #10229

telng opened this issue Mar 6, 2024 · 4 comments
Labels
docs Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@telng
Copy link

telng commented Mar 6, 2024

What is the improvement or update you wish to see?

I'm attempting to implement an API route following the guidance here: https://authjs.dev/guides/upgrade-to-v5#authenticating-server-side under the API Route tab.

The code I have is a little expanded from the sample there:

import { auth } from "../../../auth"
import { NextApiRequest, NextApiResponse } from "next"
import { NextResponse } from 'next/server';


export async function GET(req: NextApiRequest, res: NextApiResponse) {
	try {
		const session = await auth(req, res)
		if (session) {
			console.log(session.user)
			return NextResponse.json({status: "OK"})
		}
	} catch (error) {
		console.error(error)
	}
	console.log("No session")
	return NextResponse.json({status: "ERR"})
}

A couple of things:

  • session.user isn't being populated by the auth() call. session is set if the user's logged in and not set if they're not, but I'm at a loss as to how to retrieve information about the currently logged in user. A sentence on how to achieve this would be appreciated.
  • there's some weirdness around NextApiResponse - if I return res.json(..) or res.status(..) then I get an error: TypeError: res.status is not a function.

Is there any context that might help us understand?

Firstly, I'm not at all experienced at this, and I might have missed the obvious.

Everything's up-to-date (I think):
[email protected] /root/ax1/ax1
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

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

https://authjs.dev/guides/upgrade-to-v5#authenticating-server-side

@telng telng added docs Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Mar 6, 2024
@alannnc
Copy link

alannnc commented Mar 7, 2024

The same happens to me but I'm using app router api handler. Instead of await auth() I'm doing auth wrapper as suggested in docs.

@lfgtavora
Copy link

same! i was trying figure out whats wrong with no success :(

@ndom91
Copy link
Member

ndom91 commented Apr 12, 2024

We've released some new docs that explain this in detail - https://authjs.dev/getting-started/session-management. Hope that covers it for you 🙏

@ndom91 ndom91 closed this as completed Apr 12, 2024
@lfgtavora
Copy link

We've released some new docs that explain this in detail - https://authjs.dev/getting-started/session-management. Hope that covers it for you 🙏

hi! link is broken :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

4 participants