/
users.clj
30 lines (26 loc) · 1.24 KB
/
users.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns appengine-clj.users
(:import
(com.google.appengine.api.users User UserService UserServiceFactory)))
(defn user-info
"With no arguments, returns a UserService and User for the current request in a map keyed by :user-service and :user respectively.
If the user is not logged in, :user will be nil.
With a single map argument, a Ring request, returns the user-info map associated with the request by wrap-with-user-info."
([]
(let [user-service (UserServiceFactory/getUserService)]
{:user (.getCurrentUser user-service) :user-service user-service}))
([request]
(:appengine-clj/user-info request)))
(defn wrap-with-user-info
"Ring middleware method that wraps an application so that every request will have
a user-info map assoc'd to the request under the key :appengine-clj/user-info."
[application]
(fn [request]
(application (assoc request :appengine-clj/user-info (user-info)))))
(defn wrap-requiring-login
([application] (wrap-requiring-login application "/"))
([application url]
(fn [request]
(let [{:keys [user-service]} (user-info request)]
(if (.isUserLoggedIn user-service)
(application request)
{:status 302 :headers {"Location" (.createLoginURL user-service url)}})))))