@codetrix-studio/capacitor-google-auth

中文文档
3.4.0-rc.4发布于 2024/05/01MITTypeScriptCommonJS零依赖近 30 天下载 22.4万

电容器的Google Auth插件.

中文使用文档
文档目录16 节
  1. 捐款
  2. 安装
  3. 更新
  4. 使用量
  5. 韦伯
  6. iOS系统
  7. 机器人
  8. 配置
  9. API 密码
  10. 初始化(...)
  11. 签名IN ()
  12. 刷新( )
  13. 符号输出( C)
  14. 接口
  15. 移徙指南
  16. 许可证

Google Auth 电容控制器

@codetrix-studio/capacitor-google-auth

第6条

Google Auth 的电容插件.


npm npm npm Dependents (via libraries.io) install size

捐款

PR受到欢迎并非常赞赏,它使这个插件与Capacitor和官方Google Auth平台库的功能等同.

尽量遵循良好的代码惯例. 你甚至可以帮助保持 包含的演示更新.

不与Google Auth 官方库对齐的特性的公关不便.

(我们在这里是初学者友好的)

安装

1. 安装包

npm i --save @codetrix-studio/capacitor-google-auth

# pnpm 
pnpm add @codetrix-studio/capacitor-google-auth

# yarn 
yarn add @codetrix-studio/capacitor-google-auth

2. 更新电容器道具

npx cap update

更新

如果需要迁移到不同的控制器版本 ưμ㼯a。 。 。 .

使用量

韦伯

注册插件并手动初始化

import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

// use hook after platform dom ready
GoogleAuth.initialize({
  clientId: 'CLIENT_ID.apps.googleusercontent.com',
  scopes: ['profile', 'email'],
  grantOfflineAccess: true,
});

或者如果需要使用元标记(可选) :

<meta name="google-signin-client_id" content="{your client id here}" />
<meta name="google-signin-scope" content="profile email" />

选项

  • clientId - 该应用程序的客户端ID,在Google开发者控制台中找到并创建.
  • scopes - 与 配置 范围
  • grantOfflineAccess —布尔,默认值 false,设定当用户不在浏览器时,您的应用程序需要刷新访问符.

用这个

GoogleAuth.signIn();

角质

输入钩子

// app.component.ts
constructor() {
  this.initializeApp();
}

initializeApp() {
  this.platform.ready().then(() => {
    GoogleAuth.initialize()
  })
}

在函数中签名

async googleSignIn() {
  let googleUser = await GoogleAuth.signIn();

  /*
    If you use Firebase you can forward and use the logged in Google user like this:
  */
  const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
  return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}

第3组

<script setup lang="ts">
import { defineComponent, onMounted } from 'vue';
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

onMounted(() => {
  GoogleAuth.initialize();
});

async function logIn() {
  const response = await GoogleAuth.signIn();
  console.log(response);
}
</script>

或看见更多 GoogleAuth-Vue3-实例

iOS系统

  1. 在 Google 云端控制台创建证书 IOS 客户端标识 获得 客户身份 财务报告和审定财务报表 iOS URL 计划

  2. 添加 标识符 REVERSED_CLIENT_ID 作为 URL 计划 改为 Info.plist 从 iOS URL 计划
    (X代码:App - 目标/App - 信息 - URL) 类型,单击加图标)

  3. 设定 客户身份 其中一个方式:

    1. 设定为 capacitor.config.json
      • iosClientId iOS 的特定密钥
      • clientId - 或Android和iOS的常见密钥
    2. 下载 GoogleService-Info.plist 文件为 CLIENT_ID 复制到 欧斯/阿普/阿普 必须通过Xcode进行索引.

插件首先使用 iosClientId 如果没有找到用途 clientId 如果没有找到使用值 CLIENT_ID 从文件 GoogleService-Info.plist

机器人

设定 客户身份 数字 :

  1. 单位 capacitor.config.json

    • androidClientId - Android的特定密钥
    • clientId - 或Android和iOS的常见密钥
  2. 或设置在您的内部 strings.xml

插件首先使用 androidClientId 如果没有找到用途 clientId 如果没有找到使用值 server_client_id 从文件 strings.xml

<resources>
  <string name="server_client_id">Your Web Client Key</string>
</resources>

刷新方法

当应用程序初始化以确认用户是否正在登录时,应该使用这种方法 。 如果为真, 方法将返回一个访问Token, idToken和一个空的刷新Token .

checkLoggedIn() {
    GoogleAuth.refresh()
        .then((data) => {
            if (data.accessToken) {
                this.currentTokens = data;
            }
        })
        .catch((error) => {
            if (error.type === 'userLoggedOut') {
                this.signin()
            }
        });
}

配置

名称类型说明
客户端ID字符串该应用程序的客户端ID,在Google开发者控制台中找到并创建.
iosclientId 坐标字符串IOS 的特定客户端 ID 密钥
机器人客户端字符串Android 的特定客户端ID密钥
范围字符串 []您可能需要请求访问 Google API 的范围
https://developers.google.com/identity/protocols/oauth2/scopes 互联网档案馆的存檔,存档日期2014-12-2
服务器客户端Id字符串此客户端ID 用于离线访问和服务器侧处理
强制代码FreshToken布尔强制用户选择电子邮件地址以重生 AuthCode
用于获得有效的刷新工具(在iOS和Android上的工作)

