Facebook 登录迁移至 @capgo/social-login
复制一个包含安装步骤和本插件的完整Markdown指南的设置提示。
概述
概述部分本指南提供了从 @capacitor-community/facebook-login 到 @capgo/capacitor-social-login.新插件通过统一的API支持多个社交提供商、改进的 TypeScript 支持和增强的功能来现代化 Facebook 认证。
安装
安装-
移除旧包:
终端窗口 npm uninstall @capacitor-community/facebook-login -
安装新包:
终端窗口 npm install @capgo/capacitor-social-loginnpx cap sync
Code 变更
Code 变更导入变更
导入变更import { FacebookLogin } from '@capacitor-community/facebook-login';import { SocialLogin } from '@capgo/capacitor-social-login';Initialization
Initialization关键变更:新包需要在您的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 }});登录
LoginKey Change
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' }});响应类型变更
Response Type Changes响应结构已现代化,使用更全面的用户资料对象:
// 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'});平台特定变更
平台特定变更安卓设置
安卓设置配置现在通过initialize方法进行管理:
// 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])- The
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>重大变更
重大变更在迁移过程中发生的重大变更概述:
- 现在需要显式初始化 - 必须调用
initialize()在使用之前 - 响应对象结构发生了重大变化 - 新增嵌套结果格式,增强的个人资料
- 现在需要客户端令牌 - 需要额外的配置
- 不同方法名称和参数结构 - 基于提供商的方法
- 错误处理和错误类型已发生变化 - 更详细的错误信息
关键优势
标题:关键优势新插件提供:
- 统一的API 跨多个社交提供商(Google,Apple,Facebook)
- 改进的TypeScript支持 具有更好的类型定义
- 增强的个人资料 具有更多用户信息
- 积极的维护 和社区支持
- 一致的错误处理 所有提供商
- 更好的令牌管理 具有适当的过期处理
详细的设置指南,请参阅 官方文档.
从Facebook登录迁移继续到@capgo/social-login
标题:从Facebook登录迁移继续到@capgo/social-login如果您正在使用 Facebook登录迁移继续到@capgo/social-login 来规划身份验证和帐户流程,连接它到 使用@capgo/capacitor-social-login 为@capgo/capacitor-social-login中的原生能力 为@capgo/capacitor-social-login中的实现细节 为@capgo/capacitor-passkey中的实现细节 为@capgo/capacitor-passkey 为@capgo/capacitor-native-biometric中的实现细节 为@capgo/capacitor-native-biometric 为 @capgo/capacitor-native-biometric 的实现细节 双因素认证 为双因素认证的实现细节