I just started hacking around with Clojure, and although I adore the language, I can't understand how to do certain things idiomatically.
Writing a web-app using compojure, here's one of my controller actions:
(defn create [session params]
(let [user (user/find-by-email (params :email))]
(if user
(if (user/authenticate user (params :password))
(do (sign-in session user)
(resp/redirect "/home?signed-in=true"))
(resp/redirect "/?error=incorrect-password"))
(let [new-user (user/create params)]
(sign-in session new-user)
(resp/redirect "/home?new-user=true")))))
I'm writing this in a very imperative way. Using so many let
s/if
s/do
s, I can't help but think I'm doing something very wrong. How would I write this functionally?
Here's the psuedocode for what I'm trying to do
look if user exists
if user exists, try to sign user in using password provided
if password is wrong, redirect to "/?error=incorrect-password"
if password is correct, sign user in and redirect to "/home?signed-in=true"
else create user, sign user in, and redirect to "/home?new-user=true"
Thanks so much!