This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-26
Channels
- # architecture (2)
- # beginners (310)
- # boot (34)
- # cider (50)
- # cljs-dev (82)
- # cljsrn (1)
- # clojure (125)
- # clojure-dusseldorf (1)
- # clojure-hamburg (1)
- # clojure-italy (47)
- # clojure-russia (21)
- # clojure-spec (38)
- # clojure-uk (36)
- # clojurescript (200)
- # community-development (21)
- # cursive (10)
- # datomic (15)
- # duct (58)
- # emacs (20)
- # fulcro (10)
- # funcool (1)
- # graphql (2)
- # hoplon (6)
- # jobs (1)
- # lumo (12)
- # mount (20)
- # off-topic (14)
- # om (5)
- # portkey (43)
- # protorepl (2)
- # re-frame (31)
- # reagent (36)
- # ring (17)
- # ring-swagger (6)
- # shadow-cljs (50)
- # spacemacs (9)
- # sql (5)
- # tools-deps (28)
- # uncomplicate (4)
- # unrepl (5)
- # vim (2)
- # yada (2)
How can I apply a middleware only on certain route? For example GET /userinfo
will need to authenticate a user first, whereas GET /somethingelse
won't require an access token at all.
@hawari.rahman17 many routing libs support this out-of-the-box, some samples: Compojure-api (macros):
(context "/api" []
:middleware [wrap-api]
(GET "/userinfo" []
:middleware [wrap-auth]
(ok ...))
(GET "/somethingelse" []
(ok ...)))
Reitit (data):
["/api" {:middleware [wrap-api]}
["/userinfo" {:get {:middleware [wrap-require-auth]
:handler ...}}]
["/somethingelse" {:get ...}]]
@ikitommi I've found that routes
in compojure is quite extendable, I ended up doing something like this:
(def restricted-routes
(-> (routes (GET "/protected-path" [] protected-fn))
(wrap-routes authorize)))
(defroutes app-routes
(GET "/unprotected-routes" [] "Hello")
restricted-routes)
My problem is sometimes in the same context, there exists a route that doesn't need to be authorized first
I was planning to add another routes later on
yes, but that’s to way to do it with Compojure. Compojure-api just adds sugar for it, e.g. the :middleware
key.
if you are using nginx-clojure, you should be carefull with wrap-routes
. It uses mutable request maps and the wrap-routes
doesn’t work with it.
I mean, I'm not using nginx-clojure
but I'd like to know what I'd signed up for with wrap-routes
e.g. wrap-routes
adds “call me if the path matches” info to the request. As the request is mutable, the info is still present in the request for the next routes. Next route will see that and invoke the mw. Kinda fatal.