Next-auth: ReferenceError: window is not defined

Created on 14 Aug 2020  路  3Comments  路  Source: nextauthjs/next-auth

Your question

getSession() can be used on server-side, requires next-auth/client. Into client.js of next-auth window object is checked. However window only exists in the Browser, so I get the error, ReferenceError: window is not defined .... and this is absolutely right.

This response is obtained by the server at build time (in dev and prod):

error:

UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at /.../node_modules/next-auth/dist/client/index.js:279:69
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/.../node_modules/next-auth/dist/client/index.js:22:103)
    at _next (/.../node_modules/next-auth/dist/client/index.js:24:194)
    at /.../node_modules/next-auth/dist/client/index.js:24:364
    at new Promise (<anonymous>)
    at /.../node_modules/next-auth/dist/client/index.js:24:97
    at signIn (/.../node_modules/next-auth/dist/client/index.js:304:18) ...

What are you trying to do
I want to be able to get the session of a user from the server side, with the aim of returning data from a database.

Feedback
*Documentation getSession() Example:

import { getSession } from 'next-auth/client'

export default async (req, res) => {
  const session = await getSession({ req })
  if (session) {
    // Signed in
    console.log('Session', JSON.stringify(session, null, 2))
  } else {
    // Not Signed in
    res.status(401)
  }
  res.end()
}
  • [*] Found the documentation helpful
  • [*] Found documentation but was incomplete

Quite possibly the context I want to apply is not correct, I don't know, however using the code according to the example is the error that is obtained.

Any guide on how to correct this?

help wanted question

Most helpful comment

Hi Iain, this examples work perfect! no problem.

I was looking to perform a page redirect, using SignIn() without user action:

if (!session) {
     signIn();
     return null;
   }

This was calling window from the Server ... my mistake, sorry!.

This is more correct:

...
if (!session) {return <AccessDenied />}
...

And into AccessDenied click action to SignIn():

...
<button onClick={() => signIn()}>Sign In</button>
...

Regards,

All 3 comments

Hi there! I'm not sure how you are getting this error, we'd need to have a repo to look at to see what's going on.

The example repo has examples for using getSession in an API route and on a server side rendered page:
https://github.com/nextauthjs/next-auth-example/blob/main/pages/api/examples/session.js
https://github.com/nextauthjs/next-auth-example/blob/main/pages/protected-ssr.js

If signed in to the example site, you can confirm they both work:
https://next-auth-example.now.sh/api/examples/session
https://next-auth-example.now.sh/protected-ssr

I'm not sure what would cause the error you are seeing, perhaps something about the build process.

Hi Iain, this examples work perfect! no problem.

I was looking to perform a page redirect, using SignIn() without user action:

if (!session) {
     signIn();
     return null;
   }

This was calling window from the Server ... my mistake, sorry!.

This is more correct:

...
if (!session) {return <AccessDenied />}
...

And into AccessDenied click action to SignIn():

...
<button onClick={() => signIn()}>Sign In</button>
...

Regards,

@alephart Thank you for the followup!

There are quite a few situations where this sort of thing trips folks up - it's a tricky area with a library designed to support client and server side rendering with both browser and server based imports - and I really appreciate the detail as it helps us design behaviour that can help reduce confusing situations like this (e.g. via console warnings or additional conditional checks).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iaincollins picture iaincollins  路  3Comments

SharadKumar picture SharadKumar  路  3Comments

MelMacaluso picture MelMacaluso  路  3Comments

jimmiejackson414 picture jimmiejackson414  路  3Comments

bscaspar picture bscaspar  路  3Comments