# Extensions and sessions

**URL:** <https://www.ruby-forum.com/t/extensions-and-sessions/110067>\
**Category:** Radiant CMS\
**Created:** [July 22, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/extensions-and-sessions/110067 "2007-07-22T14:17:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackedder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blackedder](https://www.ruby-forum.com/u/blackedder)\
**Post date:** [July 22, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/extensions-and-sessions/110067/1 "2007-07-22T14:17:23Z")

</div>

I’m creating a Radiant extensions to secure some pages in a website,  
preventing it from opening without a valid login. To achieve this, I  
added a before filter to the site controller and created a special  
LoginPage type. If a user is not logged in and tries to enter a  
secured page, it will be redirected to a page of type LoginPage and  
prompted for an e-mailaddress and password. So far, so good.

The next step is to store the login information in the session, but  
it seems that sessions won’t work. This is my code:

class LoginPage \< Page

description %{  
A login screen for secure pages  
}

attr\_reader :login\_error

@login\_error = false

def process(request, response)  
debugger  
if request.post?  
# If the login is successfull, set the session and redirect to  
the return\_url or homepage  
if response.session[:website\_user\_id] =  
WebsiteUser.authenticate(request.parameters[:login][:email],  
request.parameters[:login][:password])  
response.redirect request.session[:return\_url] || ‘/’  
else  
@login\_error = true  
super(request, response)  
end  
else  
super(request, response)  
end  
end

def cache?  
false  
end

# … Some special login tags …

end

As you can see, it should store the website\_user\_id inside the  
response.session. Unfortunately this doesn’t work.

I enabled session support for the SiteController and tried storing  
other information in the session from several places in the code.  
Even from a normal action inside the SiteController, sessions won’t  
get stored. The weird thing is, that sessions do work once I try to  
login at the backend, but the session information from the backend is  
again not available in the frontend.

I looked through all the Radiant code, disabled some parts (like the  
LoginSystem), but can’t find any cause of the weird session  
behaviour. What am I doing wrong?

The full code of my extension can be found at Google Code: http://  
[code.google.com/p/secure-pages/](http://code.google.com/p/secure-pages/)

Regards,

Edwin Vlieg

---

<div class="post-metadata">

**Author:** ![blackedder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blackedder](https://www.ruby-forum.com/u/blackedder)\
**Post date:** [July 30, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/extensions-and-sessions/110067/2 "2007-07-30T23:47:43Z")

</div>

Edwin V. wrote:

> The next step is to store the login information in the session, but  
> it seems that sessions won’t work.
> 
> The full code of my extension can be found at Google Code: http://  
> [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/secure-pages/)

I’m running from the 0.6.2 gem and doing something similar (see  
[Can Sessions work when Radiant is run from gem? - Radiant CMS - Ruby-Forum](http://www.ruby-forum.com/topic/115871)) but as of right now I’m just  
using a simple module with some tags, hoping to switch to using a  
dedicated page type like you are doing.

Thanks to your secure\_pages\_extension.rb file, this is now working for  
me:

def activate  
Page.class\_eval { include MemberLogin }  
SiteController.class\_eval { session :disabled =\> false }  
end

Previously I was doing the following, which would not work:

SiteController.class\_eval { session :on }

Are your issues because of your environment config? My file has the  
following set:

config.cache\_classes = true  
config.action\_controller.consider\_all\_requests\_local = true  
config.action\_controller.perform\_caching = false  
ResponseCache.defaults[:perform\_caching] = false

Maybe your non-logged-in pages are cached, so you aren’t seeing the  
logged-in versions? This is probably incorrect, as you seem to be having  
trouble saving/reading sessions in general, but its worth mentioning I  
guess.
