next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

[Bug]: NextAuth basic middleware causes 500 internal error on all page .json file requests

Open Joroze opened this issue 2 years ago • 7 comments

Summary

I'm deploying a basic NextJS project using NextAuth basic middleware as documented here: https://next-auth.js.org/configuration/nextjs#basic-usage

When loading any page route, I get the page's .json internal server errors as a response multiple times.

  • I added function logs of the errors I'm seeing below.
  • Side note: I'm deploying from a NX mono repo - but I doubt this is why the issue occurs.
Screen Shot 2022-10-12 at 11 06 55 AM

Steps to reproduce

https://next-auth.js.org/configuration/nextjs#basic-usage middleware.tsx

import { withAuth } from 'next-auth/middleware';

export default withAuth({
  pages: {
    signIn: '/auth/signin',
  },
});

Relevant dependency versions:

    "next": "12.3.1",
    "next-auth": "4.12.3",
    "@netlify/plugin-nextjs": "4.25.0",
  1. Use NextAuth middleware (latest version of NextAuth)
  2. Deploy Netlify site
  3. Load a page and you'll see the .json files are returning 500 internal errors

A link to a reproduction repository

No response

Next Runtime version

4.25.0

More information about your build

  • [ ] I am building using the CLI
  • [x] I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
[build.environment]
  NPM_FLAGS = "--legacy-peer-deps"
[[plugins]]
package = "@netlify/plugin-nextjs"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
//@ts-check

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { withNx } = require('@nrwl/next/plugins/with-nx');

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  reactStrictMode: true,
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
};

module.exports = withNx(nextConfig);

Builds logs (or link to your logs)