在根中提供配置 capacitor.config.json

{
  "plugins": {
    "GoogleAuth": {
      "scopes": ["profile", "email"],
      "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
      "forceCodeForRefreshToken": true
    }
  }
}

或进入 capacitor.config.ts

/// <reference types="'@codetrix-studio/capacitor-google-auth'" />

const config: CapacitorConfig = {
  plugins: {
    GoogleAuth: {
      scopes: ['profile', 'email'],
      serverClientId: 'xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
      forceCodeForRefreshToken: true,
    },
  },
};

export default config;

API 密码

初始化(...)

initialize(options?: InitOptions) => void

初始化了GoogleAuthPlugin,加载了chaffi库并设置了插件.

参数类型说明
optionsInitOptions- 可选初始化选项.

自: 3.1.0 (中文(简体) )


签名IN ()

signIn() => Promise<User>

启动登录进程,并返回以用户信息解决的保证.

回返情况: Promise<User>


刷新( )

refresh() => Promise<Authentication>

刷新认证符并返回以更新认证细节解决的保证 .

回返情况: Promise<Authentication>


符号输出( C)

signOut() => Promise<any>

签署用户并返回承诺.

回返情况: Promise<any>


接口

输入选项

Prop类型说明默认自兹
clientIdstring该应用程序的客户端ID,在Google开发者控制台中找到并创建. 常见于Android或iOS. 默认在配置中被定义.3.1.0 (中文(简体) )
scopesstring[]指定访问 Google API 所需的范围 默认在配置中被定义.
grantOfflineAccessboolean设定当用户不在浏览器时您的应用程序需要刷新访问符 。 反应使用 serverAuthCode 键false3.1.0 (中文(简体) )

用户

Prop类型说明
idstring用户独有的标识符 .
emailstring与用户相关的电子邮件地址 .
namestring用户全名.
familyNamestring用户的姓氏(上名).
givenNamestring用户的给定名称(名).
imageUrlstring用户配置图片的 URL.
serverAuthCodestring服务器认证代码.
authenticationAuthentication认证细节,包括访问、刷新和身份标志.

认证

Prop类型说明
accessTokenstring在认证过程中获得的访问令牌 .
idTokenstring在认证过程中获得的ID 令牌 .
refreshTokenstring刷新标志.

移徙指南

从3.3.x移到3.4.x

安装3.4.x版本 :

npm i --save @codetrix-studio/capacitor-google-auth@^3.4

执行您的工程指令 更新到电容 6。 。 。 .

从3.2.x移至3.3.x

安装3.3.x版本 :

npm i --save @codetrix-studio/capacitor-google-auth^3.3

执行您的工程指令 从电容4更新到电容5。 。 。 .

从3.2.1移至3.2.2

页:1 Android 在文件中 MainActivity.onCreate

- this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
-   add(GoogleAuth.class);
- }});
+ this.registerPlugin(GoogleAuth.class);

从 3.1.x 移到 3.2.x

安装3.2.x版本 :

npm i --save @codetrix-studio/capacitor-google-auth^3.2

执行您的工程指令 从电容 3 更新到电容 4。 。 。 .

从 3. 0.2 移到 3.1.0

- GoogleAuth.init()
+ GoogleAuth.initialize()

从2个移到3个

安装3.x.x版本 :

npm i --save @codetrix-studio/capacitor-google-auth^3.0

之后 迁移到 Capcitor 3 更新项目,参见 diff :

韦伯
- import "@codetrix-studio/capacitor-google-auth";
- import { Plugins } from '@capacitor/core';
+ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth'

- Plugins.GoogleAuth.signIn();
+ GoogleAuth.init()
+ GoogleAuth.signIn()

从1移到2

安装 2. x. x 版本 :

npm i --save @codetrix-studio/capacitor-google-auth@2

2.x.x 用于电容器 指令

许可证

麻省理工学院

版本与发布记录

@codetrix-studio/capacitor-google-auth 共发布 44 个版本,首次发布于 2019/05/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.4.0-rc.4latest2024/05/012 年前
3.4.0-rc.32024/04/302 年前
3.4.0-rc.12024/04/222 年前
3.4.0-rc.02024/02/192 年前
3.3.62023/11/272 年前
3.3.52023/11/182 年前

关于 @codetrix-studio/capacitor-google-auth 的常见问题

@codetrix-studio/capacitor-google-auth 是什么?

电容器的Google Auth插件.

如何安装 @codetrix-studio/capacitor-google-auth?

在项目目录运行 npm install @codetrix-studio/capacitor-google-auth,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(^6.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codetrix-studio/capacitor-google-auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codetrix-studio/capacitor-google-auth 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codetrix-studio/capacitor-google-auth 可以商用吗?

@codetrix-studio/capacitor-google-auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codetrix-studio/capacitor-google-auth 的下载量有多少?

根据 npm 官方下载统计,@codetrix-studio/capacitor-google-auth 在 2026/10/10 – 2026/11/08 这 30 天内被下载 223,599 次,日均约 7,453 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codetrix-studio/capacitor-google-auth 还在维护吗?

最新版本 3.4.0-rc.4 发布于 2024/05/01(2 年前);至今共发布 44 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。