I have a LuminusWeb project running and I want to configure Friend for authorizing the routes. The trouble with this is that, friend has to be configured with the ring server like so:
(def page (handler/site
(friend/authenticate
routes
...
;--- elsewhere ---
(compojure/defroutes routes
(GET "/" req
But, in LuminusWeb's handler, the routes are all spread over various places and it has it's own views and models so handler barely has all routes. However, all the scattered routes are declared with the configuration. This is from LuminusWeb:
(def app (app-handler
;; add your application routes here
[home-routes app-routes]
...
As a beginner, my understanding of clojure is limited and I am not sure how to configure Friend
in this place. I've tried to look at the documentation of Friend, but apart from the readme and friend-demo apps there isn't anything else and all demo apps use the same configuration.
I have setup a very basic project on github which has just the bare bones of luminusweb and integrated Friend.
I was blindly inserting the configuration of Friend in different (and sensible) places until I got rid of all the compile time errors, but the project still doesn't run for one reason or another (results in run time error).
So, the question is, when my routes are scattered in different namespaces and I don't have a handler/site method, Where should I configure Friend?
Thank you.
I was facing that same question some days ago and I solved it like this.
In my handler.clj file I define my routes like this:
(def secured-routes
[(friend/authenticate user-routes user/friend-settings)
(friend/authenticate epics-routes user/friend-settings)
home-routes app-routes])
As you can see, some of them are secured and some are not. And then, in the app function I call them like this:
(def app
(app-handler
;; add your application routes here
secured-routes
;; instead of [user-routes home-routes ...]
...))
Update Yea, exactly, thats how my friend settings look like:
(def friend-settings
{:credential-fn (partial creds/bcrypt-credential-fn login-user)
:workflows [(workflows/interactive-form)]
:login-uri "/user/login"
:unauthorized-redirect-uri "/user/login"
:default-landing-uri "/"})
where login-user is a function that connects to datomic and reads user/password map.
You can have a look at the code at: https://github.com/sveri/friend-ui/ But please be aware, I am a clojure beginner myself and the code there surely is far from being idiomatic or follows best practices.