Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a login function in luminus or use friend?

Tags:

clojure

I am beginning to use luminus framework to develop a web app, and I am trying to use friend for auth, I am stacked here, I don't know how to use that like using gem in rails app. I don't know where should I put the code in luminus, is there anyone can show me a demo. Or tell me what to do next? Well, you can also tell me how to write a log in function in luminus.

like image 780
crazy_phage Avatar asked Oct 20 '22 17:10

crazy_phage


1 Answers

The login sort of works like it is posted in the Luminus Docs. Not sure if you managed to read that part, but I'll show you a simplified version of the code I use. I want to mention that I removed quite a bit of code to make everything a bit easier to understand, so this may not work as-is since I only deleted code and extra parens. Since it is from actual working code, it will work with a bit of tweeking:

The first part is getting the login form:

(defn login-page []
  (html5
    [:h3 "Login"]
    [:form {:method "POST" :action "login"}
     [:div "Username:" 
      [:input {:type "text" :name "username" :required "required"}]]
    [:div "Password:"
     [:input {:type "password" :name "password" :required "required"}]]
    [:div 
     [:input {:type "submit" :value "Log In"}]]]]))

Notice that there is a "POST" method? In order to get the routes to work, you have to have a "POST" route, but you will also need a "GET" route. This is the simplified version of the "GET" "POST" loop that I have:

(defroutes app-routes
  (GET "/login" []
       (log/login-page))

  (POST "/login" [username password]
        (do-login username password)))

The (do-login) function is where I authenticate the user / password combo, which then sets the session, which is shown downn below.

Notice that the POST route needs arguments. The arguments must match the "name" parameters in the form.

Finally, to get it all to work, you have to hook up some sessions. I personally use lib-noir.sessions:

(ns myapp.handler
  (:require [noir.session :as sesh])

Then you have to create a map to hold the session, which I'm wrapping in a function here (note that the :key must match whatever you have in your database:

(defn set-user [username]
  (sesh/put! :handle username))

And finally, you have to tell clojure that you want to allow sessions to be handled via middleware:

(def app
  (sesh/wrap-noir-session
    (handler/site
     app-routes)))

Hopefully that gives you a bit of a headstart. I did not include how to connect to a database or how to use a map, but the above should be enough to get you on your way. I also did not touch on authorization and security (please don't skip this!). Using a database, map, or friend isn't a huge quantum leap from here. Just wanted to offer just enough to get you started.

like image 130
dizzystar Avatar answered Jan 01 '23 05:01

dizzystar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!