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 ...

Get Web Development with Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.