;;;; src/web/hermetic.lisp
;;;;
;;;; demo quick reference
;;;;

(in-package #:cl-deck-builder2.web)

(defroute ("/hermetic" :method :GET) ()
  (if (hermetic:logged-in-p)
      (progn
        (flash :messages (format nil "Welcome, ~A!" (hermetic:user-name)))
        (render-with-env #P"/user/index.html"))
      (render-with-env #P"/user/login.html")))

(defroute ("/hermetic/login" :method :POST) (&key _parsed)
  (let ((username (query-param "username" _parsed))
        (password (query-param "password" _parsed)))
    (handler-case
        (ratify:with-parsed-forms
            ((:string username)
             (:string password))
          (let ((params (list :|username| username :|password| password)))
            (login params
                            (cl-markup:html5 (:h1 "You are logged in"))
                            (cl-markup:html5 (:h1 "Wrong password :c"))
                            (cl-markup:html5 (:h1 "No such username " params)))))
      (ratify:combined-error (e)
        (flash :errors e)
        (redirect "/hermetic")))))

(defroute ("/hermetic/logout" :method :GET) ()
  (logout
   (cl-markup:html5 (:h1 "You are logged out"))
   (cl-markup:html5 (:h1 "You are not logged in."))))

(defroute ("/hermetic/users-only" :method :GET) ()
  (auth (:user)
                 (cl-markup:html5 (:h1 "If you are seeing this, you are a user."))))

(defroute ("/hermetic/admins-only" :method :GET) ()
  (hermetic:auth (:admin)
                 (cl-markup:html5 (:h1 "If you are seeing this, you are an admin."))
                 (cl-markup:html5 (:h1 "Custom auth denied page. You are not authorized!"))))
