I've been googling around for an example on how to use Snap session. Nothing so far. Can someon please point me to an article or something with a simple example of a cookie based session?
A code snippet would be very appriciated. Something like putting a username in the session and checking for its existence and validity before every request. I looked at Snap.extension.session but I am still too noob to figure out how to use it just from the library code.
I understand that setInSession is the function to put the session value in the cookie but I cant quite figure out how to use it from the signature:
setInSession k v = Map.insert k v `liftM` getSession >>= setSession
or how to glu the whole thing together.
I dont know if it is against the rules to ask for a bit of example code but I would not do it if I could find anything on the net.
Thank you in advance.
--- UPDATE ---
I followed the example below and the links but things are still not comming out right.
I just want a simple test that reads a key and a value and puts it in session cookie then I want to read the session value back and display.
So I initialized my session and put it in the main application state.
s <- nestSnaplet "" sess $ initCookieSessionManager "config/site_key.txt" "sess" (Just 3600)
The first confusion is the key that session initialization fanction needs. The key is not created during the initial project setup.
So how do I create it? Where is the key supposed to go in the project file structure so the function can find it?
Then the handlers:
putCookie :: Handler App App ()
putCookie = do
par <- getPostParams
let k = T.decodeUtf8 $ head (par ! (B.pack "key"))
let v = T.decodeUtf8 $ head (par ! (B.pack "value"))
with sess $ setInSession k v
heistLocal (bindString "message" "Cookie inserted!") $ render "reply"
This one just reads the key and the value from a web form and it is supposed to put the key value pair in the cookie. Well, apparently it does not.
shCookie :: Handler App App ()
shCookie = do
v <- with sess $ getFromSession "key"
heistLocal (bindString "message" (fromMaybe "" v)) $ render "reply"
This one just reads it back and displays. I always get an empty string which means there is no such thing.
Does any of the above look incorrect? Where do I make a mistake? It all compiles correct but the session cookie is just not there.
Does Snap.extension.session
means this module? AFAIK the snap-auth
package is deprecated.
You can use new session api implemented as snaplet.
Check out an example how to setup your application to use session and auth snaplets. Then you will be able to store data in session:
data App = App
{ _heist :: Snaplet (Heist App)
, _sess :: Snaplet SessionManager
, _auth :: Snaplet (AuthManager App)
}
makeLens ''App
myHandler :: Handler App App ()
myHandler = do
with sess $ setInSession "key" "value"
ADDED:
File with site key will be created automatically. The function initCookieSessionManager
(defined here) uses getKey
function from clientsession
package (see here). From the docs: If the file does not exist or is corrupted a random key will be generated and stored in that file.
I think your handler doesn't works cos you didn't commit session (see commitSession
here) You can commit it in every handler that updates session, or use wrapHandlers
and/or withSession
to do it automatically.