Sessions

16 views
Skip to first unread message

Folcon

unread,
Dec 6, 2009, 10:11:44 AM12/6/09
to Compojure
Hi Everyone,
I've been following the tutorials on how to setup sessions with no
luck. I'm just trying to implement a login system with no luck. I was
wondering if anyone could give me some definitive guidelines on how
sessions are setup.

I've been reading:

http://github.com/Ejhfast/Clogger
http://preview.compojure.org/docs/middleware

I hope someone can help.

Thanks!

James Reeves

unread,
Dec 7, 2009, 3:51:49 PM12/7/09
to Compojure
You can use (session :key) to pull a value from the session, and
return (session-assoc :key :value) to set a value in the session. For
example:

(defroutes login
(GET "/"
(form-to [:post "/login"]
(text-field :login)
(password-field :passwd)
(submit-button "Login")))
(POST "/login"
(if-let [user (auth-user (params :user) (params :login))]
[(session-assoc :user user)
(redirect-to "/users")]
(redirect-to "/")))
(GET "/users"
(if (session :user)
"You're logged in"
(redirect-to "/"))))

(decorate login
(with-session :memory))

Note that whilst this should work, I haven't had chance to test it
(not on my usual development machine).

- James

On Dec 6, 3:11 pm, Folcon <fol...@gmail.com> wrote:
> Hi Everyone,
> I've been following the tutorials on how to setup sessions with no
> luck. I'm just trying to implement a login system with no luck. I was
> wondering if anyone could give me some definitive guidelines on how
> sessions are setup.
>
> I've been reading:
>
> http://github.com/Ejhfast/Cloggerhttp://preview.compojure.org/docs/middleware

Nipun

unread,
Dec 17, 2009, 7:29:02 AM12/17/09
to Compojure

> >http://github.com/Ejhfast/Cloggerhttp://preview.compojure.org/docs/mi...


>
> > I hope someone can help.
>
> > Thanks!

I am doing something similar but this doesn't seem to work for me
either, OTOH,
returning a map like {:session <modified session map>, :body ...etc.}
works just fine,
for example, I would have to use:
[302 :session (session-assoc :user user) :Location "/"]
but this doesn't:
(session-assoc :user user)
(redirect-to "/")
Am I missing something here?

Bob Follek

unread,
Dec 17, 2009, 11:37:12 AM12/17/09
to Compojure
On Dec 17, 7:29 am, Nipun <nipunkuma...@gmail.com> wrote:

> for example, I would have to use:
> [302 :session (session-assoc :user user) :Location "/"]
> but this doesn't:
> (session-assoc :user user)
> (redirect-to "/")
> Am I missing something here?

Make sure you have the two forms in a vector:

[ (session-assoc :user user)
(redirect-to "/") ]

If I'm following the Compojure code correctly, (session-assoc) doesn't
change the session when you call it. Instead, it returns a function
that later gets invoked to build the response, and that's when the
session picks up the change. Hence the vector, to make sure the
returned function is part of the response along with the redirect.

Vagif Verdi

unread,
Dec 17, 2009, 6:05:07 PM12/17/09
to Compojure

On Dec 17, 8:37 am, Bob Follek <bfol...@gmail.com> wrote:
> Make sure you have the two forms in a vector:
>
> [ (session-assoc :user user)
>   (redirect-to "/") ]
>

Yes, that's the confusing part. People coming from other frameworks
are accustomed to have read/write access to session from any part of
their code. It is hard to understand why you have to do it in specific
part.
It is an artificial requirement of this specific web framework.

James Reeves

unread,
Dec 17, 2009, 8:24:36 PM12/17/09
to Compojure
On Dec 17, 11:05 pm, Vagif Verdi <vagif.ve...@gmail.com> wrote:
> Yes, that's the confusing part. People coming from other frameworks
> are accustomed to have read/write access to session from any part of
> their code. It is hard to understand why you have to do it in specific
> part.

The original idea was to make session modification functional, so
instead of modifying the session directly, you return a delta of how
you want the session to change along with the response. This approach
does have some benefits, but I'm beginning to think that the
disadvantages outweigh the advantages in this case.

You may know that I plan to factor most of Compojure's functionality
out into independent libraries. Session support will probably be
altered to use side-effect functions and then factored out into a
"ring-sessions" library. So I do plan to change the API eventually.

However, I personally prefer the REST model for constructing web
applications, so sessions (being rather unRESTful) aren't really too
high on my list of priorities. If someone else wants to work on the
session library, I welcome any help.

- James

Reply all
Reply to author
Forward
0 new messages