dagolden / Dancer-Session-Cookie

Dancer session engine based on encrypted cookies
2 stars 2 forks source link

Cookie is not saved in hook before #6

Closed knutov closed 11 years ago

knutov commented 11 years ago
use Data::Dumper;

get '/b' => sub {
    return session('abc');
};

get '/c' => sub {
    return Dumper(session);
};

hook 'before' => sub { 
    my $a = request->path_info; 
    if ( not request->path_info =~ m{^/[a|b|c]} ){
        session abc => $a  ;
        return redirect '/b';
    }
};

get /XXX returns redirect to /b get /b returns '' (empty string, instead of '/XXX') get /c returns $VAR1 = bless( { 'id' => '24803856430730319597546959455691532' }, 'Dancer::Session::Cookie' );

Looks like session is not stored in case of redirect in hook 'before'. But everything ok in this place if downgrade to 0.15.

Additionally, if modify source code

# Copied from Dancer::Session::Abstract::write_session_id and
# refactored for testing
hook 'after' => sub {
    my $response = shift;
    use Data::Dumper;
    die Dumper $SESSION;

you can see the session is empty.

knutov commented 11 years ago

But in this example everything saved correctly. Looks like it can be problem of Dancer, not sure.

get '/b' => sub {
    return session('abc');
};

hook 'before' => sub { 
    my $a = request->path_info;
    if ( not request->path_info =~ m{^/[a|b|c|save]} ){
        session abc => $a  ;
        return redirect '/save?a='.$a;
    }
};

get '/save' => sub {
    session abc => param "a";
    return redirect '/b';
};
dagolden commented 11 years ago

I see. It's a problem in how Dancer hooks interact.

Now that D1 is maintained maybe I can patch it for better session cookie support.

David On May 5, 2013 12:49 PM, "Nick S. Knutov" notifications@github.com wrote:

But in this example everything saved correctly. Looks like it can be problem of Dancer, not sure.

get '/b' => sub { return session('abc');}; hook 'before' => sub { my $a = request->path_info; if ( not request->path_info =~ m{^/[a|b|c|save]} ){ session abc => $a ; return redirect '/save?a='.$a; }}; get '/save' => sub { session abc => param "a"; return redirect '/b';};

— Reply to this email directly or view it on GitHubhttps://github.com/dagolden/Dancer-Session-Cookie/issues/6#issuecomment-17454596 .

dagolden commented 11 years ago

I've sent a Dancer pull request that should allow this to be fixed: https://github.com/PerlDancer/Dancer/pull/921

knutov commented 11 years ago

Big thanks, now everything works.

Please merge pull request https://github.com/dagolden/Dancer-Session-Cookie/pull/7 with test for this case.

dagolden commented 11 years ago

Released new version to CPAN. Thank you for your help.