Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ring redirect after login

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

My original code be-all-like

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

Which worked well, but the user still has to navigate elsewhere manually.

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

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

doesn't do anything (aside from returning a blank page).

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

like image 407
sova Avatar asked Mar 07 '15 00:03

sova


2 Answers

response is return a body. you code is (response requri),but the param of the funtion reponse is html body,not a uri,you can use the 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: if you are writing a web site.the lib-noir is a good way to control the session and other.

like image 193
ipaomian Avatar answered Nov 10 '22 14:11

ipaomian


ipaomian has the answer.

Wanted to share a nice redirect hack:

(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"})
like image 27
leontalbot Avatar answered Nov 10 '22 13:11

leontalbot