Cookie and session in towerjs

306 views Asked by At

is there some simple example how to use cookies or sessions in towerjs? I read about Connect Middleware, but I have no experience with it (and unfortunately with node.js also).

If examples are not available I will be grateful for any tips too.

Marcin

2

There are 2 answers

2
250R On BEST ANSWER

Cookies

From the TowerJS source code here, the controller has these properties:

  • @request
  • @response
  • @cookies (which is just a shortcut for @request.cookies)
  • @session (which is just a shortcut for @request.session)

Hence to set cookies you can follow express documentation here

For example this set cookie 'rememberme'

# "Remember me" for 15 minutes 
@response.cookie 'rememberme', 'yes', 
  expires: new Date(Date.now() + 900000)
  httpOnly: true

And to get the cookie

@request.cookies.rememberme

Session

As for session, looks like it's just connect in memory session. See source code here https://github.com/viatropos/tower/blob/master/src/tower/server/application.coffee#L39

So you can just use it like:

@request.session.something = 'something'

or

@session.something = 'something'

Ok hope that helps...

1
jsbeckr On

In the Tower.js github repo they are using sessions in the example here. Maybe you can get some help there. I'm not the coffee script guy, so my help is limited. ;)

But this is where they configure session/cookie support:

// config/application.coffee
@use "cookieParser", Tower.config.session.key
@use "session", secret: Tower.config.session.secret, cookie: {domain: Tower.config.session.cookie.domain}

I hope I could help at least a little! ;)