插件
Google 登录
@nativescript/google-signin
内容
简介
一个允许您使用 Google 登录认证用户的插件。
安装
在项目根目录下运行以下命令安装插件。
npm install @nativescript/google-signin
Android 前提条件
- 要将 GoogleSignIn 用于 Android,请访问 将 Firebase 添加到您的 Android 项目 并按照以下 2 个步骤操作。
除非您正在使用其他需要它的 Google 服务,否则您无需在应用中包含 添加 Firebase 配置文件 中提到的 google-services.json
文件。
- 生成调试 SHA-1 指纹并将其添加到您的 Firebase 项目中。
为机器上的调试密钥库生成 SHA-1 指纹,并将其添加到应用的 Firebase 项目中。 否则,将导致 错误:10
错误。
使用以下命令为调试密钥库生成 SHA-1 指纹。 对于调试密钥库,密码为 android
。
- macOS/Linux
keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
- Windows
keytool -list -v \
-alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
- 使用 Google Cloud Platform API 管理器 启用您需要的 OAuth API。 确保您已在控制台中为 OAuth 同意屏幕 填写了所有必需的字段。 否则,您可能会遇到 APIException 错误。
iOS 前提条件
此插件需要
iOS 9.0+
。要将 GoogleSignIn 用于 iOS,请使用以下步骤在 将 Firebase 添加到您的 Apple 项目 中设置应用。
- 创建一个 Firebase 项目
- 在 Firebase 中注册您的应用
- 添加 Firebase 配置文件,
GoogleService-Info.plist.
到App_Resources/iOS/
。 - 进入 Google Cloud 并选择您从 Firebase 创建的项目。 访问 API 和服务 -> 凭据部分并添加一个类型为
OAuth 客户端 ID
的新凭据。 在应用程序类型中选择 iOS。 返回到凭据屏幕并在OAuth 2.0 客户端 ID
部分输入您的新密钥。 复制客户端 ID
和iOS URL 方案
以便下一步使用。 - 将以下
CFBundleURLTypes
和GIDClientID
属性添加到App_Resources/iOS/Info.plist
文件中。
<!-- 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 登录用户,请按照以下步骤操作。
- 注册并将 GoogleSignInButton 添加到您的标记中,以便能够启动 GoogleSigIn。
- 核心
<Page xmlns:ui="@nativescript/google-signin">
<ui:GoogleSigninButton tap="handleSignIn" colorScheme='auto' colorStyle='standard'/>
</Page>
- Angular
按如下方式注册按钮
import { registerElement } from '@nativescript/angular'
registerElement(
'GoogleSignInButton',
() => require('@nativescript/google-signin').GoogleSignInButton
)
接下来,将其添加到您的 html
文件中,并设置 colorScheme
和 colorStyle
的所需选项,如下所示
<GoogleSignInButton colorScheme='auto' colorStyle='standard' (tap)="yourGoogleSigninFunction()"></GoogleSignInButton>
Vue
要注册按钮,请将以下代码添加到 main.ts
文件中。
registerElement(
'GoogleSignInButton',
() => require('@nativescript/google-signin').GoogleSignInButton
)
然后在 .vue
文件中按如下方式使用它
<GoogleSignInButton colorScheme='auto' colorStyle='standard' @tap="yourGoogleSigninFunction"></GoogleSignInButton>
- 在 GoogleSignInButton 点击时调用 signIn() 方法
在调用 signIn() 或 signInSilently() 方法登录用户之前,请调用 configure()
初始化 Firebase。 如果您不想设置任何配置选项,可以将空对象 {}
作为参数传递给 configure。
import { GoogleSignin } from '@nativescript/google-signin'
try {
await GoogleSignin.configure({})
const user = await GoogleSignin.signIn()
} catch (e) {}
设置 GoogleSignInButton 的样式
GoogleSignInButton 是 View 实例,因此您可以使用 View 类样式属性。 此外,插件还提供了 colorScheme
和 colorStyle
属性。
API
GoogleSignIn
提供 GoogleSignIn API 的类。 它具有以下方法
configure()
await GoogleSignIn.configure(configuration)
指定登录请求的属性,例如范围、帐户名称等。
在调用 signIn()
或 signInSilently()
方法之前调用此方法。 在应用启动后仅调用一次。
configuration
参数是一个可选的 Configuration 对象。
配置
Configuration 对象具有以下属性。 所有属性都是可选的。
属性 | 类型 |
---|---|
scopes | string[] |
signInOptions | 'default' | 'games' |
clientId | string |
serverClientId | string |
accountName | string |
hostedDomain | string |
signIn()
user: User = await GoogleSignin.signIn()
提示一个模态窗口,让用户登录到应用程序。
signInSilently()
user: User = await GoogleSignin.signInSilently()
isSignedIn()
isUserSignedIn: boolean = GoogleSignin.isSignedIn()
检查用户当前是否已登录。
playServicesAvailable()
isPlayServicesAvailable: boolean = await GoogleSignin.playServicesAvailable()
检查设备是否已安装 Google Play 服务。 在 iOS 上始终解析为 true
。
显示登录模态窗口需要最新的 Google Play 服务。
signOut()
GoogleSignin.signOut()
注销当前用户。
disconnect()
await GoogleSignin.disconnect()
断开当前用户的连接。
getTokens()
tokens: { idToken: string; accessToken: string;} = await GoogleSignin.getTokens()
解析包含 { idToken: string, accessToken: string, } 的对象或拒绝错误。 请注意,使用 accessToken
在后端服务器上进行身份断言是不建议的。
getCurrentUser()
user : User | null = GoogleSignin.getCurrentUser()
此方法解析为 null 或 User 对象。
用户
用户对象具有以下成员
属性 | 类型 | 描述 |
---|---|---|
id | string | 只读 |
displayName | string | 只读 |
email | string | 只读 |
givenName | string | 只读 |
familyName | string | 只读 |
idToken | string | 只读 |
accessToken | string | 只读 |
grantedScopes | string[] | 只读 |
photoUrl | string | 只读 |
serverAuthCode | string | 只读 |
requestScopes(scopes: string[]) | Promise<IUser> | 有关 IUser 的属性,请参阅 User |
native | com.google.android.gms.auth.api.signin.GoogleSignInAccount | GIDGoogleUser | 平台特定实例。 |
GoogleSignInButton
属性 | 类型 |
---|---|
colorScheme | "dark" | "light" | "auto" |
colorStyle | "standard" | "wide" | "icon" |
许可证
Apache 许可证 2.0 版