[Dancer-users] login example using before filter

Joel Roth joelz at pobox.com
Sat Dec 4 07:53:03 CET 2010


I've had some difficulties with the before filter example
provided in Dancer::Introduction.

If we pass the information via 'session' rather than 'var'
and use 'redirect' instead of 'request->path_info'
we get code that more-or-less works as expected:

    before sub {
        if (!session('user') and request->path_info !~ m{^/login}) {
            session requested_path => request->path_info;
            redirect('/login');
        }
    };

    post '/login'    => sub { 
        # authenticate credentials
        session user => params->{user};
        my $path = session->{requested_path};
        session requested_path => undef;
        redirect $path
    };

Here is naive code based on the docs that _doesn't_ work:
           
    before sub {
        if (!session('user') && request->path_info !~ m{^/login}) {
            var requested_path => request->path_info;
            request->path_info('/login');
        }
    };

   post '/login'    => sub {
      # check credentials 
      session user => params->{user};
      my $path = vars->{requested_path};
      redirect $path              # [1]
      # request->path_info($path) # [2]
   };

The first difference is that this 'before' 
filter causes the login page to show the original URL 
instead of /login.

The second difference is that neither of the rerouting
alternatives leads to the desired behavior.  

   [1] results in VOID. 
   [2] results '/initial_requested_path' as display content

I'd be interested in understanding how to fix the failed
implementation. 

And I'm also willing to patch the docs to show a complete,
successful implementation.

Cheers,

Joel

-- 
Joel Roth


More information about the Dancer-users mailing list