How do i use Plack Authentication with Session middleware?

253 Views Asked by At

I am having my own written middleware called Authentication and session where Authentication middleware generates the session key and save the session information in cookie upon successful authentication.

Now using above cookie the Session middleware extract the session information and implements the session management.

But somehow i am not able to serialize the above middlewares, so that i can get the cookies created by Authentication middleware in Session middleware.

I tried to build them as below -

my $app = builder {
    mount "/login" => builder {
       enable "+X::Middleware::Authentication"; #This should be called first
       enable "+X::Middleware::Session";
    };
};

And the Authentication middleware simple validate the user to some database and on success generates the session key and save it in cookie.

The Session middleware looks like as -

package X::Middleware::Session;
use Plack::Session::State;

use parent qw(Plack::Middleware);

use warnings;
use strict;

use Moose;

use Data::Dumper;

sub call {
    my $self = shift;
    my($env) = @_;

    # Expecting the cookie information in $env, but its not there
    my $request = Plack::Request->new($env);
    my $session = $request->session;

    $session->{user} = "some";

    $env->{'psgix.session.options'}{change_id} = 1;
    $env->{'psgix.session.options'}{expires}   = 120;

    my $response = Plack::Response->new();

    print "Welcome to X::Middleware::Session Middleware\n\n\n";

    print "X::Middleware::Session::ENV - " . Dumper($env) . "\n";
    $response->status(200);
    return $response->finalize;
}

1;

Is there any problem i am calling the middleware?

0

There are 0 best solutions below