Search code examples
clojuremiddlewarenoir

how add authentication in a middleware or pre-route?


how to carry out an authentication process for each request sent. The problem i'm currently facing is i'm not able access user data which is sent as a request param. here is what i have tried

(pre-route[:any "/mainpage/*"] {:keys[data]}
 (when (not(contains? data "userid"))
   //false response
  )
)

and the middleware

(defn for-auth [handler]

  (fn [req]
    (if (contains? (:body (:params req)))
      (handler req)
      (handler (assoc req :body {})
    )
   )
)

and i add the middlware too. but neither of them work.. Any idea to access user params..

Thanks


Solution

  • Ok here is what i have done and it solves my problem..

    (pre-route [:any "/mainroute/*"] {{:keys [jsondata]}:params}
      (let [data (httputil/parse-json-data jsondata)]
    
      (cond
        (not (true? valid_req)) "false"
        (not (true? version_check)) "false"
        (not (true? user_valid)) "false"
        )
       )
      )
    

    here i have parsed my json data in middleware and added it to "params" with key name :jsondata.. it works perfectly..