경고
이 플러그인은 이제 자격 증명 관리자 또는 개인 정보와 같은 새로운 기능을 위해 낮은 유지 관리 모드에 있습니다.
@codetrix-studio/capacitor-google-auth
커패시터 6
Google Auth 용 커패시터 플러그인.
V6 버전에서는 초기화 메소드의 clientId 설정할 수있는 다른 장소보다 우선 순위로 사용됩니다. 웹에서만 사용하기 전에 모바일에서 해제하지 않으십시오. 또는 구식 동작을 복제하도록 조건부로 설정하십시오.
Capacitor 및 공식 Google Auth Platform Library 기능 패리티와 함께이 플러그인을 최신 상태로 유지하는 PR은 환영하며 감사합니다.
좋은 코드 관행을 따르십시오. 포함 된 데모를 업데이트하는 데 도움이 될 수도 있습니다.
공식 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 ( ) ; 이른 후크
// 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 >또는 더 많은 Capacitorgoogleauth-vue3- 예를보십시오
iOS 용 Google Cloud Console 자격 증명 클라이언트 ID 에서 작성하고 클라이언트 ID 및 iOS URL 체계를 가져옵니다.
Info.plist 의 url schemes 로 식별자 REVERSED_CLIENT_ID 추가
(Xcode : 앱 - 대상/앱 - 정보 - URL 유형, 클릭 플러스 아이콘)
클라이언트 ID 설정 방법 중 하나 (플러그인에서 중요한 순서로) :
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 기본적으로 사용하면 variables.gradle 에서 gmsPlayServicesAuthVersion 제공하는 것을 재정의 할 수 있습니다.
새로 고침 방법
이 메소드는 앱을 초기화 할 때 사용자가 현재 로그인되어 있는지 설정하기 위해 호출되어야합니다. true 인 경우 메소드는 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 | 끈 | 오프라인 액세스 및 서버 측 처리에 사용되는이 클라이언트 |
| forcecodeforrefreshtoken | 부울 | 사용자가 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 ; 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. 안드로이드 또는 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.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