DEV Community

Cover image for Clojure Is Awesome!!! [PART 4]
André Borba
André Borba

Posted on • Edited on

1

Clojure Is Awesome!!! [PART 4]

(ns monostate)

(def ^:private session-state
  (atom {:user-id nil
         :permissions #{}
         :last-access nil}))

(defn start-session
  "Starts a new user session with ID and permissions."
  [user-id permissions]
  (reset! session-state {:user-id user-id
                         :permissions permissions
                         :last-access (java.time.Instant/now)}))

(defn end-session
  "Ends the session, resetting the state to default values."
  []
  (reset! session-state {:user-id nil
                         :permissions #{}
                         :last-access nil}))

(defn update-last-access
  "Updates the last access timestamp to the current time."
  []
  (swap! session-state assoc :last-access (java.time.Instant/now)))

(defn get-session
  "Retrieves the complete state of the current session."
  []
  @session-state)

(defn has-permission?
  "Checks if the user has a specific permission."
  [permission]
  (contains? (:permissions @session-state) permission))

(comment
  (start-session "user-123" #{"read" "write"})
  ;; => {:user-id "user-123", :permissions #{"read" "write"}, :last-access <timestamp>}

  (has-permission? "read") ;; => true
  (has-permission? "delete") ;; => false

  (update-last-access)

  (get-session)
  ;; => {:user-id "user-123", :permissions #{"read" "write"}, :last-access <new-timestamp>}

  (end-session)
  ;; => {:user-id nil, :permissions #{}, :last-access nil}
)
Enter fullscreen mode Exit fullscreen mode

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

Pieces AI Productivity Summit

​Join top AI leaders, devs, & enthusiasts for expert talks, live demos, and panels on how AI is reshaping developer productivity at the Pieces AI Productivity Summit.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️