Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs/tutorials): setting cookies on GET requests #4

Closed
wants to merge 1 commit into from

Conversation

mjholub
Copy link

@mjholub mjholub commented Aug 22, 2024

This was something I struggled with a little before I had my 'aha' moment with the hidden form, so I figured out that writing such tutorial could be useful for other biff users.
Please note that the implementation I've used in my actual project is somewhat less context free with a little bit more hardcoding, since I've only needed to set one cookie so far hehe.

@jacobobryant
Copy link
Owner

Hey, thanks for taking the time to write this up! There's a simpler way to do this, though. Request handlers are allowed to return Rum as a convenience, but you can still return a regular ring response map if you want. For example, I modified the landing page in the starter app so it sets a cookie when you request the page (via get request):

diff --git a/starter/src/com/example/home.clj b/starter/src/com/example/home.clj
index c19235d..ec1a319 100644
--- a/starter/src/com/example/home.clj
+++ b/starter/src/com/example/home.clj
@@ -12,7 +12,7 @@
    "Until you add API keys for MailerSend and reCAPTCHA, we'll print your sign-up "
    "link to the console. See config.edn."])
 
-(defn home-page [{:keys [recaptcha/site-key params] :as ctx}]
+(defn home-page* [{:keys [recaptcha/site-key params] :as ctx}]
   (ui/page
    (assoc ctx ::ui/recaptcha true)
    (biff/form
@@ -51,6 +51,14 @@
     biff/recaptcha-disclosure
     email-disabled-notice)))
 
+(defn home-page [{:keys [cookies] :as ctx}]
+  (prn cookies)
+  {:status 200
+   :headers {"content-type" "text/html"}
+   :body (rum/render-static-markup (home-page* ctx))
+   :cookies {"foo" {:value "bar"
+                    :max-age (* 365 86400)}}})
+
 (defn link-sent [{:keys [params] :as ctx}]
   (ui/page
    ctx

After going to the landing page, you can type document.cookie in the browser console and you get "foo=bar". If you load the page a second time, you also can see {"ring-session" {:value "..."}, "foo" {:value "bar"}} in the terminal.

Would it be helpful if I updated the starter app so that in app.clj there's an example of writing a get request handler that returns a regular response map instead of relying on biff's middleware to turn the rum response into a map? This kind of question (how do you modify the session/cookies from a regular request handler) has come up a few times, so there probably should be some sort of prominent example/documentation.

@mjholub
Copy link
Author

mjholub commented Aug 25, 2024

Would it be helpful if I updated the starter app so that in app.clj there's an example of writing a get request handler that returns a regular response map instead of relying on biff's middleware to turn the rum response into a map?

Yes, that'd be quite helpful I think. Had some issues trying to force how the response is parsed.

@jacobobryant
Copy link
Owner

Sounds good--I'll close this PR, and I've got an issue here for updating the starter app: jacobobryant/biff#225. I'll get to that at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants