这整个星期我都在摸索。我已经尝试了几乎所有的方法。我正在尝试使用 next-auth + google 提供商。
在 GCP 中,在授权网址和重定向网址中:
本地主机:3000
真实域名网站
本地主机:3000/api/auth/callback/google
realdomain.com/api/auth/callback/google
[...nextauth].js:
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import FacebookProvider from "next-auth/providers/facebook";
export default NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET
})
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async session({ session, token }) {
return session;
},
},
})
NEXTAUTH_URL=realdomain.com
通过上面的配置,它可以在本地运行。但在生产中,它总是重定向到 localhost:3000/api/auth/callback/google 并且不起作用。所以,我删除了GCP中的所有本地域,仍然不起作用。需要注意一件事:
当我console.log(providers)时
即使在生产应用程序中也是如此:
google: Object { id: "google", name: "Google", type: "oauth", … }
callbackUrl: "http://localhost:3000/api/auth/callback/google"
id: "google"
name: "Google"
signinUrl: "http://localhost:3000/api/auth/signin/google"
type: "oauth"
仍然是本地主机。
错误:
Access blocked: This app’s request is invalid
Error 400: redirect_uri_mismatch
You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.
If you're the app developer, register the redirect URI in the Google Cloud Console.
Request details: redirect_uri=http://localhost:3000/api/auth/callback/google
“下一个验证”:“^4.20.1”
thanks!