Last active
November 12, 2015 15:58
-
-
Save joshuakfarrar/d79338b650b44bf483e4 to your computer and use it in GitHub Desktop.
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
;; allowing me to use a field name of my choice | |
;; shouldn't require so much boilerplate | |
(ns app.friend | |
(:require [app.db.core :as db] | |
[cemerick.friend :as friend] | |
(cemerick.friend [workflows :as workflows] | |
[credentials :as creds]) | |
[ring.util.request :as request] | |
[ring.util.response :refer [response redirect status]] | |
[compojure.core :refer [defroutes]] | |
[hiccup.page :as h] | |
[hiccup.element :as e]) | |
(:use [clojure.string :only (trim)] | |
[cemerick.friend.util :only (gets)])) | |
(defn- username | |
[form-params params] | |
(or (get form-params "email") (:email params ""))) ;; all for this | |
(defn- password | |
[form-params params] | |
(or (get form-params "password") (:password params ""))) | |
(defn- use-email-as-username-workflow | |
[& {:keys [login-uri credential-fn login-failure-handler redirect-on-auth?] :as form-config | |
:or {redirect-on-auth? true}}] | |
(fn [{:keys [request-method params form-params] :as request}] | |
(when (and (= (gets :login-uri form-config (:cemerick.friend/auth-config request)) (request/path-info request)) | |
(= :post request-method)) | |
(let [creds {:username (username form-params params) | |
:password (password form-params params)} | |
{:keys [username password]} creds] | |
(if-let [user-record (and username password | |
((gets :credential-fn form-config (:cemerick.friend/auth-config request)) | |
(with-meta creds {:cemerick.friend/workflow :interactive-form})))] | |
(workflows/make-auth user-record | |
{:cemerick.friend/workflow :interactive-form | |
:cemerick.friend/redirect-on-auth? redirect-on-auth?}) | |
((or (gets :login-failure-handler form-config (:cemerick.friend/auth-config request)) #'workflows/interactive-login-redirect) | |
(update-in request [:cemerick.friend/auth-config] merge form-config))))))) | |
(defn- find-user-by-email | |
[email] | |
(first (db/get-user-by-email { :email email }))) | |
(defn add-friend-routes [routes] | |
(defroutes user-routes | |
(friend/authenticate | |
routes | |
{:allow-anon? true | |
:login-uri "/login" | |
:default-landing-uri "/" | |
:unauthorized-handler #(-> (h/html5 [:h2 "You do not have sufficient privileges to access " (:uri %)]) | |
response | |
(status 401)) | |
:credential-fn #(creds/bcrypt-credential-fn find-user-by-email %) | |
:workflows [(use-email-as-username-workflow)]}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment