8.7 版本发布—WinterCG 兼容性第一部分
了解更多

在 GitHub 上查看

@nativescript/google-signin

内容

简介

一个允许您使用 Google 登录认证用户的插件。

安装

在项目根目录下运行以下命令安装插件。

CLI
npm install @nativescript/google-signin

Android 前提条件

  1. 要将 GoogleSignIn 用于 Android,请访问 将 Firebase 添加到您的 Android 项目 并按照以下 2 个步骤操作。

除非您正在使用其他需要它的 Google 服务,否则您无需在应用中包含 添加 Firebase 配置文件 中提到的 google-services.json 文件。

  1. 生成调试 SHA-1 指纹并将其添加到您的 Firebase 项目中。

为机器上的调试密钥库生成 SHA-1 指纹,并将其添加到应用的 Firebase 项目中。 否则,将导致 错误:10 错误。

使用以下命令为调试密钥库生成 SHA-1 指纹。 对于调试密钥库,密码为 android

  • macOS/Linux
CLI
keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
  • Windows
CLI
keytool -list -v \
-alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
  1. 使用 Google Cloud Platform API 管理器 启用您需要的 OAuth API。 确保您已在控制台中为 OAuth 同意屏幕 填写了所有必需的字段。 否则,您可能会遇到 APIException 错误。

iOS 前提条件

  1. 此插件需要 iOS 9.0+

  2. 要将 GoogleSignIn 用于 iOS,请使用以下步骤在 将 Firebase 添加到您的 Apple 项目 中设置应用。

    1. 创建一个 Firebase 项目
    2. 在 Firebase 中注册您的应用
    3. 添加 Firebase 配置文件GoogleService-Info.plist.App_Resources/iOS/
    4. 进入 Google Cloud 并选择您从 Firebase 创建的项目。 访问 API 和服务 -> 凭据部分并添加一个类型为 OAuth 客户端 ID 的新凭据。 在应用程序类型中选择 iOS。 返回到凭据屏幕并在 OAuth 2.0 客户端 ID 部分输入您的新密钥。 复制 客户端 IDiOS URL 方案 以便下一步使用。
    5. 将以下 CFBundleURLTypesGIDClientID 属性添加到 App_Resources/iOS/Info.plist 文件中。
xml
<!-- Google Sign-in Section -->
<key>CFBundleURLTypes</key>
<array>
	<dict>
		<key>CFBundleTypeRole</key>
		<string>Editor</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<!-- TODO Replace this value: -->
			<!-- Paste your iOS URL Scheme here -->
			<string>com.googleusercontent.apps.292142294722-23nmrq9mn8rhpqipjc1bt4qecga3qgsf</string>
		</array>
	</dict>
</array>
<key>GIDClientID</key>
<!-- Paste your Client ID here -->
<string>749673967192-c24phj29u2otpict36e71ocjo2g5i3hs.apps.googleusercontent.com</string>
<!-- End of the Google Sign-in Section -->

注意

根据 https://developer.apple.com/sign-in-with-apple/get-started,从 2020 年 6 月 30 日 开始,使用登录服务的应用在提交到 Apple App Store 时还必须提供 使用 Apple 登录 选项。 为此,请考虑使用 @nativescript/apple-sign-in 插件。

使用 @nativescript/google-signin

登录

要使用 GoogleSignIn 登录用户,请按照以下步骤操作。

  1. 注册并将 GoogleSignInButton 添加到您的标记中,以便能够启动 GoogleSigIn。
  • 核心
xml
<Page xmlns:ui="@nativescript/google-signin">
	<ui:GoogleSigninButton tap="handleSignIn" colorScheme='auto' colorStyle='standard'/>
</Page>
  • Angular

按如下方式注册按钮

ts
import { registerElement } from '@nativescript/angular'
registerElement(
  'GoogleSignInButton',
  () => require('@nativescript/google-signin').GoogleSignInButton
)

接下来,将其添加到您的 html 文件中,并设置 colorSchemecolorStyle 的所需选项,如下所示

xml
<GoogleSignInButton colorScheme='auto' colorStyle='standard' (tap)="yourGoogleSigninFunction()"></GoogleSignInButton>

Vue

要注册按钮,请将以下代码添加到 main.ts 文件中。

ts
registerElement(
  'GoogleSignInButton',
  () => require('@nativescript/google-signin').GoogleSignInButton
)

然后在 .vue 文件中按如下方式使用它

xml
<GoogleSignInButton colorScheme='auto' colorStyle='standard' @tap="yourGoogleSigninFunction"></GoogleSignInButton>
  1. 在 GoogleSignInButton 点击时调用 signIn() 方法

在调用 signIn()signInSilently() 方法登录用户之前,请调用 configure() 初始化 Firebase。 如果您不想设置任何配置选项,可以将空对象 {} 作为参数传递给 configure

ts
import { GoogleSignin } from '@nativescript/google-signin'

try {
  await GoogleSignin.configure({})
  const user = await GoogleSignin.signIn()
} catch (e) {}

设置 GoogleSignInButton 的样式

GoogleSignInButtonView 实例,因此您可以使用 View 类样式属性。 此外,插件还提供了 colorSchemecolorStyle 属性。

API

GoogleSignIn

提供 GoogleSignIn API 的类。 它具有以下方法

configure()

ts
await GoogleSignIn.configure(configuration)

指定登录请求的属性,例如范围、帐户名称等。

在调用 signIn()signInSilently() 方法之前调用此方法。 在应用启动后仅调用一次。

configuration 参数是一个可选的 Configuration 对象。

配置

Configuration 对象具有以下属性。 所有属性都是可选的。

属性类型
scopesstring[]
signInOptions'default' | 'games'
clientIdstring
serverClientIdstring
accountNamestring
hostedDomainstring

signIn()

ts
user: User = await GoogleSignin.signIn()

提示一个模态窗口,让用户登录到应用程序。


signInSilently()

ts
user: User = await GoogleSignin.signInSilently()

isSignedIn()

ts
isUserSignedIn: boolean = GoogleSignin.isSignedIn()

检查用户当前是否已登录。


playServicesAvailable()

ts
isPlayServicesAvailable: boolean = await GoogleSignin.playServicesAvailable()

检查设备是否已安装 Google Play 服务。 在 iOS 上始终解析为 true

显示登录模态窗口需要最新的 Google Play 服务。


signOut()

ts
GoogleSignin.signOut()

注销当前用户。


disconnect()

ts
await GoogleSignin.disconnect()

断开当前用户的连接。


getTokens()

ts
tokens: { idToken: string; accessToken: string;} = await GoogleSignin.getTokens()

解析包含 { idToken: string, accessToken: string, } 的对象或拒绝错误。 请注意,使用 accessToken 在后端服务器上进行身份断言是不建议的。


getCurrentUser()

ts
user : User | null = GoogleSignin.getCurrentUser()

此方法解析为 null 或 User 对象。

用户

用户对象具有以下成员

属性类型描述
idstring只读
displayNamestring只读
emailstring只读
givenNamestring只读
familyNamestring只读
idTokenstring只读
accessTokenstring只读
grantedScopesstring[]只读
photoUrlstring只读
serverAuthCodestring只读
requestScopes(scopes: string[])Promise<IUser>有关 IUser 的属性,请参阅 User
nativecom.google.android.gms.auth.api.signin.GoogleSignInAccount | GIDGoogleUser平台特定实例。

GoogleSignInButton

属性类型
colorScheme"dark" | "light" | "auto"
colorStyle"standard" | "wide" | "icon"

许可证

Apache 许可证 2.0 版