I am trying to use database sessions with Mojolicious instead of the builtin ones that are working with signed cookies.
In the startup subroutine I have something like:
my $dbh = DBI->connect(                                                                                                                                  
                        $config->{database}->{dsn},                                                                                                      
                        $config->{database}->{user},                                                                                                     
                        $config->{database}->{password},
                      );
my $session = MojoX::Session->new(
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',                # this is by default
    ip_match  => 1
);
(ref($self))->attr( 'session' => sub {                
                return $session;
                } );
And I want to use the session object like $self->session or $self->app->session in controllers. 
Unfortunately it doesn't work - it uses previous sessions (from different browsers).
This drives me crazy - all I tried today was to make this work - I've read all the documentation available, also the source of MojoX::Session and all its related classes, tried in about 923847293847239847 ways to make it work, but nothing seems to do it.
PS: I created the session table in the db.
Do you know what I should do in order to be able to use DB sessions with Mojolicious?
You can connect MojoX::Session to the application as a plugin in a startup function.
use Mojolicious::Plugin::Session;
[...]
sub startup {
  my $self = shift;
  [...]
  $self->plugin( session => {
    stash_key => 'mojox-session',
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',
    ip_match  => 1
  });
  [...]
Afterwards, you'll have access to session through stash key 'mojox-session' in controllers. 
For example:
$self->stash('mojox-session')->data('something');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With