[Dancer-users] Custom Session Handler issues

Al calyx2011 at gmail.com
Thu May 19 19:11:08 CEST 2011


> On 19 May 2011 05:44, Al <calyx2011 at gmail.com> wrote:
>>
>> On 18 May 2011 16:56, David Precious <davidp at preshweb.co.uk> wrote:
>>
>> > The correct answer I should give is of course "Use Dancer's own session
>> > support, which works", but I understand you're trying to maintain
>> > compatibility with other non-Dancer apps which use CGI::Session.
>>
>> I looked into using CGI::Session because I don't like the way Dancer's
>> session engine creates a session/cookie for every client
>> unconditionally. In the end I built a custom engine based on
>> Dancer::Session::Abstract.
>>

On 19 May 2011 08:43, damien krotkine <dkrotkine at gmail.com> wrote:
> Hi Al,
>
> Is the code somewhere on github ?


I uploaded to pastebin. It is quite specific to my particular needs so
not really suitable for distributing as modules on github. I'm sure it
can be adapted without too much trouble, and might help you to write
your own. For example, I like to cache the session id (sid) in the
users table in my database. You could simply remove all the database
specific parts from Abstract.pm and adapt it to your needs.

MyApp::Session::Abstract - http://pastebin.com/RfUzQQi3
MyApp::Session::Storable - http://pastebin.com/EKnrs1mA


Synopsis:

use MyApp::Session::Storable;

post '/login' => sub {
    # authenticate user
    if ($authenticated) {
        my $session = MyApp::Session::Storable->new(username =>
params{username});
    }
}

before_sub {
    my $session = MyApp::Session::Storable->get_session;
    if (defined $session) {
        var session => $session;
    }
}

# In each route handler
my $session = vars->{session};
if ($session) {
    ...
}

# The session object is a hashref, easy to add session data to it
$session->{lastvisit} = localtime;

# Flush it
$session->store;

# Delete it
$session->delete;


More information about the Dancer-users mailing list