NextAuth not working with Next's 13 on Netlify

Netlify site: https://devlifyio.netlify.app
Project: GitHub - caglarop/copjs: CopJS is a feature-rich and flexible web application boilerplate built on Next.js 13.3, designed to streamline your development process. With a modern app directory structure and Metadata API, CopJS empowers you to build web applications with ease.

Sign in with CredentialsProvider and Social Media (Discord, Github) not working. Checked the CredentialsProvider Login and got empty 200 code response from /api/auth/session

On localhost it’s work perfectly, tested with npm run dev and npm start (builded version).

Env vars are correct, no errors or something.

Updated all node modules to the latest version.

Hi @caglarop :wave:t6:, welcome to the forums! Can you confirm you configured your backend code and the URLs and parameters to match the config in your env variables?

Also would you mind sharing your latest deploy where you aren’t seeing NextAuth in effect?

1 Like

Hi @SamO ,

NEXTAUTH_URL=netlify domain
NEXTAUTH_SECRET=****

The script is working perfectly on localhost and on a vps (dev and builded version).

You can check it on devlifyio.netlify.app and on my github repo above

Yes but I also need a link to your failed deploy/build log. Can you share that ?

@SamO

I have not failed deploy. No errors. The user registration is working (creating the user) but the sign in is not working. It is anything with the session provider I think, maybee Netlify has some problems with the newest nextjs 13.3 Update (NextAuth with auth directory). I dont know exactly.

Not sure if this is related, but I’m seeing that, even if you start a login from devlifyio, you get redirected to main--devlifyio. Is this expected? From what I know, the URL needs to be same, right?

Thank you for the tip, but unfortunately, the error still persists.

4:09:34 AM: build-image version: 91488c6c0412f5c1c00fb34eff5c1fc41a32bce9 (focal)
4:09:34 AM: buildbot version: 91488c6c0412f5c1c00fb34eff5c1fc41a32bce9
4:09:34 AM: Building without cache
4:09:34 AM: Starting to prepare the repo for build
4:09:34 AM: No cached dependencies found. Cloning fresh repo
4:09:34 AM: git clone --filter=blob:none https://github.com/caglarop/copjs
4:09:35 AM: Preparing Git Reference refs/heads/main
4:09:35 AM: Parsing package.json dependencies
4:09:36 AM: Different build command detected, going to use the one specified in the Netlify configuration file: 'npm run build && npx prisma generate && npx prisma db push' versus 'npm run build' in the Netlify UI
4:09:37 AM: Starting to install dependencies
4:09:37 AM: Python version set to 3.8
4:09:37 AM: Attempting Ruby version 2.7.2, read from environment
4:09:37 AM: Using Ruby version 2.7.2
4:09:38 AM: Started restoring cached go cache
4:09:38 AM: Finished restoring cached go cache
4:09:38 AM: Installing Go version 1.19.5 (requested 1.19.5)
4:09:43 AM: go version go1.19.5 linux/amd64
4:09:43 AM: Using PHP version 8.0
4:09:44 AM: v16.20.0 is already installed.
4:09:45 AM: Now using node v16.20.0 (npm v8.19.4)
4:09:45 AM: Enabling Node.js Corepack
4:09:45 AM: Started restoring cached build plugins
4:09:45 AM: Finished restoring cached build plugins
4:09:45 AM: Started restoring cached corepack dependencies
4:09:45 AM: Finished restoring cached corepack dependencies
4:09:45 AM: No npm workspaces detected
4:09:45 AM: Started restoring cached node modules
4:09:45 AM: Finished restoring cached node modules
4:09:45 AM: Installing npm packages using npm version 8.19.4
4:10:34 AM: npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility
4:10:37 AM: npm WARN deprecated @types/nookies@2.0.3: This is a stub types definition. nookies provides its own type definitions, so you do not need this installed.
4:10:47 AM: > copjs@0.1.0 prepare
4:10:47 AM: > husky install
4:10:47 AM: husky - Git hooks installed
4:10:47 AM: added 1815 packages, and audited 1816 packages in 1m
4:10:47 AM: 372 packages are looking for funding
4:10:47 AM:   run `npm fund` for details
4:10:47 AM: found 0 vulnerabilities
4:10:47 AM: npm packages installed
4:10:48 AM: Install dependencies script success
4:10:48 AM: Starting build script
4:10:48 AM: Detected 1 framework(s)
4:10:48 AM: "next" at version "13.3.0"
4:10:48 AM: Section completed: initializing
4:10:50 AM: ​
4:10:50 AM:   Netlify Build                                                 
4:10:50 AM: ────────────────────────────────────────────────────────────────
4:10:50 AM: ​
4:10:50 AM: ❯ Version
4:10:50 AM:   @netlify/build 29.9.2
4:10:50 AM: ​
4:10:50 AM: ❯ Flags
4:10:50 AM:   baseRelDir: true
4:10:50 AM:   buildId: 643dfbdb46073e477703ad92
4:10:50 AM:   deployId: 643dfbdb46073e477703ad94
4:10:50 AM: ​
4:10:50 AM: ❯ Current directory
4:10:50 AM:   /opt/build/repo
4:10:50 AM: ​
4:10:50 AM: ❯ Config file
4:10:50 AM:   /opt/build/repo/netlify.toml
4:10:50 AM: ​
4:10:50 AM: ❯ Context
4:10:50 AM:   production
4:10:50 AM: ​
4:10:50 AM: ❯ Installing plugins
4:10:50 AM:    - @netlify/plugin-nextjs@4.34.0
4:10:59 AM: ​
4:10:59 AM: ❯ Using Next.js Runtime - v4.34.0
4:11:00 AM: ​
4:11:00 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
4:11:00 AM: ────────────────────────────────────────────────────────────────
4:11:00 AM: ​
4:11:00 AM: No Next.js cache to restore.
4:11:00 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
4:11:00 AM: ​
4:11:00 AM: (@netlify/plugin-nextjs onPreBuild completed in 16ms)
4:11:00 AM: ​
4:11:00 AM:   2. build.command from netlify.toml                            
4:11:00 AM: ────────────────────────────────────────────────────────────────
4:11:00 AM: ​
4:11:00 AM: $ npm run build && npx prisma generate && npx prisma db push
4:11:00 AM: > copjs@0.1.0 build
4:11:00 AM: > next build
4:11:01 AM: warn  - You have enabled experimental features (appDir, serverComponentsExternalPackages) in next.config.js.
4:11:01 AM: warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
4:11:01 AM: info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
4:11:01 AM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
4:11:01 AM: info  - Creating an optimized production build...
4:11:01 AM: warn - As of Tailwind CSS v2.2, `lightBlue` has been renamed to `sky`.
4:11:01 AM: warn - Update your configuration file to silence this warning.
4:11:01 AM: warn - As of Tailwind CSS v3.0, `warmGray` has been renamed to `stone`.
4:11:01 AM: warn - Update your configuration file to silence this warning.
4:11:01 AM: warn - As of Tailwind CSS v3.0, `trueGray` has been renamed to `neutral`.
4:11:01 AM: warn - Update your configuration file to silence this warning.
4:11:01 AM: warn - As of Tailwind CSS v3.0, `coolGray` has been renamed to `gray`.
4:11:01 AM: warn - Update your configuration file to silence this warning.
4:11:01 AM: warn - As of Tailwind CSS v3.0, `blueGray` has been renamed to `slate`.
4:11:01 AM: warn - Update your configuration file to silence this warning.
4:11:14 AM: info  - Compiled successfully
4:11:14 AM: info  - Linting and checking validity of types...
4:11:21 AM: info  - Collecting page data...
4:11:36 AM: info  - Generating static pages (0/12)
4:11:36 AM: info  - Generating static pages (3/12)
4:11:36 AM: info  - Generating static pages (6/12)
4:11:36 AM: info  - Generating static pages (9/12)
4:11:36 AM: info  - Generating static pages (12/12)
4:11:37 AM: info  - Finalizing page optimization...
4:11:37 AM: Route (app)                                Size     First Load JS
4:11:37 AM: β”Œ ● /[lng]                                 3.4 kB          129 kB
4:11:37 AM: β”œ   β”œ /en-US
4:11:37 AM: β”œ   β”” /de-DE
4:11:37 AM: β”œ ● /[lng]/auth/forgot-password            4.53 kB         151 kB
4:11:37 AM: β”œ   β”œ /en-US/auth/forgot-password
4:11:37 AM: β”œ   β”” /de-DE/auth/forgot-password
4:11:37 AM: β”œ ● /[lng]/auth/signin                     3.55 kB         155 kB
4:11:37 AM: β”œ   β”œ /en-US/auth/signin
4:11:37 AM: β”œ   β”” /de-DE/auth/signin
4:11:37 AM: β”œ ● /[lng]/auth/signup                     1.56 kB         153 kB
4:11:37 AM: β”œ   β”œ /en-US/auth/signup
4:11:37 AM: β”œ   β”” /de-DE/auth/signup
4:11:37 AM: β”œ ● /[lng]/housekeeping                    8.46 kB         134 kB
4:11:37 AM: β”œ   β”œ /en-US/housekeeping
4:11:37 AM: β”œ   β”” /de-DE/housekeeping
4:11:37 AM: β”œ Ξ» /api/auth/[...nextauth]                0 B                0 B
4:11:37 AM: β”” Ξ» /api/trpc/[trpc]                       0 B                0 B
4:11:37 AM: + First Load JS shared by all              74.3 kB
4:11:37 AM:   β”œ chunks/2443530c-beeb055b2961595c.js    50.2 kB
4:11:37 AM:   β”œ chunks/961-35f18f27f4f4f89d.js         22 kB
4:11:37 AM:   β”œ chunks/main-app-ab81c95d68fb02d8.js    216 B
4:11:37 AM:   β”” chunks/webpack-702a3140795a4520.js     1.88 kB
4:11:37 AM: Route (pages)                              Size     First Load JS
4:11:37 AM: ─ β—‹ /404                                   178 B          86.3 kB
4:11:37 AM: + First Load JS shared by all              86.1 kB
4:11:37 AM:   β”œ chunks/main-b8dd50d45873262e.js        84.1 kB
4:11:37 AM:   β”œ chunks/pages/_app-c544d6df833bfd4a.js  192 B
4:11:37 AM:   β”” chunks/webpack-702a3140795a4520.js     1.88 kB
4:11:37 AM: Ζ’ Middleware                               25.2 kB
4:11:37 AM: Ξ»  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
4:11:37 AM: β—‹  (Static)  automatically rendered as static HTML (uses no initial props)
4:11:37 AM: ●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
4:11:37 AM: Prisma schema loaded from prisma/schema.prisma
4:11:38 AM: βœ” Generated Prisma Client (4.12.0 | library) to ./node_modules/@prisma/client in 108ms
4:11:38 AM: You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
4:11:38 AM: ```import { PrismaClient } from '@prisma/client'const prisma = new PrismaClient()```
4:11:39 AM: Prisma schema loaded from prisma/schema.prisma
4:11:39 AM: Datasource "db": PostgreSQL database "postgres", schema "public" at "db.tbtilxwfuikhvyjqxyzh.supabase.co:5432"
4:11:43 AM: The database is already in sync with the Prisma schema.
4:11:43 AM: Running generate... (Use --skip-generate to skip the generators)
4:11:44 AM: Running generate... - Prisma Client
4:11:44 AM: βœ” Generated Prisma Client (4.12.0 | library) to ./node_modules/@prisma/client in
4:11:44 AM:  107ms
4:11:44 AM: ​
4:11:44 AM: (build.command completed in 43.7s)
4:11:44 AM: ​
4:11:44 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
4:11:44 AM: ────────────────────────────────────────────────────────────────
4:11:44 AM: ​
4:11:44 AM: NextAuth package detected, NEXTAUTH_URL environment variable set by user in Netlify configuration to https://devlifyio.netlify.app
4:11:44 AM: Patching /opt/build/repo/node_modules/next/dist/server/base-server.js
4:11:44 AM: Done
4:11:44 AM: Patching /opt/build/repo/node_modules/next/dist/server/next-server.js
4:11:44 AM: Done
4:11:44 AM: Moving static page files to serve from CDN...
4:11:44 AM: Moved 0 files
4:11:44 AM: You are not using Netlify Edge Functions for image format detection. Set env var "NEXT_FORCE_EDGE_IMAGES=true" to enable.
4:11:44 AM: ✨ Deploying middleware and functions to Netlify Edge Functions ✨
4:11:44 AM: This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
4:11:44 AM: Netlify configuration property "redirects" value changed to [
4:11:44 AM:   { from: '/_next/static/*', to: '/static/:splat', status: 200 },
4:11:44 AM:   {
4:11:44 AM:     from: '/_next/image*',
4:11:44 AM:     query: { url: ':url', w: ':width', q: ':quality' },
4:11:44 AM:     to: '/_ipx/w_:width,q_:quality/:url',
4:11:44 AM:     status: 301
4:11:44 AM:   },
4:11:44 AM:   { from: '/_ipx/*', to: '/.netlify/builders/_ipx', status: 200 },
4:11:44 AM:   { from: '/cache/*', to: '/404.html', status: 404, force: true },
4:11:44 AM:   { from: '/server/*', to: '/404.html', status: 404, force: true },
4:11:44 AM:   { from: '/serverless/*', to: '/404.html', status: 404, force: true },
4:11:44 AM:   { from: '/trace', to: '/404.html', status: 404, force: true },
4:11:44 AM:   { from: '/traces', to: '/404.html', status: 404, force: true },
4:11:44 AM:   {
4:11:44 AM:     from: '/routes-manifest.json',
4:11:44 AM:     to: '/404.html',
4:11:44 AM:     status: 404,
4:11:44 AM:     force: true
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/build-manifest.json',
4:11:44 AM:     to: '/404.html',
4:11:44 AM:     status: 404,
4:11:44 AM:     force: true
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/prerender-manifest.json',
4:11:44 AM:     to: '/404.html',
4:11:44 AM:     status: 404,
4:11:44 AM:     force: true
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/react-loadable-manifest.json',
4:11:44 AM:     to: '/404.html',
4:11:44 AM:     status: 404,
4:11:44 AM:     force: true
4:11:44 AM:   },
4:11:44 AM:   { from: '/BUILD_ID', to: '/404.html', status: 404, force: true },
4:11:44 AM:   {
4:11:44 AM:     from: '/api/*',
4:11:44 AM:     to: '/.netlify/functions/___netlify-handler',
4:11:44 AM:     status: 200
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/favicon.ico',
4:11:44 AM:     to: '/favicon.ico',
4:11:44 AM:     conditions: { Cookie: [Array] },
4:11:44 AM:     status: 200
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/*',
4:11:44 AM:     to: '/.netlify/functions/___netlify-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     conditions: { Cookie: [Array] },
4:11:44 AM:     force: true
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng.rsc',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/forgot-password.rsc',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/forgot-password',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/signin.rsc',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/signin',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/signup.rsc',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/auth/signup',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/housekeeping.rsc',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/:lng/housekeeping',
4:11:44 AM:     to: '/.netlify/builders/___netlify-odb-handler',
4:11:44 AM:     status: 200,
4:11:44 AM:     force: false
4:11:44 AM:   },
4:11:44 AM:   {
4:11:44 AM:     from: '/*',
4:11:44 AM:     to: '/.netlify/functions/___netlify-handler',
4:11:44 AM:     status: 200
4:11:44 AM:   }
4:11:44 AM: ].
4:11:44 AM: ​
4:11:44 AM: (@netlify/plugin-nextjs onBuild completed in 122ms)
4:11:44 AM: ​
4:11:44 AM:   4. Functions bundling                                         
4:11:44 AM: ────────────────────────────────────────────────────────────────
4:11:44 AM: ​
4:11:44 AM: Packaging Functions from .netlify/functions-internal directory:
4:11:44 AM:  - ___netlify-handler/___netlify-handler.js
4:11:44 AM:  - ___netlify-odb-handler/___netlify-odb-handler.js
4:11:44 AM:  - _ipx/_ipx.js
4:11:44 AM: ​
4:12:14 AM: ​
4:12:14 AM: (Functions bundling completed in 30.2s)
4:12:14 AM: ​
4:12:14 AM:   5. Edge Functions bundling                                    
4:12:14 AM: ────────────────────────────────────────────────────────────────
4:12:14 AM: ​
4:12:14 AM: Packaging Edge Functions from .netlify/edge-functions directory:
4:12:14 AM:  - next_src_middleware
4:12:14 AM:  - rsc-data
4:12:16 AM: Function 'next_src_middleware' uses an unsupported regular expression and will not be invoked:
4:12:16 AM: /^(?:\\/(_next\\/data\\/[^\/]{1,}))?(?:\\/(.*))(.json)?[\\/#\?]?$/
4:12:16 AM:        ^
4:12:16 AM: Unexpected token: "/" at 1:7.
4:12:16 AM: ​
4:12:16 AM: (Edge Functions bundling completed in 1.5s)
4:12:16 AM: ​
4:12:16 AM:   6. @netlify/plugin-nextjs (onPostBuild event)                 
4:12:16 AM: ────────────────────────────────────────────────────────────────
4:12:16 AM: ​
4:12:16 AM: Next.js cache saved.
4:12:16 AM: πŸ§ͺ Thank you for testing "appDir" support on Netlify. For known issues and to give feedback, visit https://ntl.fyi/next-13-feedback
4:12:16 AM: ​
4:12:16 AM: (@netlify/plugin-nextjs onPostBuild completed in 178ms)
4:12:16 AM: ​
4:12:16 AM:   7. Deploy site                                                
4:12:16 AM: ────────────────────────────────────────────────────────────────
4:12:16 AM: ​
4:12:16 AM: Starting to deploy site from '.next'
4:12:17 AM: Calculating files to upload
4:12:17 AM: 38 new files to upload
4:12:17 AM: 3 new functions to upload
4:12:26 AM: Section completed: deploying
4:12:26 AM: Site deploy was successfully initiated
4:12:26 AM: ​
4:12:26 AM: (Deploy site completed in 10.1s)
4:12:26 AM: Starting post processing
4:12:26 AM: ​
4:12:26 AM:   Netlify Build Complete                                        
4:12:26 AM: ────────────────────────────────────────────────────────────────
4:12:26 AM: Post processing - HTML
4:12:26 AM: ​
4:12:26 AM: (Netlify Build completed in 1m 36.6s)
4:12:27 AM: Caching artifacts
4:12:27 AM: Started saving node modules
4:12:27 AM: Finished saving node modules
4:12:27 AM: Started saving build plugins
4:12:27 AM: Finished saving build plugins
4:12:27 AM: Started saving corepack cache
4:12:27 AM: Finished saving corepack cache
4:12:27 AM: Started saving pip cache
4:12:27 AM: Finished saving pip cache
4:12:27 AM: Started saving emacs cask dependencies
4:12:27 AM: Finished saving emacs cask dependencies
4:12:27 AM: Started saving maven dependencies
4:12:27 AM: Finished saving maven dependencies
4:12:27 AM: Started saving boot dependencies
4:12:27 AM: Finished saving boot dependencies
4:12:27 AM: Post processing - header rules
4:12:27 AM: Started saving rust rustup cache
4:12:27 AM: Finished saving rust rustup cache
4:12:27 AM: Started saving go dependencies
4:12:27 AM: Finished saving go dependencies
4:12:27 AM: Build script success
4:12:27 AM: Post processing - redirect rules
4:12:27 AM: Section completed: building
4:12:28 AM: Post processing done
4:12:28 AM: Section completed: postprocessing
4:12:29 AM: Site is live ✨
4:12:29 AM: Uploading Cache of size 284.7MB
4:12:31 AM: Section completed: cleanup
4:12:31 AM: Finished processing build request in 2m56.762s

Hey @caglarop,

I was checking this right now, and I am not finding the site anymore. Has something changed?