Build logs
10:51:44 AM: Build ready to start
10:51:46 AM: build-image version: 9289b698a503f132b265bd089ab40ce1d6f9e65e (focal)
10:51:46 AM: build-image tag: v4.12.0
10:51:46 AM: buildbot version: c87074660cd9a4568c0ac759bc083987e94204b3
10:51:46 AM: Fetching cached dependencies
10:51:46 AM: Starting to download cache of 684.8MB
10:51:50 AM: Finished downloading cache in 4.661007194s
10:51:50 AM: Starting to extract cache
10:51:58 AM: Finished extracting cache in 7.262633705s
10:51:58 AM: Finished fetching cache in 12.04428989s
10:51:58 AM: Starting to prepare the repo for build
10:51:58 AM: Preparing Git Reference refs/heads/main
10:51:59 AM: Parsing package.json dependencies
10:52:00 AM: Starting build script
10:52:00 AM: Installing dependencies
10:52:00 AM: Python version set to 2.7
10:52:00 AM: Started restoring cached node version
10:52:01 AM: Finished restoring cached node version
10:52:01 AM: Downloading and installing node v16.18.0...
10:52:01 AM: Downloading https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.xz...
10:52:02 AM: Computing checksum with sha256sum
10:52:02 AM: Checksums matched!
10:52:04 AM: Now using node v16.18.0 (npm v8.19.2)
10:52:04 AM: Started restoring cached build plugins
10:52:04 AM: Finished restoring cached build plugins
10:52:04 AM: Attempting ruby version 2.7.2, read from environment
10:52:05 AM: Using ruby version 2.7.2
10:52:05 AM: Using PHP version 8.0
10:52:05 AM: No npm workspaces detected
10:52:05 AM: Started restoring cached node modules
10:52:05 AM: Finished restoring cached node modules
10:52:06 AM: Installing NPM modules using NPM version 8.19.2
10:52:06 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:06 AM: npm WARN config location in the cache, and they are managed by
10:52:06 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:06 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:06 AM: npm WARN config location in the cache, and they are managed by
10:52:06 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:16 AM: up to date, audited 2319 packages in 10s
10:52:16 AM: 239 packages are looking for funding
10:52:16 AM:   run `npm fund` for details
10:52:17 AM: 23 vulnerabilities (3 moderate, 18 high, 2 critical)
10:52:17 AM: To address issues that do not require attention, run:
10:52:17 AM:   npm audit fix
10:52:17 AM: To address all issues (including breaking changes), run:
10:52:17 AM:   npm audit fix --force
10:52:17 AM: Run `npm audit` for details.
10:52:17 AM: NPM modules installed
10:52:17 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:17 AM: npm WARN config location in the cache, and they are managed by
10:52:17 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:17 AM: Started restoring cached go cache
10:52:17 AM: Finished restoring cached go cache
10:52:17 AM: Installing Go version 1.17 (requested 1.17)
10:52:21 AM: unset GOOS;
10:52:21 AM: unset GOARCH;
10:52:21 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
10:52:21 AM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
10:52:21 AM: go version >&2;
10:52:21 AM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
10:52:21 AM: go version go1.17 linux/amd64
10:52:21 AM: Installing missing commands
10:52:21 AM: Verify run directory
10:52:23 AM: ​
10:52:23 AM: ────────────────────────────────────────────────────────────────
10:52:23 AM:   Netlify Build                                                 
10:52:23 AM: ────────────────────────────────────────────────────────────────
10:52:23 AM: ​
10:52:23 AM: ❯ Version
10:52:23 AM:   @netlify/build 27.20.1
10:52:23 AM: ​
10:52:23 AM: ❯ Flags
10:52:23 AM:   baseRelDir: true
10:52:23 AM:   buildId: 6346d480cda4aa0008891767
10:52:23 AM:   deployId: 6346d480cda4aa0008891769
10:52:23 AM: ​
10:52:23 AM: ❯ Current directory
10:52:23 AM:   /opt/build/repo
10:52:23 AM: ​
10:52:23 AM: ❯ Config file
10:52:23 AM:   /opt/build/repo/netlify.toml
10:52:23 AM: ​
10:52:23 AM: ❯ Context
10:52:23 AM:   production
10:52:23 AM: ​
10:52:23 AM: ❯ Using Next.js Runtime - v4.25.0
10:52:24 AM: ​
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM: ​
10:52:24 AM: Next.js cache restored.
10:52:24 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
10:52:24 AM: ​
10:52:24 AM: (@netlify/plugin-nextjs onPreBuild completed in 201ms)
10:52:24 AM: ​
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM:   2. Build command from Netlify app                             
10:52:24 AM: ────────────────────────────────────────────────────────────────
10:52:24 AM: ​
10:52:24 AM: $ nx run admin:build
10:52:24 AM:  >  NX   Running target build for project admin and 1 task(s) it depends on
10:52:24 AM: > nx run admin:codegen
10:52:25 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
10:52:25 AM: npm WARN config location in the cache, and they are managed by
10:52:25 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
10:52:25 AM: > [email protected] graphql:codegen
10:52:25 AM: > graphql-codegen --config libs/api/src/lib/graphql/codegen.yml && barrelsby --delete --singleQuotes --directory libs/api/src/lib/graphql --exclude 'test.ts|types.generated.ts'
10:52:26 AM: [STARTED] Parse Configuration
10:52:26 AM: [SUCCESS] Parse Configuration
10:52:26 AM: [STARTED] Generate outputs
10:52:26 AM: [STARTED] Generate to libs/api/src/lib/graphql/types.generated.ts
10:52:26 AM: [STARTED] Load GraphQL schemas
10:52:26 AM: [SUCCESS] Load GraphQL schemas
10:52:26 AM: [STARTED] Load GraphQL documents
10:52:26 AM: [SUCCESS] Load GraphQL documents
10:52:26 AM: [STARTED] Generate
10:52:27 AM: [SUCCESS] Generate
10:52:27 AM: [SUCCESS] Generate to libs/api/src/lib/graphql/types.generated.ts
10:52:27 AM: [STARTED] Generate to libs/api/src/lib/graphql/
10:52:27 AM: [STARTED] Load GraphQL schemas
10:52:27 AM: [SUCCESS] Load GraphQL schemas
10:52:27 AM: [STARTED] Load GraphQL documents
10:52:27 AM: [SUCCESS] Load GraphQL documents
10:52:27 AM: [STARTED] Generate
10:52:27 AM: [SUCCESS] Generate
10:52:27 AM: [SUCCESS] Generate to libs/api/src/lib/graphql/
10:52:27 AM: [SUCCESS] Generate outputs
10:52:27 AM: > nx run admin:build:production
10:52:29 AM: info  - Skipping linting
10:52:29 AM: info  - Checking validity of types...
10:52:35 AM: info  - Creating an optimized production build...
10:52:47 AM: info  - Compiled successfully
10:52:47 AM: info  - Collecting page data...
10:52:49 AM: info  - Generating static pages (0/5)
10:52:50 AM: info  - Generating static pages (1/5)
10:52:50 AM: info  - Generating static pages (2/5)
10:52:50 AM: info  - Generating static pages (3/5)
10:52:50 AM: info  - Generating static pages (5/5)
10:52:51 AM: info  - Finalizing page optimization...
10:52:51 AM: Route (pages)                              Size     First Load JS
10:52:51 AM: ┌ ○ / (856 ms)                             5.1 kB          170 kB
10:52:51 AM: ├   /_app                                  0 B             164 kB
10:52:51 AM: ├ ○ /404                                   193 B           165 kB
10:52:51 AM: ├ λ /api/auth/[...nextauth]                0 B             164 kB
10:52:51 AM: ├ ○ /auth/signin (937 ms)                  14 kB           182 kB
10:52:51 AM: └ ○ /oms/patients (822 ms)                 34.8 kB         203 kB
10:52:51 AM: + First Load JS shared by all              164 kB
10:52:51 AM:   ├ chunks/framework-c02f198d58d34563.js   45.3 kB
10:52:51 AM:   ├ chunks/main-7f51edf094ce88c6.js        30.9 kB
10:52:51 AM:   ├ chunks/pages/_app-d4dc28059f74008a.js  87.2 kB
10:52:51 AM:   └ chunks/webpack-df4cf1c8d23aa877.js     1.03 kB
10:52:51 AM: ƒ Middleware                               53.6 kB
10:52:51 AM: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
10:52:51 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
10:52:51 AM:  >  NX   Successfully ran target build for project admin
10:52:51 AM: ​
10:52:51 AM: (build.command completed in 26.9s)
10:52:51 AM: ​
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM: ​
10:52:51 AM: NextAuth package detected, NEXTAUTH_URL environment variable set by user in Netlify configuration to https://th-admin.netlify.app/api/auth
10:52:51 AM: Patching /opt/build/repo/node_modules/next/dist/server/base-server.js
10:52:51 AM: Done
10:52:51 AM: Patching /opt/build/repo/node_modules/next/dist/server/next-server.js
10:52:51 AM: Done
10:52:51 AM: Moving static page files to serve from CDN...
10:52:51 AM: Moved 3 files
10:52:51 AM: Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.
10:52:51 AM: ✨ Deploying middleware and functions to Netlify Edge Functions ✨
10:52:51 AM: This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
10:52:51 AM: Netlify configuration property "redirects" value changed to [
10:52:51 AM:   { from: '/_next/static/*', to: '/static/:splat', status: 200 },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/image*',
10:52:51 AM:     query: { url: ':url', w: ':width', q: ':quality' },
10:52:51 AM:     to: '/_ipx/w_:width,q_:quality/:url',
10:52:51 AM:     status: 301
10:52:51 AM:   },
10:52:51 AM:   { from: '/_ipx/*', to: '/.netlify/builders/_ipx', status: 200 },
10:52:51 AM:   { from: '/cache/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/server/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/serverless/*', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/trace', to: '/404.html', status: 404, force: true },
10:52:51 AM:   { from: '/traces', to: '/404.html', status: 404, force: true },
10:52:51 AM:   {
10:52:51 AM:     from: '/routes-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/build-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/prerender-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/react-loadable-manifest.json',
10:52:51 AM:     to: '/404.html',
10:52:51 AM:     status: 404,
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   { from: '/BUILD_ID', to: '/404.html', status: 404, force: true },
10:52:51 AM:   {
10:52:51 AM:     from: '/api',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/api/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     conditions: { Cookie: [Array] },
10:52:51 AM:     force: true
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/auth/signin.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/auth/signin',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/_next/data/yF0HNNoJhSpsl_DFYgQLO/oms/patients.json',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/oms/patients',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200,
10:52:51 AM:     force: false
10:52:51 AM:   },
10:52:51 AM:   {
10:52:51 AM:     from: '/*',
10:52:51 AM:     to: '/.netlify/functions/___netlify-handler',
10:52:51 AM:     status: 200
10:52:51 AM:   }
10:52:51 AM: ].
10:52:51 AM: ​
10:52:51 AM: (@netlify/plugin-nextjs onBuild completed in 248ms)
10:52:51 AM: ​
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM:   4. Functions bundling                                         
10:52:51 AM: ────────────────────────────────────────────────────────────────
10:52:51 AM: ​
10:52:51 AM: Packaging Functions from .netlify/functions-internal directory:
10:52:51 AM:  - ___netlify-handler/___netlify-handler.js
10:52:51 AM:  - ___netlify-odb-handler/___netlify-odb-handler.js
10:52:51 AM:  - _ipx/_ipx.js
10:52:51 AM: ​
10:53:09 AM: ​
10:53:09 AM: (Functions bundling completed in 17.8s)
10:53:09 AM: ​
10:53:09 AM: ────────────────────────────────────────────────────────────────
10:53:09 AM:   5. Edge Functions bundling                                    
10:53:09 AM: ────────────────────────────────────────────────────────────────
10:53:09 AM: ​
10:53:09 AM: Packaging Edge Functions from .netlify/edge-functions directory:
10:53:09 AM:  - ipx
10:53:09 AM:  - next_middleware
10:53:10 AM: ​
10:53:10 AM: (Edge Functions bundling completed in 1.3s)
10:53:10 AM: ​
10:53:10 AM: ────────────────────────────────────────────────────────────────
10:53:10 AM:   6. @netlify/plugin-nextjs (onPostBuild event)                 
10:53:10 AM: ────────────────────────────────────────────────────────────────
10:53:10 AM: ​
10:53:11 AM: Next.js cache saved.
10:53:11 AM: ​
10:53:11 AM: (@netlify/plugin-nextjs onPostBuild completed in 378ms)
10:53:11 AM: ​
10:53:11 AM: ────────────────────────────────────────────────────────────────
10:53:11 AM:   7. Deploy site                                                
10:53:11 AM: ────────────────────────────────────────────────────────────────
10:53:11 AM: ​
10:53:11 AM: Starting to deploy site from 'dist/apps/admin/.next'
10:53:23 AM: Creating deploy tree 
10:53:23 AM: Creating deploy upload records
10:53:23 AM: 23 new files to upload
10:53:23 AM: 2 new functions to upload
10:53:30 AM: Site deploy was successfully initiated
10:53:30 AM: ​
10:53:30 AM: (Deploy site completed in 19.4s)
10:53:30 AM: ​
10:53:30 AM: ────────────────────────────────────────────────────────────────
10:53:30 AM:   Netlify Build Complete                                        
10:53:30 AM: ────────────────────────────────────────────────────────────────
10:53:30 AM: ​
10:53:30 AM: (Netlify Build completed in 1m 7.5s)
10:53:30 AM: Caching artifacts
10:53:30 AM: Started saving node modules
10:53:30 AM: Finished saving node modules
10:53:30 AM: Started saving build plugins
10:53:30 AM: Finished saving build plugins
10:53:30 AM: Started saving pip cache
10:53:30 AM: Finished saving pip cache
10:53:30 AM: Started saving emacs cask dependencies
10:53:30 AM: Finished saving emacs cask dependencies
10:53:30 AM: Started saving maven dependencies
10:53:30 AM: Finished saving maven dependencies
10:53:30 AM: Started saving boot dependencies
10:53:30 AM: Finished saving boot dependencies
10:53:30 AM: Started saving rust rustup cache
10:53:30 AM: Finished saving rust rustup cache
10:53:30 AM: Started saving go dependencies
10:53:30 AM: Finished saving go dependencies
10:53:31 AM: Starting post processing
10:53:31 AM: Post processing - HTML
10:53:31 AM: Post processing - header rules
10:53:32 AM: Build script success
10:53:32 AM: Post processing - redirect rules
10:53:32 AM: Post processing done
10:53:37 AM: Site is live ✨
10:54:42 AM: Uploading Cache of size 747.9MB
10:54:47 AM: Finished processing build request in 3m1.40115615s

Function logs

Function logs
Oct 12, 10:58:46 AM: f810df9e ERROR  Invariant: failed to load static page {
  "page": "/",
  "url": "/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json",
  "initUrl": "http://6346d480cda4aa0008891769--th-admin.netlify.app:80/_next/data/yF0HNNoJhSpsl_DFYgQLO/index.json"
}Oct 12, 10:58:46 AM: f810df9e ERROR  MissingStaticPage [Error]: Failed to load static file for page: / Failed to download https://6346d480cda4aa0008891769--th-admin.netlify.app/index.html: 307 Temporary Redirect
    at /var/task/node_modules/next/dist/server/require.js:55:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.loadComponents (/var/task/node_modules/next/dist/server/load-components.js:61:26)
    at async NextNodeServer.findPageComponents (/var/task/node_modules/next/dist/server/next-server.js:555:36)
    at async NextNodeServer.renderPageComponent (/var/task/node_modules/next/dist/server/base-server.js:891:24)
    at async NextNodeServer.renderToResponse (/var/task/node_modules/next/dist/server/base-server.js:920:32)
    at async NextNodeServer.pipe (/var/task/node_modules/next/dist/server/base-server.js:369:25)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:743:21)
    at async Router.checkFsRoutes (/var/task/node_modules/next/dist/server/router.js:169:35)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:265:40)

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Joroze avatar Oct 12 '22 15:10 Joroze

