使用Google身份验证登录而无需打开新窗口/标签

Bob*_* JS 6 javascript google-authentication google-oauth google-oauth2 google-signin

我想在我的网站中实现Google登录,就像在Stack Exchange,Stack Overflow等中完成一样.这意味着当用户选择使用Google身份验证登录时,该网站会重定向到一个页面,在该页面中他可以选择使用哪个Google帐户而无需打开新窗口/标签并重定向到该网站您能否帮我链接/文档如何操作非常重要的是不要打开新的标签/窗口.

我正在使用这个:https://developers.google.com/identity/sign-in/web/build-button

您可以看到,当您单击按钮时,会打开一个新窗口,我会用它来防止这种情况发生.

提前致谢

Max*_*lin 3

您可以尝试切换到 JavaScript 客户端身份验证并将 ux_mode 选项传递给 signIn() 函数选项。

ux_mode: "redirect"
Run Code Online (Sandbox Code Playgroud)

此处的文档: https ://developers.google.com/identity/sign-in/web/reference#googleauthsigninoptions

然后你可以像这样获取用户个人资料:

let googleUser = window.gapi.auth2.getAuthInstance().currentUser.get();

if (googleUser) {
    let profile = googleUser.getBasicProfile();

    if (profile) {
        this.state.google.firstname = profile.getGivenName();
        this.state.google.lastname = profile.getFamilyName();
        this.state.google.email = profile.getEmail();
    }
}
Run Code Online (Sandbox Code Playgroud)

这一切都在这里和那里记录: https ://developers.google.com/identity/sign-in/web/people