NextAuth Google 提供商无法在 Safari 上运行

发布于 2025-01-17 12:23:09 字数 1364 浏览 2 评论 0原文

Google提供商在Chrome和其他浏览器上工作得很好,但无法在Safari上工作。我似乎在这里的文档中找不到任何相关的内容。

这就是在[... NextAuth]中声明我的提供商的方式。JS

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import FacebookProvider from "next-auth/providers/facebook";

export default NextAuth({
    debug: true,
    providers: [
        GoogleProvider({
            clientId: process.env.GOOGLE_CLIENT_ID,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET,
            state: false,
            authorizationUrl:
        'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
            
        }), ...

在Safari上与Google登录的函数

async function signInWithGoogle() {
        const res: any = await signIn("google");

        if (res && res.error) {
            setErrorMsg(res.error);
            setShowError(true);
        } else if(!res) {
            router.push("/account-error")
        }
        else {
            router.push("/somepage");
        }
    }

res对象始终不确定。我可以在控制台日志中看到Next Auth能够在 [next-auth] [debug] [get_authorization_url] 中生成有效的授权URL,但是该应用程序不仅不会在Safari上重定向。

The google provider works perfectly fine on Chrome and other browsers but fails to work on Safari. I can't seem to find anything relevant in the documentation here.

This is how my provider is declared in [...nextauth].js

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import FacebookProvider from "next-auth/providers/facebook";

export default NextAuth({
    debug: true,
    providers: [
        GoogleProvider({
            clientId: process.env.GOOGLE_CLIENT_ID,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET,
            state: false,
            authorizationUrl:
        'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
            
        }), ...

The function calling sign in with Google

async function signInWithGoogle() {
        const res: any = await signIn("google");

        if (res && res.error) {
            setErrorMsg(res.error);
            setShowError(true);
        } else if(!res) {
            router.push("/account-error")
        }
        else {
            router.push("/somepage");
        }
    }

On safari the res object is always undefined. I can see in the console logs that next auth is able to generate a valid authorization URL in [next-auth][debug][GET_AUTHORIZATION_URL] but the app wouldn't redirect on Safari only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2025-01-24 12:23:09

结果在 Safari 上,下一个身份验证并没有解决承诺。我删除了检查资源的代码,它工作得很好。它被重定向而没有完成使用谷歌登录的请求。让它留在这里,以防有人遇到这个问题。

所以你的函数调用应该是

async function signInWithGoogle() {
    
     const res: any = await signIn("google");

}

Turns out on Safari next auth was not resolving the promise. I removed the code where I checked for res and it worked fine. It was being redirected without the request getting completed to sign in with google. Letting it stay here in case any one gets this issue.

So your function call should just be

async function signInWithGoogle() {
    
     const res: any = await signIn("google");

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文