Skip to content

Instantly share code, notes, and snippets.

@NoamB
Created October 26, 2013 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoamB/7173599 to your computer and use it in GitHub Desktop.
Save NoamB/7173599 to your computer and use it in GitHub Desktop.
working require-login
(defn require-login*
"Used to wrap a route handler, checks if 'logged-in?'. If true, allows the handler to run.
If false, rejects the user."
[success-method fail-method {session :session :as req}]
(if (logged-in? session)
(apply success-method [req])
(apply fail-method [req])))
(defn- require-login-single-form
[fail-method [method path args & body :as form]]
`(~method ~path ~args #(require-login* ~@body ~fail-method %)))
(defmacro require-login
[fail-method & forms]
(let [processed-forms (vec (map #(require-login-single-form fail-method %) (vec forms)))]
`(apply routes ~processed-forms)))
@stask
Copy link

stask commented Oct 27, 2013

Very cool,
Btw, in lines 6 and 7, why don't you just use (success-method req) and (fail-method req)

@NoamB
Copy link
Author

NoamB commented Oct 27, 2013

Thanks, you're right, no point in using apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment