From 6b1c9d7ae508c41848828939454945bf2addb994 Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 21 Jan 2023 09:49:39 -0800 Subject: Refactor+Fix: with-hero-session macro; packaging and init --- routes.lisp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'routes.lisp') diff --git a/routes.lisp b/routes.lisp index b0b81ac..f737c80 100644 --- a/routes.lisp +++ b/routes.lisp @@ -13,10 +13,20 @@ (setf (lzb:response-header :location) location (lzb:response-code) "303")) -(defmacro with-session (&body body) - `(a:if-let ((sesh (session-with-id (lzb:request-cookie +session-cookie-name+)))) - ,@body - (redirect-to "/tavern-door"))) +(defun current-session () + "Get the session associated with the current request. Will throw an +error if lazybones:*request* is not currently bound. It will return +NIL if there is no session for the current request. + +I.e. It should be called within the scope of a request handler." + (session-with-id (lzb:request-cookie +session-cookie-name+ ))) + +(defmacro with-hero-session ((hero &key session (redirect "/tavern-door")) &body body) + (let ((session (or session (gensym "SESSION")))) + `(a:if-let (,session (current-session)) + (let ((,hero (session-hero ,session))) + ,@body) + (redirect-to ,redirect)))) (defendpoint* :get "/godess-shrine" () () (godess-shrine)) @@ -39,4 +49,5 @@ (redirect-to "/tavern-door")))) (defendpoint* :get "/tavern" () () - (with-session (tavern (session-hero sesh)))) + (with-hero-session (hero) + (tavern hero))) -- cgit v1.2.3