DEV Community

Mathew Chan
Mathew Chan

Posted on

1

Resolving next-auth "redirect" error when used with Supabase

Recently users on my side-project website have been experiencing login issues with oAuth (Discord, Twitch). Everything worked fine on localhost, but I would get Redirect error in production (hosted on vercel).

After many hours of trying out different configurations and looking through next-auth Github issues, I decided to upgrade next-auth from v3 to v4. The migration guide is here.

There are many bumps when upgrading, including releasing foreign keys from users table, and reverting migration changes recommended by the migration guide.

-- sessions.expires has to remain of type TIMESTAMPTZ instead of TEXT
-- don't run this line when migrating your postgres table
ALTER TABLE sessions ALTER COLUMN "expires" TYPE TEXT USING CAST(CAST(extract(epoch FROM "expires") AS BIGINT)*1000 AS TEXT);
Enter fullscreen mode Exit fullscreen mode

To my dismay, however, that didn't resolve the issue.

I recalled that Supabase was dropping IPv4 support and moving to IPv6. Apparently, with that change, they are also dropping PGBouncer.

The solution was to change the connection string to avoid using PGBouncer.

# .env.local
DATABASE=postgres://postgres:[password]@db.[project-ref].supabase.co:5432/postgres
## DATABASE=postgres://postgres:[password]@aws-0-us-east-1.pooler.supabase.com:5432/postgres?options=reference%3D[project-ref]
Enter fullscreen mode Exit fullscreen mode

And then update next-auth config pages/api/auth/[...nextauth].js to use the connection string.

import NextAuth from 'next-auth'
import DiscordProvider from "next-auth/providers/discord";
import TwitchProvider from "next-auth/providers/twitch";
import PostgresAdapter from "@auth/pg-adapter"
import { Pool } from 'pg'

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
  ssl: {
    rejectUnauthorized: false
  }
})

export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    DiscordProvider({
      clientId: process.env.DISCORD_CLIENT_ID,
      clientSecret: process.env.DISCORD_CLIENT_SECRET
    }),
    TwitchProvider({
      clientId: process.env.TWITCH_CLIENT_ID,
      clientSecret: process.env.TWITCH_CLIENT_SECRET
    })
    // ...add more providers here
  ],
  secret: process.env.NEXTAUTH_SECRET,
  callbacks: {
    session: async (session, user) => {
      return session;
    }
  },
  pages: {
    signIn: '/auth/signin',
  },

  adapter: PostgresAdapter(pool),
})
Enter fullscreen mode Exit fullscreen mode

Also remember to include a next-auth secret when upgrading to version 4 of next-auth.

Version 5 is already in beta of next-auth, but this upgrading experience has definitely made me rethink about making any breaking changes to my database.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay