Next.js使用NextAuth Google Provider登录回调超时错误求助
问题描述
我用Next.js结合NextAuth的Google Provider开发项目,本地localhost环境登录出错。已完成Google Cloud相关配置:创建OAuth客户端,设置授权重定向URI(http://localhost:3000、http://localhost:3000/api/auth/callback/google),授权JavaScript来源(http://localhost:3000)。项目中已配置GOOGLE_ID、GOOGLE_CLIENT_SECRET、MONGODB_URI等环境变量。此前登录正常,退出后再次登录出现以下问题:
- 浏览器提示「Try signing in with a different account.」
- 服务器返回
[SIGNIN_OAUTH_ERROR]错误,提示「outgoing request timed out after 3500ms」
错误堆栈信息:
[next-auth][error][SIGNIN_OAUTH_ERROR] outgoing request timed out after 3500ms { error: { message: 'outgoing request timed out after 3500ms', stack: 'RPError: outgoing request timed out after 3500ms\n' + ' at eval (webpack-internal:///(sc_server)/./node_modules/openid-client/lib/helpers/request.js:115:19)\n' + ' at async Issuer.discover (webpack-internal:///(sc_server)/./node_modules/openid-client/lib/issuer.js:135:30)\n' + ' at async openidClient (webpack-internal:///(sc_server)/./node_modules/next-auth/core/lib/oauth/client.js:12:18)\n' + ' at async getAuthorizationUrl (webpack-internal:///(sc_server)/./node_modules/next-auth/core/lib/oauth/authorization-url.js:88:20)\n' + ' at async Object.signin (webpack-internal:///(sc_server)/./node_modules/next-auth/core/routes/signin.js:21:30)\n' + ' at async AuthHandler (webpack-internal:///(sc_server)/./node_modules/next-auth/core/index.js:259:36)\n' + ' at async NextAuthRouteHandler (webpack-internal:///(sc_server)/./node_modules/next-auth/next/index.js:49:30)\n' + ' at async NextAuth._args$ (webpack-internal:///(sc_server)/./node_modules/next-auth/next/index.js:83:24)\n' + ' at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:265:37)', name: 'RPError' }, providerId: 'google', message: 'outgoing request timed out after 3500ms' }
我的NextAuth配置文件路径:app/api/auth/[...nextauth]/route.js,代码如下:
import NextAuth from 'next-auth'; import GoogleProvider from 'next-auth/providers/google'; import User from '@models/user'; import { connectToDB } from '@utils/database'; const handler = NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }) ], callbacks: { async session({ session }) { // 从MongoDB获取用户ID并存入session const sessionUser = await User.findOne({ email: session.user.email }); session.user.id = sessionUser._id.toString(); return session; }, async signIn({ account, profile, user, credentials }) { try { await connectToDB(); // 检查用户是否已存在 const userExists = await User.findOne({ email: profile.email }); // 不存在则创建新用户存入MongoDB if (!userExists) { await User.create({ email: profile.email, username: profile.name.replace(" ", "").toLowerCase(), image: profile.picture, }); } return true } catch (error) { console.log("Error checking if user exists: ", error.message); return false } }, } }) export { handler as GET, handler as POST }
解决方案
从错误堆栈看,超时发生在Issuer.discover阶段,也就是NextAuth尝试获取Google的OpenID配置时请求超时,以下是排查和解决方向:
1. 检查网络连接
- 确认本地网络能正常访问Google相关域名(如
accounts.google.com、openidconnect.googleapis.com),可通过浏览器直接访问或curl测试:curl https://accounts.google.com/.well-known/openid-configuration - 若网络受限(如公司内网、代理设置),需配置本地代理让Node.js进程能访问外部网络,或切换到正常网络环境测试。
2. 延长NextAuth请求超时时间
NextAuth默认请求超时为3500ms,针对网络不稳定场景可手动延长:
在GoogleProvider配置中添加httpOptions,设置更长的超时时间:
GoogleProvider({ clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, httpOptions: { timeout: 10000 // 10秒超时 } })
3. 排查MongoDB连接是否阻塞请求
虽然错误指向OAuth请求,但signIn回调中的MongoDB操作若缓慢或阻塞,也可能间接影响登录流程:
- 检查本地MongoDB服务是否正常运行,
MONGODB_URI连接字符串是否正确 - 给
User集合的email字段添加索引,优化findOne查询效率 - 可临时注释掉
signIn和session回调中的MongoDB操作,测试是否仍会超时,定位问题来源
4. 清除本地缓存与会话
- 清除浏览器Cookie和本地存储中NextAuth相关的会话数据
- 重启Next.js开发服务器并清除Node.js缓存:
npm run dev -- --clear-cache
5. 验证Google Cloud OAuth配置
- 确认OAuth客户端状态为「已发布」(测试状态下可能存在账号访问限制)
- 核对重定向URI和JavaScript来源,确保与本地项目地址完全一致(包括端口号)
内容的提问来源于stack exchange,提问作者s.mahmood.h
相关产品推荐
相关产品推荐

