Skip to content

povio/PovioKitAuthFacebook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PovioKit

Welcome to PovioKitAuthFacebook.
An auth provider for social login with Facebook.

Requirements

  • iOS 16.0+
  • Swift 5.9+
  • Xcode 15+
  • A Facebook App ID and Client Token (configured in the Meta for Developers console)

Installation

Swift Package Manager

  • In Xcode, click File -> Add Packages...
  • Insert https://github.com/poviolabs/PovioKitAuthFacebook in the Search field.
  • Select a desired Dependency Rule. Usually "Up to Next Major Version" with "1.0.0".
  • Select "Add Package" button and check PovioKitAuthFacebook.
  • Select "Add Package" again and you are done.

Setup

For the full picture, read the official Facebook documentation. The minimum required steps are summarised below.

1. Configure Info.plist

Add the following keys (replace <APP_ID>, <CLIENT_TOKEN>, and <APP_NAME> with your values):

<key>FacebookAppID</key>
<string><APP_ID></string>
<key>FacebookClientToken</key>
<string><CLIENT_TOKEN></string>
<key>FacebookDisplayName</key>
<string><APP_NAME></string>

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb<APP_ID></string>
        </array>
    </dict>
</array>

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-share-api</string>
</array>

2. Initialise the Facebook SDK at launch

UIKit (AppDelegate)

import FBSDKCoreKit

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
  ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
  return true
}

SwiftUI app lifecycle

import SwiftUI
import FBSDKCoreKit

@main
struct MyApp: App {
  @UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
  
  var body: some Scene {
    WindowGroup { ContentView() }
  }
}

final class AppDelegate: NSObject, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
  }
}

Usage

// initialization
let authenticator = FacebookAuthenticator()

// signIn user with default permissions ([.email, .publicProfile])
let result = try await authenticator
  .signIn(from: <view-controller-instance>)

// signIn user with custom permissions
let result = try await authenticator
  .signIn(from: <view-controller-instance>, with: [.email, .userBirthday])

// build "defaults + extras" without hard-coding them
let permissions = FacebookAuthenticator.defaultPermissions + [.userFriends]
let result = try await authenticator
  .signIn(from: <view-controller-instance>, with: permissions)

// refresh the access token (and re-fetch user details)
let refreshed = try await authenticator.refreshTokenIfNeeded()

// get authentication status
let state = authenticator.isAuthenticated

// signOut user (local only — does NOT revoke server-side permissions)
authenticator.signOut()

// fully revoke permissions on Facebook's servers, then signOut locally
try await authenticator.revokePermissions()

The FacebookPermission typealias is exposed by this package so you don't need to import FacebookLogin to use .email, .publicProfile, .userBirthday, etc.

Handling the OAuth return URL

UIKit (UIApplicationDelegate)

func application(
  _ application: UIApplication,
  open url: URL,
  options: [UIApplication.OpenURLOptionsKey: Any] = [:]
) -> Bool {
  authenticator.canOpenUrl(url, application: application, options: options)
}

SwiftUI (.onOpenURL)

ContentView()
  .onOpenURL { url in
    _ = authenticator.canOpenUrl(url, application: .shared, options: [:])
  }

UIKit Scene-based apps (UISceneDelegate)

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  guard let context = URLContexts.first else { return }
  _ = authenticator.canOpenUrl(
    context.url,
    application: .shared,
    options: [.sourceApplication: context.options.sourceApplication as Any]
  )
}

License

PovioKitAuthFacebook is available under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages