@klippa/nativescript-login
klippa 编写 | v3.1.1
在 NativeScript 中实现社交登录的最佳方式,一个具有现代 SDK 的插件,允许使用各种提供程序进行身份验证,并访问所有 SDK 功能
npm i --save @klippa/nativescript-login

nativescript-login

NPM version Downloads TotalDownloads Build Status

:rocket: 在 NativeScript 中实现社交登录的最佳方式 :rocket

一个具有现代 SDK 的插件,允许使用各种提供程序进行身份验证,并访问所有 SDK 功能。

功能

NativeScript 版本支持

NS 版本 nativescript-login 版本 安装命令 文档
^8.0.0 ^3.0.0 ns plugin add @klippa/nativescript-login@^3.0.0 本页
^7.0.0 ^2.0.0 ns plugin add @klippa/nativescript-login@^2.0.0 这里
^6.0.0 ^1.0.0 tns plugin add @klippa/nativescript-login@^1.0.0 这里

安装(NS 8)

ns plugin add @klippa/nativescript-login@^3.0.0

用法

Facebook 登录

Android 集成

  • 遵循 手册 中的 1. 选择一个应用或创建一个新应用 步骤
  • 编辑/创建您的 App_Resources/Android/src/main/res/values/strings.xml 文件并添加以下内容,替换 {{app-id}}{{app-name}}{{client-token}} 的值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- If your strings.xml already existed, add the following <string> elements -->
<string name="app_name">{{app-name}}</string>
<string name="title_activity_kimera">{{app-name}}</string>
<string name="facebook_app_id">{{app-id}}</string>
<string name="fb_login_protocol_scheme">fb{{app-id}}</string>
<string name="facebook_client_token">{{client-token}}</string>
</resources>
  • 编辑您的 App_Resources/Android/src/main/AndroidManifest.xml 文件并在 <application> 元素内添加以下内容。
<meta-data android:name="com.facebook.sdk.ApplicationId" 
android:value="@string/facebook_app_id"/>


<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"

android:label="@string/app_name" />

<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
  • 遵循 手册 中的 6. 为您的应用提供开发版和发布版的密钥哈希 步骤
  • 现在使用您的 Facebook 账户登录应该可以正常工作了!SDK 会处理剩余的部分。

iOS 集成

  • 遵循 手册 中的 1. 选择一个应用或创建一个新应用 步骤
  • 3. 在 Facebook 上注册和配置您的应用 -> 3a. 添加您的 Bundle Identifier 步骤中输入您的 Bundle Identifier -> ** 打开 App_Resources/iOS/Info.plist 并添加以下内容,将 {{APP_ID}} 替换为您的应用 ID,将 {{CLIENT_TOKEN}} 替换为您的客户端令牌,将 {{APP_NAME}} 替换为您的应用名称
<key>CFBundleURLTypes</key>
<array>
<!-- If you already have a CFBundleURLTypes key, only add the dict section to the array -->
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb{{APP_ID}}</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>{{APP_ID}}</string>
<key>FacebookClientToken</key>
<string>{{CLIENT_TOKEN}}</string>
<key>FacebookDisplayName</key>
<string>{{APP_NAME}}</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- If you already have a LSApplicationQueriesSchemes key, only add the strings to the array -->
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

NativeScript 集成

仅限 iOS 需要

普通 NativeScript:编辑 app/app.ts

import { wireInFacebookLogin } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInFacebookLogin();

// ... Other code/wirings

app.run({ moduleName: "app-root" });

NativeScript Angular:编辑 src/main.ts


// Other imports.
import { wireInFacebookLogin } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInFacebookLogin();

// ... Other code/wirings

runNativeScriptAngularApp({
appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});

NativeScript Vue:编辑 src/main.ts


// Other imports.
import { wireInFacebookLogin } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInFacebookLogin();

// ... Other code/wirings

new Vue({
render: (h) => h('frame', [h(Home)]),
}).$start();

import { startFacebookLogin, FacebookLoginOptions } from "@klippa/nativescript-login";

// First create an options object:

// The most basic sign in options.
const loginOptions: FacebookLoginOptions = {};

// Please note that result can also be a failure result.
// The actual result is in the object.
startFacebookLogin(loginOptions).then((result) => {
console.log("Facebook login result: ", result);
}).catch((e) => {
console.log("Error while logging in to Facebook: ", e);
});

警告:Facebook 的自动记录事件

