Google Auth 电容控制器
@codetrix-studio/capacitor-google-auth
第6条
Google Auth 的电容插件.
捐款
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系统
-
在 Google 云端控制台创建证书 IOS 客户端标识 获得 客户身份 财务报告和审定财务报表 iOS URL 计划
-
添加 标识符
REVERSED_CLIENT_ID作为 URL 计划 改为Info.plist从 iOS URL 计划
(X代码:App - 目标/App - 信息 - URL) 类型,单击加图标) -
设定 客户身份 其中一个方式:
- 设定为
capacitor.config.jsoniosClientIdiOS 的特定密钥clientId- 或Android和iOS的常见密钥
- 下载
GoogleService-Info.plist文件为CLIENT_ID复制到 欧斯/阿普/阿普 必须通过Xcode进行索引.
- 设定为
插件首先使用 iosClientId 如果没有找到用途 clientId 如果没有找到使用值 CLIENT_ID 从文件 GoogleService-Info.plist
机器人
设定 客户身份 数字 :
-
单位
capacitor.config.jsonandroidClientId- Android的特定密钥clientId- 或Android和iOS的常见密钥
-
或设置在您的内部
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库并设置了插件.
| 参数 | 类型 | 说明 |
|---|---|---|
options | InitOptions | - 可选初始化选项. |
自: 3.1.0 (中文(简体) )
签名IN ()
signIn() => Promise<User>
启动登录进程,并返回以用户信息解决的保证.
回返情况: Promise<User>
刷新( )
refresh() => Promise<Authentication>
刷新认证符并返回以更新认证细节解决的保证 .
回返情况: Promise<Authentication>
符号输出( C)
signOut() => Promise<any>
签署用户并返回承诺.
回返情况: Promise<any>
接口
输入选项
| Prop | 类型 | 说明 | 默认 | 自兹 |
|---|---|---|---|---|
clientId | string | 该应用程序的客户端ID,在Google开发者控制台中找到并创建. 常见于Android或iOS. 默认在配置中被定义. | 3.1.0 (中文(简体) ) | |
scopes | string[] | 指定访问 Google API 所需的范围 默认在配置中被定义. | ||
grantOfflineAccess | boolean | 设定当用户不在浏览器时您的应用程序需要刷新访问符 。 反应使用 serverAuthCode 键 | false | 3.1.0 (中文(简体) ) |
用户
| Prop | 类型 | 说明 |
|---|---|---|
id | string | 用户独有的标识符 . |
email | string | 与用户相关的电子邮件地址 . |
name | string | 用户全名. |
familyName | string | 用户的姓氏(上名). |
givenName | string | 用户的给定名称(名). |
imageUrl | string | 用户配置图片的 URL. |
serverAuthCode | string | 服务器认证代码. |
authentication | Authentication | 认证细节,包括访问、刷新和身份标志. |
认证
| Prop | 类型 | 说明 |
|---|---|---|
accessToken | string | 在认证过程中获得的访问令牌 . |
idToken | string | 在认证过程中获得的ID 令牌 . |
refreshToken | string | 刷新标志. |
移徙指南
从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 用于电容器 指令