@achernykh/capacitor-google-auth 中文文档教程

发布于 3年前 浏览 33 项目主页 更新于 3年前

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.plistCLIENT_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)

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文