当您使用 Facebook SDK 时,您的应用中的一些事件会自动记录并收集到 Facebook 分析中,除非您禁用自动事件记录。您可以在 AndroidiOS 上通过进行一些小的配置更改来禁用它。如果您仅因为登录功能而使用 Facebook SDK,我建议禁用“自动记录事件”,以防止用户信息泄露到 Facebook(即使他们没有使用 Facebook)。

Google 登录

Android 集成

  • 遵循 手册 中的 配置 Google API Console 项目 步骤。
  • 如果您想请求服务器端代码以请求用户信息,请遵循 手册 中的 获取您的后端服务器的 OAuth 2.0 客户端 ID 步骤。
  • 使用您的Google账户登录现在应该可以正常工作了!SDK会处理其余部分。

iOS 集成

  • 按照手册中的“获取OAuth客户端ID”步骤操作,记下客户端ID并下载凭证文件。
  • 打开credentials.plist,复制位于<string></string>之间、在<key>REVERSED_CLIENT_ID</key>下面的值。
  • 打开App_Resources/iOS/Info.plist,添加以下内容,将{{REVERSED_CLIENT_ID}}替换为您复制的值
<key>CFBundleURLTypes</key>
<array>
<!-- If you already have a CFBundleURLTypes key, only add the dict section to the array -->
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>{{REVERSED_CLIENT_ID}}</string>
</array>
</dict>
</array>

NativeScript 集成

仅限 iOS 需要

正常原生Script

编辑app/app.ts

import { wireInGoogleSignIn } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInGoogleSignIn("{{CLIENT_ID}}");

// ... Other code/wirings

app.run({ moduleName: "app-root" });

原生Script Angular

编辑src/main.ts


// Other imports.
import { wireInGoogleSignIn } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInGoogleSignIn("{{CLIENT_ID}}");

// ... Other code/wirings

runNativeScriptAngularApp({
appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});

NativeScript Vue:编辑 src/main.ts


// Other imports.
import { wireInGoogleSignIn } from "@klippa/nativescript-login";

// ... Other code/wirings

wireInGoogleSignIn("{{CLIENT_ID}}");

// ... Other code/wirings

new Vue({
render: (h) => h('frame', [h(Home)]),
}).$start();

打开credentials.plist,复制位于<string></string>之间、在<key>CLIENT_ID</key>下面的值。将{{CLIENT_ID}}替换为您复制的值。


import { GoogleSignInOptions, GoogleSignInType, startGoogleSignIn } from "@klippa/nativescript-login";

// First create an options object:

// The most basic sign in options.
const signInOptions: GoogleSignInOptions = {
SignInType: GoogleSignInType.Local,
RequestEmail: true
};

// Please note that result can also be a failure result.
// The actual result is in the object.
startGoogleSignIn(signInOptions).then((result) => {
console.log("Google sign in result: ", result);
}).catch((e) => {
console.log("Error while singing in to Google: ", e);
});

使用Apple登录

  • 前往Apple开发者网站,并创建一个新的应用程序标识符,启用“使用Apple登录”功能。确保使用该应用程序标识符使用配置文件对应用程序进行签名。
  • 打开应用程序的App_Resources/iOS文件夹,并将以下内容添加(或附加)到名为app.entitlements的文件中。

Android集成(以及iOS < 13)

遗憾的是,“使用Apple登录”不支持Android,由于他们制作的JS版本,也无法在webview中创建版本。您将始终需要一个后端来处理它。我稍后会写一个教程。

iOS集成(iOS >= 13)

开始登录

import { SignInWithAppleOptions, startSignInWithApple, SignInWithAppleScope, signInWithAppleAvailable } from "@klippa/nativescript-login";
import { Dialogs } from "@nativescript/core";

if (signInWithAppleAvailable()) {
// First create an options object:
const signInOptions: SignInWithAppleOptions = {
Scopes: [SignInWithAppleScope.EMAIL, SignInWithAppleScope.FULLNAME]
};

// Please note that result can also be a failure result.
// The actual result is in the object.
startSignInWithApple(signInOptions).then((result) => {
console.log("Sign In with Apple result: ", result);
}).catch((e) => {
console.log("Error while using Sign In with Apple: ", e);
});
} else {
Dialogs.alert("Sign In with Apple is not available for your device");
}

获取当前状态

import { getSignInWithAppleState, signInWithAppleAvailable } from "@klippa/nativescript-login";
import { Dialogs } from "@nativescript/core";

if (signInWithAppleAvailable()) {
// User ID must be the ID that was previously received from the sign in.
const userID = "";

// Please note that result can also be a failure result.
// The actual result is in the object.
getSignInWithAppleState(userID).then((result) => {
console.log("Sign in with Apple State result: ", result);
}).catch((e) => {
console.log("Error getting Sign in with Apple State: ", e);
});
} else {
Dialogs.alert("Sign In with Apple is not available for your device");
}

