@achernykh/capacitor-google-auth 中文文档教程
CapacitorGoogleAuth
Google Auth 的电容器插件。
Install
npm i --save @codetrix-studio/capacitor-google-auth
npx cap update
WEB
在标题中添加 clientId
元标记。
<meta name="google-signin-client_id" content="{your client id here}">
通过导入注册插件。
import "@codetrix-studio/capacitor-google-auth";
使用它
import { Plugins } from '@capacitor/core';
Plugins.GoogleAuth.signIn();
AngularFire2
async googleSignIn() {
let googleUser = await Plugins.GoogleAuth.signIn();
const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}
iOS
确保你有 GoogleService-Info.plist
和 CLIENT_ID
添加 REVERSED_CLIENT_ID
作为 url scheme 到 Info.plist
Android
在你的 strings.xml
<resources>
<string name="server_client_id">Your Web Client Key</string>
</resources>
中导入包在你的 MainActivity
import com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth;
中注册插件在你的 MainActivity.onCreate
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
add(GoogleAuth.class);
}});
Configure
中提供配置在根 capacitor.config.json
{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken" : true
}
}
}
注意:forceCodeForRefreshToken
强制用户选择电子邮件地址以重新生成用于获取有效刷新令牌的 AuthCode(适用于 iOS 和 Android)(用于离线访问和服务器端处理)
CapacitorGoogleAuth
Capacitor plugin for Google Auth.
Install
npm i --save @codetrix-studio/capacitor-google-auth
npx cap update
WEB
Add clientId
meta tag to head.
<meta name="google-signin-client_id" content="{your client id here}">
Register the plugin by importing it.
import "@codetrix-studio/capacitor-google-auth";
Use it
import { Plugins } from '@capacitor/core';
Plugins.GoogleAuth.signIn();
AngularFire2
async googleSignIn() {
let googleUser = await Plugins.GoogleAuth.signIn();
const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}
iOS
Make sure you have GoogleService-Info.plist
with CLIENT_ID
Add REVERSED_CLIENT_ID
as url scheme to Info.plist
Android
Inside your strings.xml
<resources>
<string name="server_client_id">Your Web Client Key</string>
</resources>
Import package inside your MainActivity
import com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth;
Register plugin inside your MainActivity.onCreate
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
add(GoogleAuth.class);
}});
Configure
Provide configuration in root capacitor.config.json
{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken" : true
}
}
}
Note : forceCodeForRefreshToken
force user to select email address to regenerate AuthCode used to get a valid refreshtoken (work on iOS and Android) (This is used for offline access and serverside handling)