Next.js Discord

Discord Forum

NextAuth with Google provider

Unanswered
Labrador Duck posted this in #help-forum
Open in Discord
Labrador DuckOP
I am trying to implement NextAuth with Google in my frontend, it works fine locally, but in production I get this: Try signing in with a different account. My deploy is on Vercel, which has the env variables setted.

7 Replies

Labrador DuckOP
That's my nextauth file:

import { NextApiHandler } from "next";
import NextAuth from "next-auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import GoogleProvider from "next-auth/providers/google";
import prisma from "../../../../lib/prisma";

const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options);
export default authHandler;

const options = {
  adapter: PrismaAdapter(prisma),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code",
        },
      },
    }),
  ],
  callbacks: {
    async session({ session, token }: any) {
      session.user.username = session.user.name
        .split(" ")
        .join("")
        .toLocaleLowerCase();

      session.user.uid = token.sub;
      return session;
    },
    async redirect() {
      return "/";
    },
  },
  secret: process.env.JWT_SECRET,
};
And that's my config in GCP:
Did you set NEXTAUTH_URL on Vercel? If you did, remove it. NextAuth automatically finds out correct NEXTAUTH_URL
Labrador DuckOP
I don't have this variable in Vercel since I don't need it there, but thanks 🙂
Labrador DuckOP
But I might need it on my .env, regarding this docs, which is weird
https://next-auth.js.org/deployment
1. Make sure to expose the Vercel System Environment Variables in your project settings.
2. Create a NEXTAUTH_SECRET environment variable for all environments.
- You can use openssl rand -base64 32 or https://generate-secret.vercel.app/32 to generate a random value.
- You do not need the NEXTAUTH_URL environment variable in Vercel.
3. Add your provider's client ID and client secret to environment variables. (Skip this step if not using an OAuth Provider)
4. Deploy!
Labrador DuckOP
Thanks @maxswjeon ! My env variables in Vercel have this naming convention: