Next.js Discord

Discord Forum

NextAuth Timeout

Unanswered
Basenji posted this in #help-forum
Open in Discord
BasenjiOP
My auth works on dev localhost however not on vercel... and I can't figure out why. I'm not getting a clear error in the logs either, its just a 504 error.

8 Replies

BasenjiOP
I'm sure I have the user info as I have logged it (so google has given it to me), but it still just 504's on the callback route.
My /api/auth/[...nextauth]/route.ts:
import { UserModel } from '@/db';
import { NextAuthOptions } from 'next-auth';
import NextAuth from 'next-auth/next';
import GoogleProvider from 'next-auth/providers/google';
import { session } from '@/lib/session';

const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID!;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET!;

const authOption: NextAuthOptions = {
    session: {
        strategy: 'jwt',
    },
    providers: [
        GoogleProvider({
            clientId: GOOGLE_CLIENT_ID,
            clientSecret: GOOGLE_CLIENT_SECRET,
        }),
    ],
    callbacks: {
        async signIn({ user }) {
            console.log('signIn callback', user);
            if (!user?.email) {
                throw new Error('No User');
            }
            const existingUser = await UserModel.findOne({ email: user.email });
            if (!existingUser) {
                await UserModel.create({
                    username: user.name,
                    email: user.email,
                    avatar: user.image,
                });
            } else {
                await existingUser.updateOne({
                    username: user.name,
                    avatar: user.image,
                });
            }
            return true;
        },
        session,
        async jwt({ token, user, account, profile }) {
            console.log(`jwt callback`, { token, user, account, profile });
            if (profile) {
                const user = await UserModel.findOne({ email: profile.email });
                if (!user) {
                    throw new Error('No user found');
                }
                token.id = user.id;
            }
            return token;
        },
    },
    debug: true,
};

const handler = NextAuth(authOption);
export { handler as GET, handler as POST };
Oak rough bulletgall wasp
Do you have NEXTAUTH_URL and NEXTAUTH_SECRET defined in your env?
Also depending on what vercel plan you have, I think the hobby plan only allows for api requests to take 5 seconds or less and if it takes more, it gives a 504. Not 100% sure though
@Basenji
Plott Hound
Did you setup another app for your google provider for prod? The dev callback will probably have localhost hardcoded in so you’d need to make another one with your actual url for prod and add those to the prod env
BasenjiOP
no i didn’t. i just added the callbacks to the google app but i’m already bright l being redirected to the callback. i get a console log with the user data in the signin callback so it’s something else. would it be that my db is taking too long to respond?
Plott Hound
is the physical distance between your db and app hosting far away?