警告
このプラグインは、資格情報マネージャーやプライバシーマニフェストなどの新機能の場合は、https://github.com/cap-go/capacitor-social-loginを使用してください。
@codetrix-studio/capacitor-google-auth
コンデンサ6
Google Authのコンデンサプラグイン。
V6バージョンでは、初期化方法のclientId 、セットアップできる他の場所よりも優先されて使用されます。これをWebでのみ使用する前に、モバイルで設定してください。または、古い動作を複製するために条件付きで設定します。
このプラグインをコンデンサと公式のGoogle Auth Platform Library機能のパリティで最新の状態に保つPRを大歓迎します。
良いコードプラクティスに従ってください。付属のデモを更新するのを支援することもできます。
Google Auth Libraryの公式に整合していない機能の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 -Google Developers Consoleで見つけて作成されたアプリのクライアントID。scopes - スコープの構成と同じgrantOfflineAccess - ブール、デフォルトのfalse 、アプリケーションがブラウザに存在しないときにアプリケーションがアクセストークンを更新する必要がある場合は設定します。使用してください
GoogleAuth . signIn ( ) ; initフック
// 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 >または、より多くのCabeGoOgLeauth-Vue3-Exampleを参照してください
iOS用のGoogle Cloud Console資格認定IDを作成し、クライアントIDとiOS URLスキームを取得する
iOS URLスキームからInfo.plistにurlスキームとしてREVERSED_CLIENT_IDをroversed_client_idを追加する
(xcode:app-ターゲット/アプリ - 情報 - urlタイプ、クリックプラスアイコン)
クライアントIDを1つの方法の1つを設定します(プラグインで重要な順序によって):
clientId設定しますcapacitor.config.jsonにiosClientId設定しますcapacitor.config.jsonでclientIdを設定しますGoogleService-Info.plistでCLIENT_ID設定しますクライアントIDを設定します(プラグインで重要な順序によって):
clientId設定しますcapacitor.config.jsonのandroidClientId設定しますcapacitor.config.jsonでclientIdを設定しますstrings.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 gmsPlayServicesAuthVersion使用しますvariables.gradle
更新方法
このメソッドは、ユーザーが現在ログインしているかどうかを確立するためにアプリが初期化されたときに呼び出されます。真の場合、メソッドはAccessToken、Idtoken、および空の爽快なものを返します。
checkLoggedIn ( ) {
GoogleAuth . refresh ( )
. then ( ( data ) => {
if ( data . accessToken ) {
this . currentTokens = data ;
}
} )
. catch ( ( error ) => {
if ( error . type === 'userLoggedOut' ) {
this . signin ( )
}
} ) ;
} | 名前 | タイプ | 説明 |
|---|---|---|
| clientid | 弦 | Google Developers Consoleで見つけて作成されたアプリのクライアントID。 |
| iosclientid | 弦 | iOSの特定のクライアントIDキー |
| AndroidClientID | 弦 | Androidの特定のクライアントIDキー |
| スコープ | 弦[] | Google APIにアクセスするためにリクエストする必要があるかもしれないスコープ https://developers.google.com/identity/protocols/oauth2/scopes |
| serverClientID | 弦 | オフラインアクセスとサーバーサイドの取り扱いに使用されるこのclientID |
| forcodeforrefreshtoken | ブール | ユーザーに電子メールアドレスを選択して、AuthCodeを再生成します 有効なRefreshtoken(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 ) = > voidGoogleAuthPluginを初期化し、GAPIライブラリをロードしてプラグインを設定します。
| パラメーション | タイプ | 説明 |
|---|---|---|
options | InitOptions | - オプションの初期化オプション。 |
以来: 3.1.0
signIn ( ) = > Promise < User >サインインプロセスを開始し、ユーザー情報で解決する約束を返します。
返品: Promise<User>
refresh ( ) = > Promise < Authentication >認証トークンを更新し、更新された認証の詳細で解決する約束を返します。
返品: Promise<Authentication>
signOut ( ) = > Promise < any >ユーザーにサインアウトし、約束を返します。
返品: Promise<any>
| 小道具 | タイプ | 説明 | デフォルト | 以来 |
|---|---|---|---|---|
clientId | string | Google Developers Consoleで見つけて作成されたアプリのクライアントID。 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.4Project Conapacitor 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.0Capcitor 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の場合、命令を使用します
mit