警告
此插件现在处于低维护模式,对于凭证管理器或隐私清单等新功能,请使用:https://github.com/cap-go/capacitor-social-login
@codetrix-studio/capacitor-google-auth
电容器6
Google Auth的电容器插件。
在V6版本中,初始化方法中的clientId用于优先级,而不是您可以设置的其他地方。如果您仅在网络上使用它之前,请在移动设备上取消设置。或将其设置为复制旧行为。
欢迎PRS和非常感谢,可以将此插件与电容器和官方Google Auth Platform Library Library功能均等保持最新。
尝试遵循良好的代码实践。您甚至可以帮助保持随附的演示更新。
不建议使用与官方的Google Auth库的功能的PRS。
(我们在这里对初学者友好)
npm i --save @codetrix-studio/capacitor-google-auth
# pnpm
pnpm add @codetrix-studio/capacitor-google-auth
# yarn
yarn add @codetrix-studio/capacitor-google-authnpx cap update如果需要迁移到不同的电容器版本,请参见迁移插件到新版本的说明。
注册插件并手动初始化
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 Developers Console中找到并创建。scopes - 与配置范围相同grantOfflineAccess - 布尔值,默认为false ,如果您的应用程序需要在浏览器中不存在时刷新访问令牌,请设置。使用它
GoogleAuth . signIn ( ) ; init Hook
// app.component.ts
constructor ( ) {
this . initializeApp ( ) ;
}
initializeApp ( ) {
this . platform . ready ( ) . then ( ( ) => {
GoogleAuth . initialize ( )
} )
}登录功能
import { GoogleAuth } from "@codetrix-studio/capacitor-google-auth" ;
import { Auth , GoogleAuthProvider , signInWithCredential } from '@angular/fire/auth' ;
async googleSignIn ( ) {
let googleUser = await GoogleAuth . signIn ( ) ;
/*
If you use Firebase you can forward and use the logged in Google user like this:
*/
constructor ( private auth : Auth ) { }
const googleUser = await GoogleAuth . signIn ( ) ;
const _credential = GoogleAuthProvider . credential ( googleUser . authentication . idToken ) ;
return signInWithCredential ( this . auth , _credential ) ;
} < 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 >或查看更多capicitorgoogleauth-vue3示例
在Google Cloud Cond Console凭证客户ID中为iOS创建并获取客户端ID和iOS URL方案
将标识符REVERSED_CLIENT_ID添加到url Info.plist中
(Xcode:App -Targets/App -Info- URL类型,单击Plus图标)
设置客户ID的方式之一(按插件中的重要性顺序):
clientIdiosClientId设置在capacitor.config.json中capacitor.config.json中设置clientIdGoogleService-Info.plist中设置CLIENT_ID设置客户端ID (按插件中的重要性顺序):
clientIdcapacitor.config.json中设置androidClientIdcapacitor.config.json中设置clientIdstrings.xml中设置server_client_id < resources >
< string name = " server_client_id " >Your Web Client Key</ string >
</ resources >更改Play Services Auth版本(可选):
此插件使用com.google.android.gms:play-services-auth:21.2.0默认情况下,您可以覆盖它在variables.gradle上提供gmsPlayServicesAuthVersion 。
刷新方法
初始化应用程序以确定用户是否当前登录时,应调用此方法。如果为true,该方法将返回访问,iDtoken和一个空的刷新。
checkLoggedIn ( ) {
GoogleAuth . refresh ( )
. then ( ( data ) => {
if ( data . accessToken ) {
this . currentTokens = data ;
}
} )
. catch ( ( error ) => {
if ( error . type === 'userLoggedOut' ) {
this . signin ( )
}
} ) ;
} | 姓名 | 类型 | 描述 |
|---|---|---|
| 客户端 | 细绳 | 该应用程序的客户ID,在Google开发人员控制台中找到并创建。 |
| iosclientid | 细绳 | iOS的特定客户ID密钥 |
| AndroidClientID | 细绳 | Android的特定客户ID密钥 |
| 范围 | 细绳[] | 您可能需要请求访问Google API的范围 https://developers.google.com/indesity/protocols/oauth2/scopes |
| ServerClientID | 细绳 | 此客户端用于离线访问和服务器端处理 |
| forcecodeforrefreshtoken | 布尔 | 迫使用户选择电子邮件地址以再生验证码 用于获得有效的刷新(在iOS和Android上工作) |
在root 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 ; initialize函数下配置范围。 initialize(...)signIn()refresh()signOut() initialize ( options ?: InitOptions ) = > void初始化GoogleAuthPlugin,加载GAPI库并设置插件。
| 参数 | 类型 | 描述 |
|---|---|---|
options | InitOptions | - 可选的初始化选项。 |
自从: 3.1.0
signIn ( ) = > Promise < User >启动登录过程,并返回通过用户信息解决的承诺。
返回: Promise<User>
refresh ( ) = > Promise < Authentication >刷新身份验证令牌,并返回一个诺言,可以通过更新的身份验证详细信息解决。
返回: Promise<Authentication>
signOut ( ) = > Promise < any >签署用户并返回承诺。
返回: Promise<any>
| 支柱 | 类型 | 描述 | 默认 | 自从 |
|---|---|---|---|---|
clientId | string | 该应用程序的客户ID,在Google开发人员控制台中找到并创建。 Android或iOS常见。默认值在配置中定义。 | 3.1.0 | |
scopes | string[] | 指定访问Google API所需的范围,默认值在配置中定义。 | 3.4.0-RC.4 | |
grantOfflineAccess | boolean | 设置如果您的应用程序需要在浏览器中不存在的用户时需要刷新访问令牌。在响应中使用serverAuthCode键 | false | 3.1.0 |
| 支柱 | 类型 | 描述 |
|---|---|---|
id | string | 用户的唯一标识符。 |
email | string | 与用户关联的电子邮件地址。 |
name | string | 用户的全名。 |
familyName | string | 用户的姓氏(姓氏)。 |
givenName | string | 用户的给定名称(名字)。 |
imageUrl | string | 用户个人资料图片的URL。 |
serverAuthCode | string | 服务器身份验证代码。 |
authentication | Authentication | 身份验证详细信息,包括访问,刷新和ID令牌。 |
| 支柱 | 类型 | 描述 |
|---|---|---|
accessToken | string | 在身份验证期间获得的访问令牌。 |
idToken | string | 身份验证期间获得的ID令牌。 |
refreshToken | string | 刷新令牌。 |
安装版本3.4.x:
npm i --save @codetrix-studio/capacitor-google-auth@^3.4按照您的指示为您的项目更新到电容器6。
安装版本3.3.x:
npm i --save @codetrix-studio/capacitor-google-auth^3.3按照指令为您的项目更新从电容器4到电容器5。
用于文件MainActivity.onCreate中的Android 。
- this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
- add(GoogleAuth.class);
- }});
+ this.registerPlugin(GoogleAuth.class); 安装版本3.2.x:
npm i --save @codetrix-studio/capacitor-google-auth^3.2请按照说明为您的项目更新从电容器3到电容器4。
- GoogleAuth.init()
+ GoogleAuth.initialize() 安装版本3.xx:
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() 安装版本2.xx:
npm i --save @codetrix-studio/capacitor-google-auth@2用于电容器2.xx使用指令
麻省理工学院