Skip to content

Instantly share code, notes, and snippets.

@iperdomo
Created March 17, 2014 09:08
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 iperdomo/9596103 to your computer and use it in GitHub Desktop.
Save iperdomo/9596103 to your computer and use it in GitHub Desktop.
Proposal to make `:redirect-on-auth?` dynamic
diff --git a/src/cemerick/friend.clj b/src/cemerick/friend.clj
index 0c8dda5..0731a64 100644
--- a/src/cemerick/friend.clj
+++ b/src/cemerick/friend.clj
@@ -154,6 +154,7 @@ Equivalent to (complement current-authentication)."}
resp (response/redirect-after-post
(or unauthorized-uri
(and (string? redirect) redirect)
+ (and (fn? redirect) (redirect authentication-map request))
(str (:context request) (-> request ::auth-config :default-landing-uri ))))]
(if unauthorized-uri
(-> resp
@iperdomo
Copy link
Author

  • The intention is to redirect a user to its home after successful login
  • The function gets the current authentication-map and request and returns a string with the redirect path
  • It's based on the similar change introduced in v0.1.2
(def secured-app
  (-> ring-app
    (friend/authenticate {:credential-fn (partial creds/bcrypt-credential-fn users)
                          :workflows [(workflows/interactive-form)]
                          :redirect-on-auth? (fn [auth-map req]
                                               (str "/" (get-in req [params username])})
    ; ...required Ring middlewares ...
    ))

@cemerick
Copy link

The redirect value you're proposing checking to be a function or not is not coming from the configuration to friend/authenticate; it's a value that is attached to the authentication map's metadata, as returned by your workflow.

You can achieve what you want now by modifying the responses of the workflow(s) you use to add in the user's home URL as the :cemerick.friend/redirect in the authentication map's metadata.

That said, providing a way to dynamically control redirects without providing custom or wrapped workflows would make things easier (and simpler, if multiple workflows are in play). Please file an issue, but the proposed change here is not correct.

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