Using 2.0.0-beta.77. I'm trying to get the current session in a server api request, however the example in the docs doesn't seem to work: https://next-auth.js.org/getting-started/client#using-getsession-in-api-routes
Assuming the import is
import { session as getSession } from 'next-auth/client'
since there is no getSession in the example, the server returns
[next-auth][error][CLIENT_FETCH_ERROR] [
'http://localhost:3000/api/auth/session',
ReferenceError: fetch is not defined
Which makes sense since fetch isn't implemented in node.
Example code:
import { session } from 'next-auth/client'
export default async (req, res) => {
const s = await session({ req });
console.log(s.accessToken.length);
}
I've seen #202 and #206 however it seems to point towards the same getSession call. I feel like there must be something obvious going over my head with this.
Ah, if you are getting an error about fetch not being defined, you probably need upgrade your Next.js version to version 9.4 or newer (which includes fetch).
We rely on the built-in version so we don't have to bundle a fetch polyfill.
Note: The session and getSession client methods are the same.
Note that I had some problems with server-side getSession too, not related to fetch. It seemed like babel miscompiled it - admittedly I was running on the typescript fork (https://github.com/iaincollins/next-auth/pull/220).
To fix it, I had to change getSession to this:
const getSession = (arg) => {
const { req } = arg || {}
const baseUrl = _baseUrl({ req })
const options = req ? { headers: { cookie: req.headers.cookie } } : {}
return _fetchData(`${baseUrl}/session`, options)
}
from
const getSession = async ({ req } = {}) => {
const baseUrl = _baseUrl({ req })
const options = req ? { headers: { cookie: req.headers.cookie } } : {}
return _fetchData(`${baseUrl}/session`, options)
}
In babel-compiled code, the default parameter value was interfering badly with the async function. Note that also the async keyword is superfluous in the above definition (as _fetchData returns a promise already). That btw was made obvious to me when I tried to make a type definition for getSession!
I would be interested to know if other people had the same issue. Not easy for me to test from master because I need the typescript type definitions.
Note that I had some problems with server-side getSession too, not related to fetch. It seemed like babel miscompiled it - admittedly I was running on the typescript fork (#220).
That's a separate issue specific to that fork and TypeScript (which isn't merged in); please create separate issues for those.
The getSession() function works fine in JavaScript, as you can see from the live demo site:
https://github.com/iaincollins/next-auth-example/blob/main/pages/example-page-3.js
At a glance, the method responses may not be technically correct _fetchData() SHOULD be explicitly returning a Promise and that might be causing your problem.
Marking this as closed as the example project already shows how to use this function.
@leftyfl1p @arnodel
Update: Thanks again for the feedback, meant to come back and update this issue.
The issue with the incorrect types on the response from client methods should be fixed as of 2.1.0 which went out yesterday
Note: There has been an update in 2.2 as a result of a bug report a fix that is only specifically for accessing getSession() from an API routes was required (see below).
This resolves an intermittent bug that can could cause this error (depending on the auth flow), but we are looking at how we can provide a more elegant approach.
You can use a currently undocumented feature called setOptions() in API routes to configure the site name so you can use getSession() in an API route. This is not yet a supported feature and it's possible it may be changed or removed in future but it exists specifically to address this problem as an interim solution.
Note: You should not use this approach in client side pages, you should configure pages/_app.js instead.
import { setOptions, getSession } from 'next-auth/client'
setOptions({ site: process.env.SITE })
export default async (req, res) => {
const session = await getSession({ req })
console.log('session', session)
res.end()
}
Most helpful comment
Ah, if you are getting an error about fetch not being defined, you probably need upgrade your Next.js version to version 9.4 or newer (which includes fetch).
We rely on the built-in version so we don't have to bundle a fetch polyfill.
Note: The
sessionandgetSessionclient methods are the same.