Hi @Joroze ,

Thanks for submitting an issue. Unfortunately I haven't been able to replicate the behaviour that you've mentioned (all of my tests thus far have shown the .json files being returned as 200s).

Could you potentially provide us with a minimal reproduction?

ericapisani avatar Oct 12 '22 16:10 ericapisani

Hi @Joroze ,

Thanks for submitting an issue. Unfortunately I haven't been able to replicate the behaviour that you've mentioned (all of my tests thus far have shown the .json files being returned as 200s).

Could you potentially provide us with a minimal reproduction?

@ericapisani I believe it's under pages (routes) protected by the NextAuth middleware.

Try loading the /admin page in the new deploy preview.

Joroze avatar Oct 12 '22 16:10 Joroze

@ericapisani The easiest way to replicate is by using my provided middleware example from their official documentation for protecting all routes (which I need in my use case)

https://next-auth.js.org/configuration/nextjs#basic-usage

Joroze avatar Oct 12 '22 16:10 Joroze

@ericapisani I replicated a more simple demo of the issue occurring here: https://github.com/Joroze/next-netlify-starter-nextauth/pull/1

Joroze avatar Oct 13 '22 03:10 Joroze

@Joroze Amazing, thank you! And good call out on the /admin page in the deploy preview - wasn't on my 'A' game yesterday and forgot to test that route 😅

