Dear James, et al.,
Here is what I'm trying to do:
-----------------------------------
;;Login form to login. To login successfully, let
;;password = secret
(defn login-view []
(html
[:p
(form-to [:post "/login/" ]
"User name: "
[:input {:name "name", :type "text"}]
[:br]
"Password: "
[:input {:name "password", :type "password"}]
[:br]
[:input {:type "submit" :value "Log in"}])]))
;;takes the input params from the login form and stores the
;;logged in user into a session. You get logged in when the
;;password value is = secret
(defn login-controller [params session]
(do (println (params "password") (params "name")))
(dosync
(if
(= "secret" (params "password"))
; Username can include letters, numbers,
; spaces, underscores, and hyphens.
;(.matches (params :name) "[\\w\\s\\-]+"))
(do
{:session (assoc session :name (params "name"))}
(redirect "/page/"))
(redirect "/oops/"))))
;;In the repl, the following appears:
;;
;;Getting userid: nil
;;
(defn page [request session title body]
(do
(println "Getting userid:" (:name session))
)
)
;; I'm using compojure's defroutes (as you can see)
(defroutes main-routes
(GET "/login/" [] (login-view))
(POST "/login/" {params :params session :session} (login-controller
params session))
(ANY "/page/" {request :request session :session} (page request
session "hello" "world"))
)
-------------------------------
PROBLEM: I can't retrieve the session ":name" that I've been attempted
to store to. I don't know if it's getting stored into the session in
the first place. And finally, is it that I'm unable to retrieve it. I
know it's probably a very minor problem that needs to be fixed, but
I've been going up the wall on this one!!!!!!!!!!!!!
Thanks in advance,
shree
ps. My code may seem a bit contrived. It's been bundled together off
of a bunch of examples I've seen at the ring, compojure, sandbar
examples, and elsewhere...
On May 11, 8:01 pm, James Reeves <[email protected]> wrote:
> On 12 May 2011 00:04, Shree Mulay <[email protected]> wrote:
>
> > From this code, I can't figure out where you "instantiate" the session
> > var, store to it, and read back from it? I know there's not
> > instantiation in clojure(???). I hope your able to get what I'm having
> > trouble figuring out.
>
> The session is read from a :session key on the request map, and
> written using a :session key on the response map.
>
> For example, this route returns the value of the session key :x
>
> (GET "/get" {session :session}
> (str "x = " (:x session)))
>
> It's equivalent to the Sinatra route:
>
> get "/get" do
> "x = #{session[:x]}"
> end
>
> The main difference is that the session has to be bound explicitly. We
> don't just get a magic session map hanging around in the background.
>
> Writing a session is a little trickier:
>
> (GET "/set" [x :as {session :session}]
> {:session (assoc session :x x)
> :body (str "You set x = " x)})
>
> The equivalent Sinatra route is:
>
> get "/set" do |x|
> session[:x] = x
> "You set x = #{x}"
> end
>
> Ring sessions are a little unwieldy because you write by changing the
> response, rather than calling a side-effectful function. Sandbar takes
> a slightly different approach that looks more like the Sinatra code:
>
> (GET "/get" []
> (str "x = " (session-get :x)))
>
> (GET "/set" [x]
> (session-put! :x x)
> (str "You set x = " x))
>
> Sandbar is a little cleaner for simple things, but Ring sessions have
> the advantage of working well with middleware. Idiomatic Ring tends to
> use a lot of middleware, far more than one might use in Rack (for
> instance).
>
> - James
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en