2

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?

CodeQuestor
  • 881
  • 1
  • 11
  • 25
  • I have got the answer for this problem as i am suppose to return the %env hash through Authentication middleware which will then modify/access this hash and the response to the app. – CodeQuestor Apr 09 '15 at 07:08

0 Answers0