Weave provides a session management system that handles authentication, CSRF protection, and server-sent event (SSE) connections.
Weave's session system uses three cookies to manage user state:
When a user first visits a Weave application:
For authenticated sessions:
weave-auth cookieFor all requests:
x-csrf-token headerWeave tracks active browser connections using a combination of session ID and instance ID:
push-html!)broadcast-html!);; Sign in a user and get the auth cookie string
(weave/set-cookie! (session/sign-in {:name "username" :role "admin"}))
;; Sign out a user by clearing the auth cookie
(weave/set-cookie! (session/sign-out))
set-cookie!The set-cookie! function is a key part of session management in
Weave. The function works by sending JavaScript that sets the
document.cookie value, which updates or creates the specified cookie
in the browser.
;; Basic usage
(weave/set-cookie! "mycookie=value; Path=/; Max-Age=86400")
;; Sign in example
(weave/handler []
(weave/set-cookie!
(session/sign-in {:name "Weave" :role "User"}))
(weave/push-reload!))
;; Sign out example
(weave/handler []
(weave/set-cookie! (session/sign-out))
(weave/push-path! "/sign-in"))
Weave automatically tracks the last activity timestamp for each session instance whenever a handler is called. This enables you to:
(require '[weave.session :as session])
;; Get the last activity timestamp for a specific session instance
(session/last-activity session-id instance-id)
;; => 1672531200000 (timestamp in milliseconds)
;; Get all activity data for a session
;; Returns a map of {instance-id -> timestamp}
(session/session-activity session-id)
;; => {"instance-123" 1672531200000, "instance-456" 1672531150000}
;; Get all session activity data
;; Returns a map of {session-id -> {instance-id -> timestamp}}
(session/session-activities)
;; => {"session-abc" {"instance-123" 1672531200000}
;; "session-def" {"instance-456" 1672531150000}}
;; Get activity data for a specific session
;; Returns a map of {instance-id -> timestamp}
(session/session-activities "session-abc")
;; => {"instance-123" 1672531200000}
When starting a Weave application, you can configure session security:
(weave/run view-fn
{:csrf-secret "your-csrf-secret" ;; Secret for CSRF token generation
:jwt-secret "your-jwt-secret"}) ;; Secret for JWT signing
If not provided, Weave will generate random secrets for each server instance.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |