         username (get form-params "username" "")
         password (get form-params "password" "")]
     (if (or (empty? username) (empty? password))
+      ;; Firstly validates the input, if required fields
+      ;; are empty, render the login page html with
+      ;; convenient error mesasage and return it.
       (http/ok (render-login-page ["The two fields are mandatory."])
                {:content-type "text/html"})
+
+      ;; In case contrary, try validate the incoming
+      ;; credentials, and in case of them validates
+      ;; successful, update the session `:identity` key
+      ;; with the authenticated user object.
+      ;; In case contrary, return a login page
+      ;; rendered with approapiate error message
       (if-let [user (authenticate username password)]
-        (let [nexturl (get query-params "next" "/")]
+        (let [nexturl (get query-params "next" "/")
+              session (:session context)]
+          (swap! session assoc :identity user)
           (http/found nexturl))
         (http/ok (render-login-page ["User or password are incorrect"])
                  {:content-type "text/html"})))))

        (for [e errors] [:div e])])
     [:form {:action "" :method "post"}
      [:div {:class "input-wrapper"}
       [:input {:type "text" :name "username" :placeholder "Write your username here..."}]]
      [:div {:class "input-wrapper"}
       [:input {:type "password" :name "password" :placeholder "Write your password here..."}]]
      [:div {:class "input-wrapper"}
       [:input {:type "submit" :value "Submit"}]]]])))


(defn- authenticate
  [username, password]
  (when (and (= username "admin")
             (= password "123123"))
    {:userid 1}))

(defn login-page
  [context]
  (-> (render-login-page ["foo", "bar"])
      (http/ok {:content-type "text/html"})))

(defn login-submit
  [context]

;; Home Page
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Shows a simple html page. It has different content for anonynmous
;; and logged users.

(defn home-page
  [context]
  (-> (layout [:section {:class "home-page"}
               (if-let [user (:identity context)]
                 [:div
                  [:p (format "Welcome %s" (:username user))]
                  [:p [:a {:href "/logout"} "logout"]]]
                 [:div
                  [:p "Welcome to the dummy website application."]
                  [:p [:a {:href "/login"} "Login"]]])])
      (http/ok {:content-type "text/html"})))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Login page
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; A helper function for render login page, it used also for initial
;; rendering and render login page with errors on post requests.

(defn- render-login-page
  ([] (render-login-page []))
