Created
          September 28, 2014 17:45 
        
      - 
      
 - 
        
Save vbedegi/91d180c711bd1e0ecec3 to your computer and use it in GitHub Desktop.  
    Qarth sample with Ring
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (ns qartoy.handler | |
| (:require [clojure.tools.logging :as log] | |
| [ring.util.response :refer [redirect]] | |
| [compojure.core :refer :all] | |
| [compojure.handler :as handler] | |
| [compojure.route :as route] | |
| [qarth.oauth :as oauth] | |
| [qarth.impl.facebook :refer :all] | |
| [qarth.impl.google :refer :all] | |
| [qarth.impl.github :refer :all] | |
| [qarth.impl.twitter :refer :all] | |
| [qarth.ring :as qr] | |
| [cheshire.core :as j])) | |
| (defn get-record-type [record] | |
| (let [type (:type record)] | |
| (if (= type :multi) | |
| (:key record) | |
| type))) | |
| (defmulti get-user (fn [service record] (get-record-type record))) | |
| (defmethod get-user :facebook [service record] | |
| (let [requestor (oauth/requestor service record)] | |
| (-> (requestor {:url "https://graph.facebook.com/me"}) | |
| :body | |
| (j/parse-string true)))) | |
| (def facebook-conf {:type :facebook | |
| :scope "public_profile,email,user_likes,user_friends,user_photos,user_relationships,read_stream,publish_actions" ; Scopes are optional | |
| :api-key "***" | |
| :api-secret "***" | |
| :callback "http://localhost:3000/auth"}) | |
| (def facebook-service (oauth/build facebook-conf)) | |
| (def omni-handler-config {:service facebook-service | |
| :success-handler (fn [req] | |
| (let [record (get-in req [:session :qarth.oauth/record]) | |
| user (get-user facebook-service record)] | |
| (-> | |
| (redirect "/welcome") | |
| (assoc-in [:session :user] user)))) | |
| :failure-handler (fn [req] | |
| (redirect "/auth-failed"))}) | |
| (def auth-handler (qr/omni-handler omni-handler-config)) | |
| (defroutes app-routes | |
| (GET "/" _ | |
| (str "<html><head/><body>" | |
| "<p><a href=\"/auth\">Login with Facebook</p>" | |
| "</body></html>")) | |
| (GET "/welcome" req | |
| (str "<h3>welcome</h3>" (get-in req [:session :user]))) | |
| (GET "/auth-failed" req | |
| "auth failed") | |
| (ANY "/auth" req (auth-handler req)) | |
| (route/resources "/") | |
| (route/not-found "Not Found")) | |
| (def app | |
| (handler/site app-routes)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment