O'Reilly logo

Web Development with Clojure by Dmitri Sotnikov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Task B: Login and Logout

Login and logout actions are very simple to implement. To log in the user will submit her ID and password using a login form. These will be checked against the stored credentials, and if they match we’ll put the ID in the session.

Let’s navigate to our picture-gallery.models.db namespace. We’ve already added a function to store the users in the database, and we now need to add one to read the users based on the IDs. We’ll use the with-query-results macro to get the records and return the first item. If no record matches the supplied ID, we’ll simply get a nil value back.

 
(​defn​ get-user [id]
 
(sql/with-connection
 
db
 
(sql/with-query-results
 
res [​"select * from users where id = ?"​ id] (​first​ res))))

Note ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required