其他类型的身份验证

为了保持此项目的简单性和整洁,以及保持依赖项较小,我们仅支持具有原生SDK的登录提供商。如果您想支持其他登录方式,请查看这些项目

API

Google

GoogleSignInOptions:

属性 描述
SignInType 登录类型。 GoogleSignInType.LOCAL用于使用设备上的信息,GoogleSignInType.ServerAuthCode用于在服务器端检索用户信息。
ServerClientId 请求ServerAuthCodeIdToken的服务器客户端ID。当登录类型为ServerAuthCode时,或当RequestIdToken为true时。
ForceCodeForRefreshToken 当类型为ServerAuthCode时使用。如果为true,则授予的代码可以交换为访问令牌和刷新令牌。第一次检索代码时,将自动授予刷新令牌。后续请求将需要额外的用户同意。默认使用false;仅当您的服务器遇到某些故障并丢失用户的刷新令牌时才使用true。仅在Android上有效。
HostedDomain 指定一个托管域限制。通过设置此选项,登录将限制为指定域中的用户账户。限制用户域(例如,“mycollege.edu”)
AccountName 指定应使用的设备上的账户名称。如果从未调用此选项,则客户端将使用此应用程序的当前默认账户。用于登录的设备上的账户名称。仅在Android上有效。
RequestIdToken 指定请求认证用户的ID令牌。请求ID令牌需要指定服务器客户端ID。iOS始终返回用户ID令牌。
RequestId 指定应用程序请求用户ID。对于iOS,您无法控制此选项,ID总是返回。
RequestEmail 指定您的应用程序请求电子邮件信息。请注意,我们不建议通过电子邮件地址来识别用户,因为电子邮件地址可能会更改。通过ID识别用户是更佳的方法。对于iOS,您无法控制此操作,如果您需要电子邮件,请使用RequestProfile。
RequestProfile 指定您的应用程序请求用户的个人资料信息。默认值:true。在iOS上,您必须设置RequestProfile或提供自定义作用域。
ExtraScopes 一个GoogleSignInScope值的列表,用于指定应用程序请求的OAuth 2.0作用域。通常您不需要此功能。
ForceAccountSelection 是否要强制选择账户。如果您启用此选项,我们将代表您在应用程序中注销用户。

GoogleSignInResult:

属性 描述
ResultType 结果,可以是GoogleSignInResultType.FAILEDGoogleSignInResultType.SUCCESS
ErrorCode 当结果类型为GoogleSignInResultType.FAILED时,请求的错误代码。
ErrorMessage 当结果类型为GoogleSignInResultType.FAILED时,请求的错误消息。
GrantedScopes 授权的作用域列表。
RequestedScopes 请求的作用域列表。此列表仅由Android SDK填充。
GivenName -
Id 用户的ID
IdToken 要发送到您后端的服务器ID令牌(JWT)
DisplayName -
FamilyName -
PhotoUrl -
Email -
ServerAuthCode 您的后端可以使用它来检索用户信息的服务器身份验证代码。

Facebook

FacebookLoginOptions:

属性 描述
Scopes 请求的权限。如果您不添加此参数,我们将为您请求public_profile和email。
RequestProfileData 是否请求个人资料数据。如果您不启用此选项,您将仅获得ID和令牌。非常适合服务器端处理。如果您启用此选项,我们将使用请求的令牌在Graph API上请求用户个人资料。当使用LimitedLogin时不可用。
ProfileDataFields 在请求个人资料数据时要获取的字段。如果没有设置,我们将获取以下字段:id,name,first_name,last_name,picture.type(large),email。某些字段可能会返回一个对象,如图片字段。当使用LimitedLogin时不可用。
ForceAccountSelection 是否要强制选择账户。如果您启用此选项,我们将代表您在应用程序中注销用户。
LimitedLogin 仅限iOS!是否要使用Limited Login。Facebook Login提供Limited Login模式。当您使用Facebook Login的有限版本时,不会使用人们使用该应用程序的Facebook Login事实来个性化或衡量广告效果。当您启用此选项时,您将不会获得访问令牌。

FacebookLoginResult:

