Describe the bug
When I try to run await getSession(context) in getServerSideProps, it throws the following error:
TypeError: Only absolute URLs are supported
It comes from node-fetch library, which luckily I'm one of the core maintainers :smile: Next.js uses it to polyfill Fetch API on the server.
It looks like next-auth is trying to fetch data from 'api/auth/session, but no domain name is provided and therefore it won't work. I did set options.site in the next-auth configuration, so I don't really understand why this error occurs :confused:
To Reproduce
export const getServerSideProps: GetServerSideProps = async context => {
console.log(await getSession(context));
};
Expected behavior
I should be able to obtain the session object.
Screenshots or error logs
N/A
Additional context
As a workaround, I tried using fetch directly to fetch the session object:
const response = await fetch(`${process.env.SITE ?? 'http://localhost:3000'}/api/auth/session`);
console.log(await response.json());
Unfortunately, it returns an empty object - I guess it probably lacks some context.
Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
duplicate
Changing the Provider component props in _app.js solved the issue:
Before:
<Provider options={{site: process.env.SITE}} session={session}>
After:
<Provider options={{site: process.env.SITE ?? 'http://localhost:3000'}} session={session}>
For anyone seeing this (we have a had a few repots of it) the thing you probably to check is where / how the environment variable is configured.
The tl;dr is it goes in a .env file when running locally and not the env option in next.config.js which is for client side environment variables only.
In a future version of NextAuth.js it might just hard fail and error if it detects that the site name is not configured when a server side call is made.
I'd like to add that if you run your nextjs server with the now or vercel cli. you can prepend NEXT_PUBLIC_ on the var so it's accessible on the client-side. So it would be process.env.NEXT_PUBLIC_SITE