next.js的新功能.无论如何,我试图在我的本地环境中使用next-auth在我的react web应用程序中添加登录功能,但每当我点击登录按钮时,它都会重定向到"http://localhost:3000/api/auth/error"

它应该带我到谷歌登录页面,我 Select 哪个帐户登录.

首先, struct 是这样的:

image showing structure of code

这是我的LoginPage.tsx

"use client";
import { signIn, useSession } from "next-auth/react";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/navigation";
import React from "react";

const LoginPage = () => {
  const { status } = useSession();
  const router = useRouter();

  if (status === "loading") {
    return <p>Loading...</p>;
  }
  if (status === "authenticated") {
    router.push("/")
  }

  return (
    <div className="p-4 h-[calc(100vh-9rem)] md:h-[calc(100vh-9rem)] flex items-center justify-center">
      
      {/* BOX */}
      <div className=" h-full shadow-2xl rounded-md flex flex-col md:flex-row md:h-[70%] md:w-full lg:w-[60%] 2xl:w-1/2">
        
        {/* FORM CONTAINER */}
        <div className="p-10 items-center flex flex-col gap-8 md:w-[100%]">
          <h1 className="font-bold text-xl xl:text-3xl">Welcome</h1>
          <p>Log into your account or create a new one using social buttons</p>
          
          // ISSUE IS HERE
          <button
            className="flex gap-4 p-4 ring-1 ring-[#213b5e] rounded-md"
            onClick={() => {
              signIn('google')}}
          >
            <Image
              src="/google.png"
              alt=""
              width={20}
              height={20}
              className="object-contain"
            />
            <span>Sign in with Google</span>
          </button>
          <button className="flex gap-4 p-4 ring-1 ring-[#213b5e] rounded-md"
          onClick={() => signIn("github")}>
            <Image
              src="/facebook.png"
              alt=""
              width={20}
              height={20}
              className="object-contain"
            />
            <span>Sign in with GitHub</span>
          </button>
          <p className="text-sm">
            Have a problem?
            <Link className="underline" href="/">
              {" "}
              Contact us
            </Link>
          </p>
        </div>
      </div>
    </div>
  );
};

export default LoginPage;

这是我的options.ts代码

import { NextAuthOptions, User, getServerSession } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";
import { prisma } from "./connect";

declare module "next-auth" {
  interface Session {
    user: User & {
      isAdmin: Boolean;
    };
  }
}
declare module "next-auth/jwt" {
  interface JWT {
    isAdmin: Boolean;
  }
}

export const options: NextAuthOptions = {

    debug: true,
  session: {
    strategy: "jwt",
  },
  providers: [
    GoogleProvider({
      // clientId: process.env.GOOGLE_ID as string,
      // clientSecret: process.env.GOOGLE_SECRET as string,
      clientId: process.env.GOOGLE_ID!,
      clientSecret: process.env.GOOGLE_SECRET!,
    }),

    GithubProvider({
        // clientId: process.env.GOOGLE_ID as string,
        // clientSecret: process.env.GOOGLE_SECRET as string,
        clientId: process.env.GITHUB_ID!,
        clientSecret: process.env.GITHUB_SECRET!,
      }),
  ],
  secret: "PLACE-HERE-ANY-STRING",
};

export const getAuthSession = () => getServerSession(options);

我的路由.TS代码

import { options } from "./options";
import NextAuth from "next-auth";

const handler = NextAuth(options);

export {handler as GET, handler as POST}

这是我的.env文件代码

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/mydb?schema=public"
NEXTAUTH_SECRET=This is text
NEXTAUTH_URL=https://localhost:3000
GOOGLE_ID= 977467927902-trcb4gg3reqj34b6drhjs5novnftou12.apps.googleusercontent.com
GOOGLE_SECRET= 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
GITHUB_ID=f83f7e3e91fef7d3af71
GITHUB_SECRET=81432edd1610b06fe0aec444ce8c679e26f48f07

我try 了删除API文件夹并重新创建、在环境文件中添加NEXTAUTH_URL等其他解决方案.

我还正确设置了Google和Github OAuth客户端,我使用的授权重定向URI

那就是谷歌的http://localhost:3000/api/auth/callback/google GitHub的http://localhost:3000/api/auth/callback/github

我使用的是最新版本的Next-auth和next.js

推荐答案

感谢@oluwatowo-rosanwo comments

删除我的.Next文件夹并重新启动服务器后,登录功能现在就可以使用了.太疯狂了.

Reactjs相关问答推荐

使用useEffect挂钩获取数据

为什么VScode中的React应用程序无法识别文件夹 struct ?

如何防止使用useCallback和memo重新渲染

在Next.js中实现动态更改的服务器组件

Redux Provider Stuck甚至彻底遵循了文档

可以使用mode.css/mode.scss引用常规的类名吗?

试图从Amazon-Cogito-Identity-js模拟CogitoUser.authateUser,并将其获取为未定义的S

点击页脚链接时,如何更改ReactJS导航栏中的活动菜单项(样式为Tailwind CSS)?

如何更新 FunctionComponentElement 的 props

在React Router 6中,在路由渲染后更新路由数据

ReactJS on AWS Amplify与EC2上的Spring boot服务之间的混合内容错误

React Native 背景动画反转

Firebase Storage: 对象不存在图片上传路径错误

React 测试库包装器组件

条件渲染元素的测试不起作用

页面不显示

在 React 的父级中多次调用子级时从子级获取数据到父级

如何使用 React Hook Form 将寄存器作为 props 发送到子组件来收集数据?

网格项不缩小以适应包含的可滚动列表

在 Redux 中更新布尔状态时出错