Facebook Sign-in using Clojure

Table of contents
Reading Time: 2 minutes

In order to provide Facebook sign-in functionality you need to create a new Facebook App from this url: https://developers.facebook.com/docs/facebook-login. Provide the redirection URI that you’ll use for the App. After setting up the app you’ll get an App ID and App Secret key.

We’ll use “clj-oauth2.client” to redirect a user to the authentication page, “clj-http.client” for HTTP requests, “cheshire.core” to parse Json string in order to get keywords, “noir.respose” to provide re-directions and “compojure.core” for routing.

We’ll begin with defining a new namespace:

In facebook namespace define an atom, facebook-user, to store Facebook user details:

Now include the App ID and App Secret key you just got after creating a new Facebook app along with the redirection URI, in the code below:

and define an oauth2 map containing all the details required for Facebook log in:

We can use “make-auth-request” function defined in “clj-oauth2.client” library to get the redirect URI on which the user should be redirected. The user can now give access to the app we created in first step.

Note that we used noir.response/redirect function to provide the redirection.

After a Facebook user grant access to the app, we’ll get a response containing access token on the redirection URI we provided while setting up our Facebook app. We’ll use “compojure.core’s” GET to get the response:

and pass it to a function in order to get the access token and user details.

We’ll use the code below for getting access token and user details:

Written by 

Sidharth is a Lead Consultant, having experience of more than 4.5 years. He has started working on Scala and Clojure and is actively involved in other developmental work. He enjoys working in a team and believes that knowledge is something that should be shared openly and on a large scale. As an avid gamer and passionate player, he likes to be involved in both indoor and outdoor activities.

4 thoughts on “Facebook Sign-in using Clojure2 min read

    1. Hi Jason, unfortunately there’s no git repo for it, though the code snippets in this blog post covers almost all the things that would be required to use it.

Comments are closed.