   (POST "/login" [] login-authenticate)
   (GET "/logout" [] logout))
 
-
-;; Self defined unauthorized handler
-;; This function is responsible of handling unauthorized requests.
+;; User defined unauthorized handler
+;; This function is responsible for handling unauthorized requests.
 ;; (When unauthorized exception is raised by some handler)
 
 (defn unauthorized-handler

  [request]
  (render (slurp (io/resource "login.html")) request))

;; Authenticate Handler
;; Respons to post requests in same url as login and is responsible to
;; identify the incoming credentials and set the appropiate authenticated
;; user into session. `authdata` will be used as source of valid users.

(defn login-authenticate
  [request]
  (let [username (get-in request [:form-params "username"])
        password (get-in request [:form-params "password"])
        session (:session request)]
    (if-let [found-password (get authdata (keyword username))]
      (if (= found-password password)
        (let [nexturl (get-in request [:query-params :next] "/")
              session (assoc session :identity (keyword username))]
          (-> (redirect nexturl)
              (assoc :session session)))
        (render (slurp (io/resource "login.html")) request))
      (render (slurp (io/resource "login.html")) request))))


;; Logout handler
;; It is responsible of cleaing session.

(defn logout
  [request]

    (throw-unauthorized)
    (response (slurp (io/resource "index.html")))))

;; Login page controller
;; It returns a login page on get requests.

