Facebook 登录迁移至 @capgo/social-login
复制一个包含安装步骤和本插件的完整Markdown指南的设置命令.
本指南提供了从中迁移的全面指南 @capacitor-community/facebook-login 到 @capgo/capacitor-social-login. The new plugin modernizes Facebook authentication with a unified API that supports multiple social providers, improved TypeScript support, and enhanced capabilities.
安装
标题:安装-
移除旧包:
终端窗口 npm uninstall @capacitor-community/facebook-login -
安装新包:
终端窗口 npm install @capgo/capacitor-social-loginnpx cap sync
Code 的变化
标题:Code 的变化Import Changes
导入更改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 initializationawait 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 typeinterface FacebookLoginResponse { accessToken: { applicationId: string; userId: string; token: string; expires: string; }; recentlyGrantedPermissions: string[]; recentlyDeniedPermissions: string[];}
// New response typeinterface 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 设置- 在 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])- 配置
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>重大变更
在迁移过程中发生的重大变更总结:现在需要显式初始化
- __CAPGO_KEEP_0__ - 必须先调用
initialize()在使用之前 - 响应对象结构已发生重大变化 - 新的嵌套结果格式,增强的用户资料
- 现在需要客户端令牌才能在 Android 上使用 - 需要额外的配置
- 不同方法名称和参数结构 - 基于提供商的方法
- 错误处理和错误类型已发生变化 - 更详细的错误信息
关键优势
标题为“关键优势”的部分新插件提供:
- 统一的API 跨多个社交提供商 (Google, Apple, Facebook)
- 改进的TypeScript支持 更好的类型定义
- 增强的用户资料 更多用户信息
- 积极维护 社区支持
- 一致的错误处理 所有提供商
- 更好的令牌管理 正确处理过期
请参阅 官方文档.
Keep going from Facebook Login Migration to @capgo/social-login
Section titled “Keep going from Facebook Login Migration to @capgo/social-login”如果您正在使用 Facebook Login Migration to @capgo/social-login 来规划身份验证和帐户流程,连接它 Using @capgo/capacitor-social-login for the native capability in Using @capgo/capacitor-social-login, @capgo/capacitor-social-login for the implementation detail in @capgo/capacitor-social-login, @capgo/capacitor-passkey 关于@capgo/capacitor-passkey的实现细节 @capgo/capacitor-native-biometric 关于@capgo/capacitor-native-biometric的实现细节,以及 双因素认证 关于双因素认证的实现细节。