[dancer-users] Tiny problem with sessions in a Dancer2 plugin

Cymon cymon.ML at gmail.com
Fri Oct 4 23:29:55 BST 2013


Hi,
what Dancer2 version are you using?

I think I found a bug in the last master branch.

In it, redirected response are configured with halted status
(Dancer2/Core/Context.pm, line 181. this is the commit:
https://github.com/PerlDancer/Dancer2/commit/74528623aa5ff2738dd319cf70639e0b434a3f20).
Problem is that in the dispatcher halted responses don't trigger the
after_request hook.
Session flush, in App.pm, is configured to happen on an after_request
hook, so I think this is the reason your code fails.

Has anyone an idea about how to solve this? I'll try to think about it
too, but at the moment I have no solution...

Gideon, if you want to go on with your app you can use this dark dark
dark dark magic:

     my $controller = sub { $dsl->session(user =>  "Yippikiayeeeh");
         my $redir = $dsl->redirect("/");
         $dsl->context->response->is_halted(0);
         return $redir;
     };

Obviously is a dirty workaround, I don't know all the side effects of
this.


Cymon
http://perlishscrewdriver.blogspot.it/



Il giorno Fri, 4 Oct 2013 20:55:05 +0530
"Gideon D'souza" <gidisrael at gmail.com> ha scritto:

> I found the source of the problem, and it still leaves me hanging.
> 
> 
> For some hideous reason, if I redirect after making the request for
> the users json data, the session is undef in the main application.
> However, if I don't redirect. Then goto the homepage from the
> browser, it accesses my session! :S
> 
> 
> my $jresp  =
> $browser->get("https://api.github.com/user?access_token=$acc"); my
> $json = decode_json($jresp->decoded_content);
> $dsl->session(github_user =>  $json); #also tested by just setting a
> string instead of json! $dsl->session(github_access_token => $acc);
> return "done";   #This works fine!
> #but if I # return $dsl->redirect("/"); #it doesn't work !
> 
> 
> And worst of all, if I take your code and do this:
> 
>   my $controller = sub { $dsl->session(user =>  "Yippikiayeeeh");
>                       return $dsl->redirect("/");};
> 
> it works! but for me it doesn't me. The session goes undef.
> 
> I tried it with a YAML backend and the default (simple session)
> 
> 
> 
> 
> On Fri, Oct 4, 2013 at 4:14 AM, Cymon <cymon.ML at gmail.com> wrote:
> 
> > I wrote a code similar to yours and it works for me.
> >
> > Here the routes:
> >
> > package Test;
> > use Dancer2;
> > use Dancer2::Plugin::Dummy;
> >
> > get '/' => sub {
> >
> >       if (not defined(session->read('user'))) {
> >         return "No user"
> >       }
> >       else
> >       {
> >           return "User is here"
> >       }
> > };
> >
> > 1;
> >
> > Here the dummy plugin:
> >
> > package Dancer2::Plugin::Dummy;
> > use Dancer2::Plugin;
> >
> > on_plugin_import {
> >      my $dsl = shift;
> >      my $controller = sub { $dsl->session(user =>  "Yippikiayeeeh");
> >                       return "You have a user now!"};
> > $dsl->any(['get'] => '/populate/session', $controller); };
> >
> > register_plugin for_versions => [ 2 ];
> >
> >
> > If I go to populate/session and the on the home I can see user
> > populated.
> >
> > What kind of session manager are you using? Are you sure it's
> > working well?
> >
> > Cymon
> > http://perlishscrewdriver.blogspot.it/
> >
> >
> >
> >
> >
> > Il giorno Fri, 4 Oct 2013 00:36:23 +0530
> > "Gideon D'souza" <gidisrael at gmail.com> ha scritto:
> >
> > > Tried it, It still goes into a redirect loop because the session
> > > is always undef the sub for "/".
> > >
> > > :( I'd really appreciate anyone just peeing into this!
> > >
> > >
> > > On Tue, Oct 1, 2013 at 4:48 AM, Slavik Goltser <slavikg at gmail.com>
> > > wrote:
> > >
> > > > Doesn't that get ignored in Dancer2?
> > > >
> > > >
> > > > On Mon, Sep 30, 2013 at 6:51 PM, Cymon <cymon.ML at gmail.com>
> > > > wrote:
> > > >
> > > >> Hi,
> > > >> i tried your code.
> > > >>
> > > >> Did you remember to add:
> > > >>
> > > >> register_plugin for_versions => [ 2 ];
> > > >>
> > > >> at the end of the plugin?
> > > >> It will not work without this.
> > > >>
> > > >> Cymon
> > > >> http://perlishscrewdriver.blogspot.it/
> > > >>
> > > >> Il giorno Sun, 29 Sep 2013 22:55:05 +0530
> > > >> "Gideon D'souza" <gidisrael at gmail.com> ha scritto:
> > > >>
> > > >> > Thank you very much for your time.
> > > >> >
> > > >> > I tried it. No luck still.
> > > >> >
> > > >> > If I may bother you so:
> > > >> >
> > > >> > on_plugin_import {
> > > >> >      my $dsl = shift;
> > > >> >      my $controller = sub { ...; ...; $dsl->session(user =>
> > > >> > $some_json);} $dsl->any(['get'] => '/your/path',
> > > >> > $controller); }
> > > >> >
> > > >> > in app that uses this plugin:
> > > >> >
> > > >> > get '/' => sub {
> > > >> >
> > > >> >       if (not defined(session->read('user'))) {
> > > >> > ######
> > > >> > #session->Read('user') is always undef!!!
> > > >> > ######
> > > >> >           return redirect auth_github_authenticate_url;
> > > >> >       }
> > > >> > };
> > > >> >
> > > >> > Any clues here?
> > > >> >
> > > >> >
> > > >> > On Sun, Sep 29, 2013 at 6:49 PM, Cymon <cymon.ML at gmail.com>
> > > >> > wrote:
> > > >> >
> > > >> > >
> > > >> > > >
> > > >> > > > Is there a way to add routes inside a plugin, on init or
> > > >> > > > something like that? Or perhaps it's something else I've
> > > >> > > > missed?
> > > >> > > >
> > > >> > > > Any reply or pointers appreciated.
> > > >> > >
> > > >> > > Hi,
> > > >> > > I have no time to try your code now and help you with your
> > > >> > > issue, but I can answer this.
> > > >> > >
> > > >> > > To add routes inside a plugin just define the
> > > >> > > on_plugin_import sub in your plugin package as this.
> > > >> > >
> > > >> > > on_plugin_import {
> > > >> > >      my $dsl = shift;
> > > >> > >      my $controller = sub { ...do things ... }
> > > >> > >      $dsl->any(['get'] => '/your/path', $controller);
> > > >> > > }
> > > >> > >
> > > >> > >
> > > >> > > --
> > > >> > > Cymon
> > > >> > > http://perlishscrewdriver.blogspot.it/
> > > >> > > _______________________________________________
> > > >> > > dancer-users mailing list
> > > >> > > dancer-users at dancer.pm
> > > >> > > http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
> > > >> > >
> > > >>
> > > >>
> > > >> --
> > > >> Cymon
> > > >> Coniglio domina,
> > > >> http://www.therabbit.it
> > > >> _______________________________________________
> > > >> dancer-users mailing list
> > > >> dancer-users at dancer.pm
> > > >> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
> > > >>
> > > >
> > > >
> > > > _______________________________________________
> > > > dancer-users mailing list
> > > > dancer-users at dancer.pm
> > > > http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
> > > >
> > > >
> >
> >
> > --
> > Cymon
> > Coniglio domina,
> > http://www.therabbit.it
> > _______________________________________________
> > dancer-users mailing list
> > dancer-users at dancer.pm
> > http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
> >


-- 
Cymon
Coniglio domina,
http://www.therabbit.it


More information about the dancer-users mailing list