跳过内容

Facebook登录迁移到@capgo/social-login

本指南提供了从 @capacitor-community/facebook-login@capgo/capacitor-social-login的全面迁移指南。新插件现代化了 Facebook 认证,采用统一的 API,支持多个社交提供商,改进了 TypeScript 支持,并增强了功能。

安装

安装
  1. 移除旧的包:

    终端窗口
    npm uninstall @capacitor-community/facebook-login
  2. 安装新包:

    终端窗口
    npm install @capgo/capacitor-social-login
    npx cap sync

导入变更

导入变更
import { FacebookLogin } from '@capacitor-community/facebook-login';
import { SocialLogin } from '@capgo/capacitor-social-login';

密钥变更: 需要在您的 code: 中显式设置新包

// Old package required no explicit initialization in code
// Configuration was done only in native platforms
// New package requires explicit initialization
await SocialLogin.initialize({
facebook: {
appId: 'YOUR_FACEBOOK_APP_ID', // Required for web and Android
clientToken: 'YOUR_CLIENT_TOKEN' // Required for Android
}
});

登录方法现在接受一个提供者参数:

const FACEBOOK_PERMISSIONS = ['email', 'public_profile'];
const result = await FacebookLogin.login({ permissions: FACEBOOK_PERMISSIONS });
const result = await SocialLogin.login({
provider: 'facebook',
options: {
permissions: ['email', 'public_profile'],
limitedLogin: false,
nonce: 'optional_nonce'
}
});

响应类型变更

标题:响应类型变更

响应结构已现代化,具有更全面的配置文件对象:

// Old response type
interface FacebookLoginResponse {
accessToken: {
applicationId: string;
userId: string;
token: string;
expires: string;
};
recentlyGrantedPermissions: string[];
recentlyDeniedPermissions: string[];
}
// New response type
interface FacebookLoginResponse {
provider: 'facebook';
result: {
accessToken: {
token: string;
applicationId?: string;
expires?: string;
userId?: string;
permissions?: string[];
declinedPermissions?: string[];
} | null;
idToken: string | null;
profile: {
userID: string;
email: string | null;
friendIDs: string[];
birthday: string | null;
ageRange: { min?: number; max?: number } | null;
gender: string | null;
location: { id: string; name: string } | null;
hometown: { id: string; name: string } | null;
profileURL: string | null;
name: string | null;
imageURL: string | null;
};
};
}

关键差异:

  • 响应现在包括一个 provider 字段来识别身份验证提供者
  • 更详细的 profile 对象,包含更多用户信息
  • 所有社交登录提供商都具有一致的结构
const result = await FacebookLogin.getCurrentAccessToken();
const isLoggedIn = result && result.accessToken;
const status = await SocialLogin.isLoggedIn({
provider: 'facebook'
});
const isLoggedIn = status.isLoggedIn;

注销

登出
await FacebookLogin.logout();
await SocialLogin.logout({
provider: 'facebook'
});

平台特定更改

平台特定更改

安卓设置

安卓设置

现在通过初始化方法来处理配置:

// AndroidManifest.xml changes remain the same
// strings.xml become irrelevant
// Additionally initialize in your code:
await SocialLogin.initialize({
facebook: {
appId: 'your-app-id',
clientToken: 'your-client-token' // New requirement
}
});

重要客户端令牌现在需要在安卓身份验证中

iOS设置

iOS设置
  1. iOS设置在 AppDelegate.swift 保持不变:
import FBSDKCoreKit
// In application:didFinishLaunchingWithOptions:
FBSDKCoreKit.ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
// In application:openURL:options:
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
  1. 配置 Info.plist 保持不变:
<key>CFBundleURLTypes</key>
<array>
<dict>
<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>
<string>fbapi</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

现在需要显式初始化

  1. - 必须在 使用之前调用 initialize() __CAPGO_KEEP_0__
  2. Response object structure has undergone significant changes - 新的嵌套结果格式,带有增强的用户资料
  3. 现在需要客户端令牌才能在 Android 上使用 - 需要额外的配置
  4. 方法名和参数结构发生了变化 - 基于提供商的方法
  5. 错误处理和错误类型发生了变化 - 提供了更详细的错误信息

新插件提供:

  • 统一的 API 在 Google、Apple、Facebook 等多个社交提供商之间
  • 改进的 TypeScript 支持 更好的类型定义
  • 增强的个人资料 更多用户信息
  • 活跃的维护 社区支持
  • 一致的错误处理 所有提供商
  • 更好的令牌管理 正确的过期处理

详细的设置指南,请参阅__CAPGO_KEEP_0__ 官方文档.