[Dancer-users] Selectively calling before() from route handlers

Mike Schroeder mike at donor.com
Fri Dec 17 16:46:47 CET 2010


Another pattern you can use is to put all the authenitcation related routes
into your app.pm, and put all the routes which require authentication into a
different module (eg secure.pm).  For secure.pm you have "before" defined to
to do your auth check.  I end up with something like:

load_app 'secure';
before \&detect; # No secure behavior here
get '/' => sub {
   redirect '/menu';
};

and then over in secure.pm:

before \&secure_and_detect;
get  '/menu' => sub {
   template 'mainmenu';
};

For completeness, here are the subs for detect() and secure_and_detect()

sub detect {
   my $b = HTTP::BrowserDetect->new( request->user_agent );
   if    ( $b->iphone         ) { var mylayout => 'iphone';     }
   elsif ( $b->android        ) { var mylayout => 'android';    }
   elsif ( $b->blackberry     ) { var mylayout => 'blackberry'; }
   elsif ( $b->ipad           ) { var mylayout => 'ipad';       }
   elsif ( $b->ipod           ) { var mylayout => 'iphone';     }
   elsif ( $b->mobile         ) { var mylayout => 'mobile';     }
   elsif ( $b->ie             ) { var mylayout => 'ie';         }
   else                         { var mylayout => 'main';       }
}

sub secure_and_detect {
   detect();
   if ( ! session('username') && request->path_info !~ m{^/login}) {
      var requested_path => request->path_info;
      request->path_info('/login');
   }
}

That lets me keep the two behaviors separate both in my head and in the code
to avoid future confusion.

Hope that helps.

Mike.


On Fri, Dec 17, 2010 at 6:44 AM, Gurunandan Bhat <gbhat at pobox.com> wrote:

>
>
> On Fri, Dec 17, 2010 at 5:09 PM, David Precious <davidp at preshweb.co.uk>wrote:
>
>> On Friday 17 December 2010 06:31:33 Gurunandan Bhat wrote:
>> > Hi,
>> >
>> > Is there a way I can set whether a particular route handler calls
>> before().
>> > I am looking to implement authenticated/unauthenticated routes and would
>> > like to execute before() only if the route requires authentication. Any
>> > suggestions in doing something like this would be great.
>>
>>
>> before handlers are called before all requests, but you don't have to do
>> anything if the request isn't one you're interested in.
>>
>> e.g.
>>
>>    before sub {
>>       return if request->path_info !~ m{^/private/};
>>       # Authentication stuff here
>>    };
>> Is that any use?
>>
>
> Yes, in effect, currently this is how I am doing it. What I have done now
> is to put the yaml config which I search in before() to check if I need to
> run at all.
>
>
>> Of course, that means the list of which routes require authentication is
>> kept
>> separately to the route declarations themselves, which might not be what
>> you
>> want.
>>
>>
> Indeed. Thats what I am looking for: Ideally some syntactic sugar that I
> sweeten my route handler with:
>
> *run before => true;*
>
> Dancer does spoil you, doesn't it :)
>
> Thank you.
>
>
>
>>
>>
>> Cheers
>>
>> Dave P
>>
>> _______________________________________________
>> Dancer-users mailing list
>> Dancer-users at perldancer.org
>> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>>
>
>
> _______________________________________________
> Dancer-users mailing list
> Dancer-users at perldancer.org
> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20101217/14664463/attachment.htm>


More information about the Dancer-users mailing list