Facebook Login Setup
Konten ini belum tersedia dalam bahasa Anda.
Facebook Login Setup
Introduction
In this guide, you will learn how to setup Facebook Login with Capgo Social Login. You will need the following:
- A Facebook Developer Account
- Your app’s package name/bundle ID
- Access to a terminal for generating key hashes (Android)
General Setup
If you don’t already have a Facebook app created, follow these steps:
-
Create a Facebook App
Follow the tutorial to Create an App
-
Add Facebook Login to your app
In your Facebook Developer Dashboard, add the Facebook Login product to your app
-
Before you can release your app to the public, follow this tutorial to publish it
Important Information
Here’s where to find the key information you’ll need for integration:
-
CLIENT_TOKEN
: -
APP_ID
: -
APP_NAME
:
Android Setup
-
Add internet permission to your
AndroidManifest.xml
Ensure this line is present:
<uses-permission android:name="android.permission.INTERNET"/> -
Generate your Android key hash
This is a crucial security step required by Facebook. Open your terminal and run:
Terminal window keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl base64 -AWhen prompted for a password, use:
android
-
Add the key hash to your Facebook app
- Go to your app’s dashboard on Facebook Developers
- Navigate to Settings > Basic
- Scroll down to “Android” section
- Click “Add Platform” if Android isn’t added yet and fill in the details
- Add the key hash you generated
- For production, add both debug and release key hashes
-
Update your
AndroidManifest.xml
to include:<application>...<activity android:name="com.facebook.FacebookActivity"android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"android:label="@string/app_name" /><activityandroid: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="FB[APP_ID]" /></intent-filter></activity></application>
iOS Setup
-
Add the iOS platform in Facebook Developer Console
- Go to your app’s dashboard on Facebook Developers
- Navigate to Settings > Basic
- Scroll down to very bottom of the page and click “Add Platform”
- Select iOS and fill in the required details
-
Open your Xcode project and navigate to Info.plist
-
Add the following entries to your Info.plist:
<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>fb-messenger-share-api</string></array><key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>fb[APP-ID]</string></array></dict></array> -
Modify the
AppDelegate.swift
import FBSDKCoreKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.// Initialize Facebook SDKFBSDKCoreKit.ApplicationDelegate.shared.application(application,didFinishLaunchingWithOptions: launchOptions)return true}func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {// Called when the app was launched with a url. Feel free to add additional processing here,// but if you want the App API to support tracking app url opens, make sure to keep this callif (FBSDKCoreKit.ApplicationDelegate.shared.application(app,open: url,sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,annotation: options[UIApplication.OpenURLOptionsKey.annotation])) {return true;} else {return ApplicationDelegateProxy.shared.application(app, open: url, options: options)}}}
Using Facebook Login in Your App
-
Initialize the Facebook login in your app
import { SocialLogin } from '@capgo/capacitor-social-login';// Initialize during app startupawait SocialLogin.initialize({facebook: {appId: 'APP_ID',clientToken: 'CLIENT_TOKEN',}}) -
Implement the login function
async function loginWithFacebook() {try {const result = await SocialLogin.login({provider: 'facebook',options: {permissions: ['email', 'public_profile'],limitedLogin: false // this will depend if you want to use the limited login or not}});console.log('Facebook login result:', result);// Handle successful login} catch (error) {console.error('Facebook login error:', error);// Handle error}}
Troubleshooting
Common Issues and Solutions
-
Key hash errors on Android
- Double check that you’ve added the correct key hash to the Facebook dashboard
- For release builds, make sure you’ve added both debug and release key hashes
- Verify you’re using the correct keystore when generating the hash
-
Facebook login button doesn’t appear
- Verify all manifest entries are correct
- Check that your Facebook App ID and Client Token are correct
- Ensure you’ve properly initialized the SDK
-
Common iOS issues
- Make sure all Info.plist entries are correct
- Verify URL schemes are properly configured
- Check that your bundle ID matches what’s registered in the Facebook dashboard
Testing
-
Before testing, add test users in the Facebook Developer Console
- Go to Roles > Test Users
- Create a test user
- Use these credentials for testing
-
Test both debug and release builds
- Debug build with debug key hash
- Release build with release key hash
- Test on both emulator and physical devices
Remember to test the full login flow, including:
- Successful login
- Login cancellation
- Error handling
- Logout functionality