Last active
August 29, 2015 14:00
-
-
Save ebaxt/11244031 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
(defn path-info | |
"Returns the relative path of the request." | |
[request] | |
(or (:path-info request) | |
(:uri request))) | |
(defn authentication-failed [] | |
{:status 401 | |
:headers {"Content-Type" "application/json;charset=UTF-8"} | |
:body (json/write-str {:error "Unknown username or password."})}) | |
(defn authenticated [{session :session config :config}] | |
{:status 200 | |
:session session | |
:headers {"location" "/app/view"}}) | |
(defn ajax-login | |
[& {:keys [login-uri credential-fn] :as wf-config}] | |
(fn [{:keys [request-method headers params] :as request}] | |
;;util/gets is from friend utils, it's like get, but can take multiple maps and returns the first non-nil value | |
(when (and (= (util/gets :login-uri wf-config (::friend/auth-config request)) (path-info request)) | |
(= :post request-method) | |
(.startsWith (get headers "content-type") "application/json")) | |
(if-let [{:keys [username password] :as creds} (get-in request [:params :json-params])] | |
(if-let [user-record (and username password | |
((util/gets :credential-fn wf-config (::friend/auth-config request)) | |
(with-meta creds {::friend/workflow :ajax-login})))] | |
(let [resp (friend/merge-authentication request (workflows/make-auth user-record))] | |
(authenticated (if (get-in params [:json-params :remember]) | |
(assoc-in resp [:session :session_remember] true) | |
resp))) | |
(authentication-failed)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment