跳过内容

Google登录设置

GitHub

在本指南中,您将学习如何设置 Google 登录与 Capgo 社交登录。您需要以下内容才能设置 Google 登录:

  • Google 帐户

一般设置

注意

请前往

  1. console.cloud.google.com 点击项目选择器
  2. Google Console 项目选择器 Google 登录需要 Google 帐户
  3. 如果您还没有一个项目,请 创建一个新项目.
    1. 点击 New project Google Console 中的新项目按钮
    2. 命名您的项目并点击 Create 项目命名屏幕显示名称字段和创建按钮
    3. 确保您在正确的项目中 项目名称在选择器中显示,表示正确的项目选择
  4. 开始配置 OAuth consent screen
    1. 点击搜索栏

      Google Console 搜索栏
    2. 搜索 OAuth consent screen 点击它

      显示 OAuth 同意屏幕选项的搜索结果
    3. 配置同意屏幕

      点击 create

      OAuth 同意屏幕用户类型选择,显示外部和内部选项
  5. 填写您的应用程序信息
    1. 让我们从 App Information

      应用程序信息部分开始,显示应用程序名称和用户支持邮箱字段
      • 请输入您的 App Name
      • 输入 user support email
      1. 可以 添加应用程序Logo CAN

        OAuth 同意屏幕中的应用程序 logo 上传部分
      2. 应该 配置 App domain

        应用程序域名配置部分,授权域名字段
      3. 必须 提供开发者的电子邮件

        开发者联系信息部分,包含电子邮件字段
      4. 点击 save and continue

        保存并继续按钮(位于表单底部)
  6. 配置权限范围
    1. 点击 add or remove scopes

      添加或删除权限范围按钮(位于权限配置屏幕)
    2. 选择以下权限并点击 update

      权限选择对话框,包含电子邮件和个人资料权限
    3. 点击 save and continue

      范围屏幕中的保存并继续按钮
  7. 添加测试用户
    1. 点击 add users 测试用户部分中的添加用户按钮
    2. 输入您的Google邮箱,点击回车,然后点击 add 测试用户邮箱输入字段和添加按钮
    3. 点击 save and continue 测试用户屏幕中的保存并继续按钮
  8. 点击 back to dashboard 完成页面底部的返回到仪表板按钮
  9. 提交您的应用程序进行验证

There are multiple ways to use Google Login with Capacitor. Here is a table that summarizes the differences between the two:

离线访问需要后端
长期访问令牌
需要后端
Easy setup

如果您仍然不知道应该选择哪一个,请考虑以下场景:

  1. 您希望用户登录,立即之后您将为他发行一个自定义 JWT。您的应用程序将不会调用 Google API

    在这种情况下,请选择在线访问。

  2. 您的应用程序将从客户端调用一些 Google API,但永远不会从后端调用

    在这种情况下,请选择在线访问

  3. 您的应用程序将从后端调用一些 Google API,但仅当用户正在积极使用应用程序时

    在这种情况下,请选择在线访问

  4. 您的应用程序将定期检查用户的日历,即使他不在使用应用程序时

    在这种情况下,选择离线访问

在线访问的示例后端

标题:在线访问的示例后端

在本教程的这一部分,我将展示如何在您的后端验证用户。

这个示例将非常简单,它将基于以下技术:

您可以在这里找到code的示例 如您所见:

VS __CAPGO_KEEP_0__显示Google身份验证 __CAPGO_KEEP_1__,验证令牌

VS Code showing Google authentication code that verifies tokens

The idea is rather simple. You send a simple GET request to https://www.googleapis.com/oauth2/v3/tokeninfo and this returns you whether the token is valid or not and if it it is, it gives you the email of the user. It also gives you some other info about the user token

Google OAuth Playground showing token information response with user details

从那里,您可以为用户颁发自己的 JWT 或颁发会话 cookie。最终认证实现的可能性无穷尽。

如果您想调用 Google API,我强烈建议您查看 Google’s OAuth 2.0 Playground。从那里您可以轻松看到可以调用的 API。

使用离线访问与您的后端

使用离线访问需要以下内容:

一个 HTTP 服务器

  • 一个 HTTP 服务器

本例中,我将使用以下技术来为我的应用提供离线访问:

本例的code可以在 这里

关于客户端code,它看起来像这样:

import { Capacitor } from '@capacitor/core';
import { GoogleLoginOfflineResponse, SocialLogin } from '@capgo/capacitor-social-login';
import { usePopoutStore } from '@/popoutStore'; // <-- specific to my app
const baseURL = "[redacted]";
async function fullLogin() {
await SocialLogin.initialize({
google: {
webClientId: '[redacted]',
iOSClientId: '[redacted]',
iOSServerClientId: 'The same value as webClientId',
mode: 'offline' // <-- important
}
})
const response = await SocialLogin.login({
provider: 'google',
options: {
forceRefreshToken: true // <-- important
}
})
if (response.provider === 'google') {
const result = response.result as GoogleLoginOfflineResponse
const res = await fetch(`${baseURL}/auth/google_offline`, {
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
serverAuthCode: result.serverAuthCode,
platform: Capacitor.getPlatform()
}),
method: "POST"
})
if (res.status !== 200) {
popoutStore.popout("Full google login failed", "check console");
return
}
const { jwt } = await res.json();
const userinfo = await fetch(`${baseURL}/auth/get_google_user`, {
headers: {
Authorization: `Bearer ${jwt}`
}
})
if (userinfo.status !== 200) {
popoutStore.popout("Full google (userinfo) login failed", "check console");
return
}
popoutStore.popout("userinfo res", await userinfo.text());
}
}

注意这里什么都没有:没有 SocialLogin.refresh() 在应用中调用。 这是故意的。在 Google 离线模式下,刷新发生在您的后端与刷新令牌的交换和存储之后。 serverAuthCode 并且安全地存储刷新令牌。

如果您正在使用 Google 登录设置 来规划身份验证和帐户流程,连接它到 使用 @capgo/capacitor-social-login 为原生能力在使用 @capgo/capacitor-social-login 中 @capgo/capacitor-social-login 为实现细节在 @capgo/capacitor-social-login 中 @capgo/capacitor-passkey 对于 @capgo/capacitor-passkey 的实现细节 @capgo/capacitor-native-biometric 对于 @capgo/capacitor-native-biometric 的实现细节, 双因素认证 对于双因素认证的实现细节。