Facebook登录迁移到@capgo/social-login
复制一个设置提示,包含安装步骤和该插件的完整 Markdown 指南。
本指南提供了从 @capacitor-community/facebook-login 到 @capgo/capacitor-social-login的全面迁移指南。新插件现代化了 Facebook 认证,采用统一的 API,支持多个社交提供商,改进了 TypeScript 支持,并增强了功能。
安装
安装-
移除旧的包:
终端窗口 npm uninstall @capacitor-community/facebook-login -
安装新包:
终端窗口 npm install @capgo/capacitor-social-loginnpx 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 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>Breaking Changes
关于迁移时的破坏性变更的总结:现在需要显式初始化
- - 必须在 使用之前调用
initialize()__CAPGO_KEEP_0__ - Response object structure has undergone significant changes - 新的嵌套结果格式,带有增强的用户资料
- 现在需要客户端令牌才能在 Android 上使用 - 需要额外的配置
- 方法名和参数结构发生了变化 - 基于提供商的方法
- 错误处理和错误类型发生了变化 - 提供了更详细的错误信息
主要优势
标题:主要优势新插件提供:
- 统一的 API 在 Google、Apple、Facebook 等多个社交提供商之间
- 改进的 TypeScript 支持 更好的类型定义
- 增强的个人资料 更多用户信息
- 活跃的维护 社区支持
- 一致的错误处理 所有提供商
- 更好的令牌管理 正确的过期处理
详细的设置指南,请参阅__CAPGO_KEEP_0__ 官方文档.