属性 描述
ResultType 结果,可以是FacebookLoginResultType.FAILEDFacebookLoginResultType.CANCELEDFacebookLoginResultType.SUCCESS
ErrorCode 当结果类型为FacebookLoginResultType.FAILED时,请求的错误代码。
ErrorMessage 当结果类型为FacebookLoginResultType.FAILED时,请求的错误消息。
ProfileDataErrorCode 当结果类型为FacebookLoginResultType.FAILED且错误代码为2时,个人资料请求的错误代码。
ProfileDataErrorMessage 当结果类型为FacebookLoginResultType.FAILED且错误代码为2时,个人资料请求的错误消息。
ProfileDataUserErrorMessage 当结果类型为FacebookLoginResultType.FAILED且错误代码为2时,个人资料请求的用户错误消息。
DeniedScopes 拒绝的作用域列表,以验证用户是否授权所有请求的作用域。
GrantedScopes 授权的作用域列表。
Id 用户的ID
AccessToken 您的后端可以使用它来检索用户信息的访问令牌。当使用LimitedLogin时不可用。
ProfileDataFields 在iOS上使用LimitedLogin选项时,请求的FacebookLoginOptions.ProfileDataFields中的个人资料字段的对象,或基本个人资料。

Apple

SignInWithAppleOptions:

属性 描述
User 非必需。不确定此值的作用。它将被放入ASAuthorizationAppleIDRequest的用户属性中。
Scopes 要请求的额外作用域。通常情况下,您只会获得用户ID。注意:用户可以拒绝您访问这些作用域。可能的值:SignInWithAppleScope.EMAIL 和 SignInWithAppleScope.FULLNAME

SignInWithAppleResult:

属性 描述
ResultType 结果,要么是 SignInWithAppleResultType.ERROR,要么是 SignInWithAppleResultType.SUCCESS
ErrorCode 当结果类型为 SignInWithAppleResultType.ERROR 时,请求的错误代码。
ErrorMessage 当结果类型为 SignInWithAppleResultType.ERROR 时,请求的错误信息。
IdentityToken 一个JSON Web Token(JWT),用于安全地将有关用户的信息传达给您的应用程序。
AuthorizationCode 一个短期有效的令牌,用于您的应用程序在与应用程序的服务器对应部分交互时证明授权。
State 您的应用程序提供给生成凭证的请求的任意字符串。
User 与认证用户关联的标识符。
Email 当您添加了 EMAIL 作用域时。用户授权您的应用程序访问的联系人信息,当用户没有共享他们的个人电子邮件地址时,这可能是 @privaterelay.appleid.com。仅在用户第一次授权您的应用程序时可用。然而,它始终在 IdentityToken 字段中的 JWT 令牌中可用。
FullName 当您添加了 FULLNAME 作用域时。用户的姓名。仅在用户第一次授权您的应用程序时可用。
NameComponents 当您添加了 FULLNAME 作用域时。用户的姓名,表示为姓名组件(例如,名,后缀,昵称)。仅在用户第一次授权您的应用程序时可用。请参阅 SignInWithAppleNameComponents
AuthorizedScopes 一个授权作用域列表,用于验证用户是否授权了所有请求的作用域。
RealUserStatus 一个值,指示用户是否看起来是真实的人。

SignInWithAppleStateResult:

属性 描述
ResultType 结果,要么是 SignInWithAppleResultType.ERROR,要么是 SignInWithAppleResultType.SUCCESS
ErrorCode 当结果类型为 SignInWithAppleResultType.ERROR 时,请求的错误代码。
ErrorMessage 当结果类型为 SignInWithAppleResultType.ERROR 时,请求的错误信息。
State 授权的状态,要么是 SignInWithAppleStateResultState.REVOKED,要么是 SignInWithAppleStateResultState.AUTHORIZEDSignInWithAppleStateResultState.NOTFOUND

SignInWithAppleNameComponents:

属性 描述
GivenName 用户的给定(第一个)名字。
MiddleName 用户的中名。
FamilyName 用户的姓(最后一个名字)。
NamePrefix 用户的名字前缀(例如,Dr.,Ms.)。
NameSuffix 用户的名字后缀(例如,Ph.D.,Jr.)。
Nickname 用户的昵称。
PhoneticRepresentation 用户的名字,按照音译表示,表示为姓名组件(例如,名,后缀,昵称)。

关于 Klippa

Klippa 是一家来自 荷兰格罗宁根 的成长型公司,并于 2015 年由六位荷兰 IT 专家创立,目标是使用现代技术数字化纸质流程。

我们通过使用机器学习和 OCR 帮助客户提高其组织的有效性。自 2015 年以来,我们已为 1000 多位满意的客户提供了 Klippa 提供的各类软件解决方案。我们的热情是帮助我们的客户通过使用智能应用程序、应付账款软件以及 OCR 进行数据提取来数字化纸质流程。

许可证

MIT 许可证(MIT)