# SessionTimeout plugin doesn't work w/other before\_filters?

**URL:** <https://www.ruby-forum.com/t/sessiontimeout-plugin-doesnt-work-w-other-before-filters/86379>\
**Category:** Rails\
**Created:** [February 21, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/sessiontimeout-plugin-doesnt-work-w-other-before-filters/86379 "2007-02-21T21:07:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 21, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/sessiontimeout-plugin-doesnt-work-w-other-before-filters/86379/1 "2007-02-21T21:07:25Z")

</div>

All,

I’ve installed Luke R.'s SessionTimeout plugin and am trying to  
make it work.

This is what I’ve put into my application.rb file:

session\_times\_out\_in 30.seconds, :after\_timeout =\>  
:handle\_session\_timeout, :except =\> :handle\_session\_timeout

But I also have a before\_filter named :check\_authentication. Even  
though I do a “return false” from the handle\_session\_timeout method, it  
still keeps trying to call check\_authentication.

I looked at the SessionTimeout code and I was under the impression that  
it prepended itself to the before\_filter chain. So returning false from  
the session timeout handler should cause the filter chain to stop  
executing, correct?

Anyone know what’s going on here?

Here’s my handle\_session\_timeout method:

def handle\_session\_timeout  
puts “Running handle\_session\_timeout”  
return false  
end

and what I believe to be the relevant code from SessionTimeout:

module ClassMethods  
def session\_times\_out\_in(time, opts={})  
opts[:after\_timeout] = opts[:after\_timeout] || nil  
class\_eval { prepend\_before\_filter Proc.new { |c|  
c.check\_session\_expiry(time, opts)}, :except =\> opts[:except] }  
end  
end

Am I misunderstanding what prepend\_before\_filter does?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 21, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/sessiontimeout-plugin-doesnt-work-w-other-before-filters/86379/2 "2007-02-21T21:25:42Z")

</div>

I think I see what’s going on here.  
My :after\_timeout callback is not a before\_filter. The before\_filter  
that SessionTimeout sets up is a Proc that in turn calls my method.  
This is in the body of the method that gets called as a before\_filter…

if session\_has\_timed\_out?  
logger.info “::: Session has expired, resetting session.”  
reset\_session  
unless opts[:after\_timeout].nil?  
logger.info “::: Calling after timeout callback”  
opts[:after\_timeout].call(self) if  
opts[:after\_timeout].instance\_of?(Proc)  
self.send(opts[:after\_timeout]) if  
opts[:after\_timeout].instance\_of?(Symbol)  
return  
end  
else  
logger.info “::: Session has not expired. Reinitialising.”  
initialize\_session\_expiry(time)  
end

I think if I just change this code so that it returns the value of  
opts[:after\_timeout], then it should work.

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 21, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/sessiontimeout-plugin-doesnt-work-w-other-before-filters/86379/3 "2007-02-21T21:32:34Z")

</div>

Yup, that was it. By making the method that the Proc calls  
(check\_session\_expiry) return the value of the callback, the callback  
can then halt the filter processing if that is appropriate.

See updated code below:

module ClassMethods  
def session\_times\_out\_in(time, opts={})  
opts[:after\_timeout] = opts[:after\_timeout] || nil  
class\_eval { prepend\_before\_filter Proc.new { |c|  
c.check\_session\_expiry(time, opts)}, :except =\> opts[:except] }  
end  
end

def check\_session\_expiry(time, opts)  
logger.info “::: Checking session expiry”  
if session[:expires\_at]  
if session\_has\_timed\_out?  
logger.info “::: Session has expired, resetting session.”  
reset\_session  
unless opts[:after\_timeout].nil?  
logger.info “::: Calling after timeout callback”

#WG - bug fix so that callback method can halt filter processing.

# opts[:after\_timeout].call(self) if

opts[:after\_timeout].instance\_of?(Proc)

# self.send(opts[:after\_timeout]) if

opts[:after\_timeout].instance\_of?(Symbol)

# return

```
      return opts[:after_timeout].call(self) if 

```

opts[:after\_timeout].instance\_of?(Proc)  
return self.send(opts[:after\_timeout]) if  
opts[:after\_timeout].instance\_of?(Symbol)  
end  
else  
logger.info “::: Session has not expired. Reinitialising.”  
initialize\_session\_expiry(time)  
end  
else  
“::: Session expiry not initialized”  
initialize\_session\_expiry(time)  
end  
end

WG
