在 Next.js 应用程序中将 Google 身份验证与 NextAuth 集成

问题描述 投票:0回答:1

我有这个 auth.js 文件用于配置:

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";

export const {
    handlers : { GET, POST },
} = NextAuth({
   providers: [
       GoogleProvider({
           clientId: process.env.GOOGLE_CLIENT_ID,
           clientSecret: process.env.GOOGLE_CLIENT_SECRET,
           authorization: {
               params: {
                   prompt: "consent",
                   access_type: "offline",
                   response_type: "code"
               }
           }
       })
   ]
});

我从 src/app/api/auth/[...nextauth]/route.js 文件导出它:

export { GET, POST } from '../../../../auth.js';

但是当我尝试访问 http://localhost:3000/api/auth/signin 时出现此错误:

enter image description here

这是我的package.json:

{
  "name": "frontend",
  "version": "0.1.0",
  "type": "module",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "^15.0.2",
    "next-auth": "^4.24.10",
    "react": "^18.3.1",
    "react-dom": "^18.3.1"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "15.0.2",
    "postcss": "^8",
    "tailwindcss": "^3.4.1",
    "typescript": "^5"
  }
}

next.js google-oauth next-auth
1个回答
0
投票

通过更改 beta 上的下一个身份验证版本来解决:

npm i next-auth@beta
© www.soinside.com 2019 - 2024. All rights reserved.