Ring redirection after login

(ns ...
  (:require  [ring.util.response :refer [ response redirect]))

My original code will look like everything

(-> (response "You are now logged in! communist party time!")
    (assoc :session new-session)
    (assoc :headers {"Content-Type" "text/html"}))

Which worked fine, but the user still has to move to another location manually.

Trying to use http://ring-clojure.imtqy.com/ring/ring.util.response.html#var-redirect

(-> (redirect requri)
    (assoc :session new-session)
    (assoc :headers {"Content-Type" "text/html"}))

does nothing (except returning a blank page).

How can I achieve a redirect to a known URI using a ring?

+5
source share
3 answers

responsereturn the body. (response requri)code (response requri), but the parameter reponseis html body, not uri, you can use this function

like this

(ns foo
   (:require [ring.util.response :as response]))
(def requi "/")
(-> (response/redirect requri)
    (assoc :session new-session)
    (assoc :headers {"Content-Type" "text/html"}))

ps: -. lib-noir - .

+6

ipaomian .

:

(ns foo
   (:require [ring.util.response :as response]))

(defn redirect
  "Like ring.util.response/redirect but also accepts key value pairs
   to assoc to response."
  [url & kvs]
  (let [resp (response/redirect url)] 
    (if kvs (apply assoc resp kvs) resp)))

(redirect "/" :session new-session :headers {"Content-Type" "text/html"})
+1

ipaomian is right, however mine worked by removing headers. This is my code:

(:require
    [ring.util.response :refer [redirect]])

(defn set-user! [id {session :session}]
  (-> (redirect "/home")
      (assoc :session (assoc session :user id))))
0
source

All Articles