The latest version of Kitura-CredentialsFacebook requires Swift 4.0 or newer. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.
Usage
Add dependencies
Add the Kitura-CredentialsFacebook and Credentials packages to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest Kitura-CredentialsFacebookrelease and the latest Kitura-Credentialsrelease.
fbClientId is the App ID of your app in the Facebook Developer dashboard
fbClientSecret is the App Secret of your app in the Facebook Developer dashboard
options is an optional dictionary ([String:Any]) of Facebook authentication options whose keys are listed in CredentialsFacebookOptions
Note: The callbackUrl parameter above is used to tell the Facebook web login page where the user’s browser should be redirected when the login is successful. It should be a URL handled by the server you are writing.
Specify where to redirect non-authenticated requests:
Example of authentication with Facebook OAuth token
This example shows how to use CredentialsFacebookToken plugin to authenticate post requests, it shows both the server side and the client side of the request involved.
Server side
First create an instance of Credentials and an instance of CredentialsFacebookToken plugin:
import Credentials
import CredentialsFacebook
let credentials = Credentials()
let fbCredentials = CredentialsFacebookToken(options: options)
Where:
options is an optional dictionary ([String:Any]) of Facebook authentication options whose keys are listed in CredentialsFacebookOptions.
If the authentication is successful, request.userProfile will contain user profile information received from Facebook:
router.post("/collection/:new") {request, response, next in
...
let profile = request.userProfile
let userId = profile.id
let userName = profile.displayName
...
next()
}
Client side
The client needs to put Facebook access token in request’s access_token HTTP header field, and “FacebookToken” in X-token-type field:
Kitura-CredentialsFacebook
Plugin for the Credentials framework that authenticates using Facebook.
Summary
Plugin for the Kitura-Credentials framework that authenticates using the Facebook web login with OAuth and a Facebook OAuth token that was acquired by a mobile app or other client of the Kitura based backend.
Swift version
The latest version of Kitura-CredentialsFacebook requires Swift 4.0 or newer. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.
Usage
Add dependencies
Add the
Kitura-CredentialsFacebook
andCredentials
packages to the dependencies within your application’sPackage.swift
file. Substitute"x.x.x"
with the latestKitura-CredentialsFacebook
release and the latestKitura-Credentials
release.Add
CredentialsFacebook
andCredentials
to your target’s dependencies:Import packages
Example of Facebook web login
A complete sample can be found in Kitura-Sample.
First set up the session:
Create an instance of
CredentialsFacebook
plugin and register it with theCredentials
framework:Where:
CredentialsFacebookOptions
Note: The callbackUrl parameter above is used to tell the Facebook web login page where the user’s browser should be redirected when the login is successful. It should be a URL handled by the server you are writing. Specify where to redirect non-authenticated requests:
Connect
credentials
middleware to requests to/private
:And call
authenticate
to login with Facebook and to handle the redirect (callback) from the Facebook login web page after a successful login:Example of authentication with Facebook OAuth token
This example shows how to use
CredentialsFacebookToken
plugin to authenticate post requests, it shows both the server side and the client side of the request involved.Server side
First create an instance of
Credentials
and an instance ofCredentialsFacebookToken
plugin:Where:
CredentialsFacebookOptions
.Now register the plugin:
Connect
credentials
middleware to post requests:If the authentication is successful,
request.userProfile
will contain user profile information received from Facebook:Client side
The client needs to put Facebook access token in request’s
access_token
HTTP header field, and “FacebookToken” inX-token-type
field:API documentation
For more information visit our API reference.
Community
We love to talk server-side Swift, and Kitura. Join our Slack to meet the team!
License
This library is licensed under Apache 2.0. Full license text is available in LICENSE.