使用 Google 自动(新)登录/保持登录状态 - 网络应用

Automatic (new) sign in with Google / stay signed in - web app

TLDR:我一直在努力使用新的 Google 登录功能,尤其是如何让用户保持登录状态的部分。我从文档中了解到 Google 只告诉“这是一个想要登录的用户”,但基本上我仍然需要创建自己的后端来跟踪该用户。

注意 这个问题是关于 new Sign in with Google functionality, 所有 guides/questions I似乎可以找到关于 legacy 登录的信息,这里有很好的描述: https://developers.google.com/identity/sign-in/web/server-side-flow

最让我困惑的基本上已经在指南的开头说明了: https://developers.google.com/identity/gsi/web/guides/overview?hl=en#user_sign-in_to_your_site

You'll manage per user session state for sign-in to your site.

User sign-in status to their Google Account and your app are independent of each other, except during the sign-in moment itself when you know that the user has successfully authenticated and is signed into their Google Account. Users may remain signed-in, sign-out, or switch to a different Google Account while maintaining an active, signed-in session on your website.

我了解 OAuth 背后的基本原理以及您必须将授权代码交换为访问令牌的部分,并且您可以验证此访问令牌(这在遗留指南中有完美描述),但这只是现在OAuth2 需要访问个人数据。如果我理解正确,此访问令牌可用作特定会话的标识符(作为密码或会话 cookie 的替代方法)。

使用新的登录策略,您将只会获得一个用于识别用户的 JWT。同样显然,获得 JWT 的唯一方法是当用户单击“使用 Google 登录”按钮并在同意屏幕中选择帐户时作为响应(理想情况下应该只出现一次)。

我真正想要实现的是当用户进入网站时我想发送一个请求“嘿Google这个用户正在访问我的网站,你能识别这个会话并且它仍然有效吗?” .

也许我想的太难了,但我只是不明白新的 Google 登录如何真正帮助我记住和验证用户?

在进一步挖掘之后,我在此页面上找到了线索:https://developers.google.com/identity/gsi/web/guides/migration#object_migration_reference_for_user_sign-in

基本上我要找的是折旧的GoogleAuth.isSignedIn.get()函数提供的,但是注释清楚地显示:

Remove. A user's current sign-in status on Google is unavailable. Users must be signed-in to Google for consent and sign-in moments.

结合前面的说法:

You'll manage per user session state for sign-in to your site.

为了验证假设,我对使用 Google 登录的其他 Web 服务进行了一些测试,从 Google 控制台撤销了该网站的登录访问权限,并在重新访问该网站时我仍然登录到该网站。

我的结论: Google 登录仅验证 初始 登录 Google 基本上回应“是的,这是一个有效的用户” 我必须自己使用 cookies/databases

来跟踪用户会话