NextAuth重定向到Vercel上的localhost

vatpfxk5  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(196)

我正在使用NextAuth 4.22.0和Next.js 13.3.0以及应用程序文件夹结构。当我处于开发模式时,身份验证工作正常,并且会像我期望的那样重定向回localhost:3000,但当应用程序部署到Vercel时,它仍然重定向到localhost:3000。我错过了什么?
这是我的NextAuth路由文件(路径:app/API/auth/[...nextauth]/route.ts):

import NextAuth, { type NextAuthOptions } from "next-auth"

// Providers
import GoogleProvider from "next-auth/providers/google"
import FacebookProvider from "next-auth/providers/facebook"
import GitHubProvider from "next-auth/providers/github"

// Prisma Adapter
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient()

export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),

  secret: process.env.SECRET,

  session: {
    strategy: "jwt",
  },
  providers: [
    GoogleProvider({
      // @ts-ignore
      clientId: process.env.GOOGLE_CLIENT_ID,
      // @ts-ignore
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
    FacebookProvider({
      // @ts-ignore
      clientId: process.env.FACEBOOK_CLIENT_ID,
      // @ts-ignore
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
    }),
    GitHubProvider({
      // @ts-ignore
      clientId: process.env.GITHUB_ID,
      // @ts-ignore
      clientSecret: process.env.GITHUB_SECRET,
    }),
  ],
  callbacks: {
    session: ({ session, token }) => {
      return {
        ...session,
        user: {
          ...session.user,
          id: token.id,
          randomKey: token.randomKey,
        },
      }
    },
    jwt: ({ token, user }) => {
      if (user) {
        const u = user as unknown as any
        return {
          ...token,
          id: u.id,
          randomKey: u.randomKey,
        }
      }
      return token
    },
  },
}

const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }

这是我的handleSignIn函数:

const handleSignIn = async (provider: string) => {
    switch (provider) {
      case "github":
        setIsSigningInWithGitHub(true)
        break
      case "google":
        setIsSigningInWithGoogle(true)
        break
      case "facebook":
        setIsSigningInWithFacebook(true)
        break
      default:
        break
    }
    await signIn(provider, {
      redirect: false,
      callbackUrl: `/`,
    })

    setIsSigningInWithGitHub(false)
    setIsSigningInWithGoogle(false)
    setIsSigningInWithFacebook(false)
  }

当点击像这样的按钮时,我会调用它:

<button
  type="button"
  onClick={() => handleSignIn("github")}
>
  <FaGithub className="mr-2 -ml-1 h-4 w-4" />
  {isSignIn ? "Continue with GitHub" : "Sign up with GitHub"}
</button>

它将我重定向到其中一个提供商,我可以通过他们进行身份验证,但一旦我这样做,它就会将我重定向回:
http://localhost:3000/api/auth/signin?error=OAuthCallback应该是我的域名。我做错了什么?

mum43rcc

mum43rcc1#

当你向提供商注册你的应用时,你添加了Authorized redirect URIs。你可能只有这些:

但是既然你部署了你的应用程序,你也应该传递你的生产域

相关问题