I'll move this into my team's backlog so someone can start taking a look at this when they're available.

ericapisani avatar Oct 13 '22 13:10 ericapisani

Thanks @ericapisani .

I believe the /me page wouldn't work either. Any page that the Auth middleware is covering basically.

Joroze avatar Oct 13 '22 14:10 Joroze

I wanted to add that in addition to the issues already reported, the middleware config matcher breaks the NextAuth middleware functionality completely.

I'm following examples taken from this topic for having the NextAuth middleware skip my custom Typography public assets: https://github.com/vercel/next.js/discussions/36308#discussioncomment-3758041

Joroze avatar Oct 15 '22 05:10 Joroze

Internal: Ticket ID: #113335.

hrishikesh-k avatar Oct 15 '22 17:10 hrishikesh-k

Hi @Joroze ,

Thanks for your patience. I've taken a look at this today, and just to rule this out as a possible source of your 500 errors - have you set the NEXTAUTH_URL environment variable for your site?

The reason I'm asking is because not setting it results in a 500 error response (the next-auth library throws an error when this happens), and we didn't have it set on our deploy preview demo site which is why the 500 errors were happening there when visiting /admin and /me pages there.

ericapisani avatar Oct 24 '22 21:10 ericapisani

Hi @ericapisani

I'm left confused because this release (https://github.com/netlify/netlify-plugin-nextjs/pull/1309) mentions NextAuth support out of the box with only having to set the Auth Secret env var (which I did)

Here are the logs of the deploy preview build (this PR: https://github.com/Joroze/next-netlify-starter-nextauth/pull/1)

5:12:12 PM:   4. @netlify/plugin-nextjs (onBuild event)                     
5:12:12 PM: ────────────────────────────────────────────────────────────────
5:12:12 PM: ​
5:12:12 PM: NextAuth package detected, setting NEXTAUTH_URL environment variable to https://deploy-preview-1--resplendent-frangollo-dfdb69.netlify.app
5:12:12 PM: Patching

Joroze avatar Oct 25 '22 01:10 Joroze

Hi @Joroze,

I'm so sorry, I wrote NEXTAUTH_URL but meant NEXTAUTH_SECRET (I wrote the wrong thing but linked to the correct place).

Thanks for confirming that you set the secret though, I'll take a look at the deploy preview logs for the site that your provided to see what I can find

ericapisani avatar Oct 25 '22 12:10 ericapisani

@Joroze I've merged a fix which should address your issue as part of version 4.28.2. Can you try installing this release as a dev dependency in your site's package.json and adding this entry to your netlify.toml:

[[plugins]]
  package = "@netlify/plugin-nextjs"

to confirm?

I tested it with the sample project that you had provided but just want to double check before considering this fully resolved.

ericapisani avatar Oct 26 '22 19:10 ericapisani

@ericapisani confirmed and works now. Thanks!

Joroze avatar Oct 28 '22 03:10 Joroze