Giter VIP home page Giter VIP logo

Comments (5)

jacobobryant avatar jacobobryant commented on June 24, 2024 1

Happy new year 🙂

You can do this in "app space" by modifying the default wrap-signed-in middleware to do a DB query:

(defn wrap-signed-in [handler]
  (fn [{:keys [session biff/db] :as ctx}
       user (some->> (:uid session) (biff/lookup db))]
    (if user
      (handler (assoc ctx :user user))
      {:status 303
       :headers {"location" "/signin?error=not-signed-in"}
       :session (dissoc session :uid)})))

As for whether this should be the default implementation: not sure. Assuming your app has a sign-out button somewhere easily accessible, then you can also just click on that after doing rm -r storage right? I believe that'll work in the default example app anyway--the page will still load and the sign-out button will be visible, you just won't see any values in the default crud form because there's no user document.

I think I'd prefer to leave it at that rather than modifying the default middleware to do a DB query on every (authenticated) request. Those who want this behavior can make the decision to do that if they want. As it is, Biff's default middleware stack doesn't do any DB queries (I'm pretty sure?) so I wouldn't be able to add this functionality without introducing a new query somewhere.

from biff.

olavfosse avatar olavfosse commented on June 24, 2024

I hear what you're saying. I still think the better default is to hit the db on every request though.

I've come to use this middleware instead:

(defn wrap-require-user [handler]
  "Ensures the user is logged in and that :user is associated
  with (xt/entity uid). If your handler or middleware requires :user,
  use it explicitly."
  (fn [{:keys [session biff/db] :as ctx}]
    (if-not (some? (:uid session))
      {:status 303
       :headers {"location" "/signin?error=not-signed-in"}}
      (if-let [user (or (:user ctx)
                        (xt/entity db (:uid session)))]
        (handler (assoc ctx :user user))
        {:status 303
         :headers {"location" "/signin?error=not-signed-in"}}))))

from biff.

olavfosse avatar olavfosse commented on June 24, 2024

Btw feel free to close :^), or I can pr if u like the middleware

from biff.

jacobobryant avatar jacobobryant commented on June 24, 2024

I hear what you're saying. I still think the better default is to hit the db on every request though.

I still think it's definitely better not to do that by default 😉 .

Do you want to stick this middleware in a gist or write up a short blog post or something though? I'd be happy to add a link for it in the content library.

from biff.

olavfosse avatar olavfosse commented on June 24, 2024

I think it's fine that it lives here for now :)

from biff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.