警告
此插件現在處於低維護模式,對於憑證管理器或隱私清單等新功能,請使用: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使用指令
麻省理工學院