Skip to content

Instantly share code, notes, and snippets.

@lrenn
Created July 24, 2009 03:20
Show Gist options
  • Save lrenn/153821 to your computer and use it in GitHub Desktop.
Save lrenn/153821 to your computer and use it in GitHub Desktop.
(defn login-view [flash]
(html [:html
[:body
[:h2 "Login"]
(if-let [message (:message flash)]
[:h4 message])
(form-to [:post "/login"]
(text-field :username)
(password-field :password)
(submit-button "Login"))]]))
(defn login-controller [params request]
(if (= (:password params) "secret")
[(session-assoc :username (:username params))
(redirect-to "/home")]
[(flash-assoc :message "Login failed.")
(redirect-to "/login")]))
(defn home-view [username]
(html
[:html
[:body
[:h2 "Home"
[:p (str "Hello " username ".")]]]]))
(defn home-controller [session]
(if-let [username (:username session)]
(home-view username)
(redirect-to "/login")))
(defroutes app
(GET "/login" (login-view flash))
(POST "/login" (login-controller params request))
(GET "/home" (home-controller session)))
(decorate app
(with-session))
(run-server {:port 8080} "/*" (servlet app))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment