I'm building a single page application using angular and clojure. Currently, a user registers their account and logs in by providing and submitting their credentials through a form in an html template. Clojure then checks to see if their password matches the encrypted copy in the database and returns a json object containing their username, first/last names, role, and some other information. The entire front end is angular, and as such, there is one main route:
(defroutes main
(GET "/" [] (layout/master)))
The application makes various requests to service routes for other functionality:
(defroutes service
(GET "/api/private" {params :params} (http/json-response 200 {:success true :message "it worked"}))
(GET "/api/even-privater" {params :params} (http/json-response 200 {:success true :message "it really worked"})))
Currently, there is no security on those routes. What i would like, is to use friend/authenticate
to protect those service routes, however, I cannot seem to find a workflow that works for me nor can I can find any documentation regarding using cemerick/friend for a single page application.
Ideally, a user would log in, and then be able to make requests to the secured routes. If they are not authenticated, they would simply receive 401 responses. Upon successful login, the user would receive an http 200 with some relevant user information.
I have thoroughly read through the workflows code, this stackoverflow post as well as this issue solution, but still cannot wrap my head around what I need to do. The demos found here helped me understand what friend can do, but I am finding it difficult to apply what I've learned from them.
Ideally, I would like to put the app together something like this:
(def secured-service
(friend/authenticate
routes/service
{:credential-fn a-credential-function
:unauthenticated-handler {:status 401 :body "Unauthenticated"}
:workflows [(workflows/a-spa-workflow)]}))
(def app (middleware/app-handler
[routes/main routes/public secured-service routes/app]
:middleware []
:formats [:json-kw :edn]))
With authentication handled by a route that uses make-auth
(defroutes public
(POST "/api/login" {params :params} (workflows/make-auth user-record ...)))
Does anyone know about some documentation regarding this issue that could help me? Or, better yet, any ideas on how to accomplish this?