Monks, I'm learning Perl Dancer and have been trying to get a simple login system working. I've gone over the example given in the Dancer::Cookbook and modified it slightly. I'm experiencing an issue that I hope you can help me with. Below is my code.

package pmtt; use Dancer ':syntax'; our $VERSION = '0.1'; set session => 'YAML'; hook 'before' => sub { if (! session('user') && request->path_info !~ m{^/login}) { var requested_path => request->path_info; request->path_info('/login'); } }; get '/' => sub { template 'index'; }; get '/login' => sub { # Display a login page; the original URL they requested is availab +le as # vars->{requested_path}, so could be put in a hidden field in the + form template 'login', { path => vars->{requested_path} }; }; post '/login' => sub { # Validate the username and password they supplied if (params->{username} eq 'monktopher' && params->{password} eq 'p +assw0rd') { session user => params->{username}; redirect params->{path} || '/'; } else { redirect '/login?failed=1'; } }; any ['get','post'] => '/projects' => sub { return "Projects. Yay."; }; any ['get','post'] => '/logout' => sub { session->destroy(); redirect '/'; }; true;

The before hook is being run before all requests except that for /login and checks to see whether or not a user session exists. What I'd like to do is give non-users access to certain routes, much like what is being done for /login. I tried changing the regex to m{^/login|/} thinking that the session check would be bypassed when http://blahblah.com/ was requested. Before changing the regex, the login system would work as expected, I couldn't get to /project unless I already had a session. After changing the regex to include /, I can somehow access the /projects path, even before logging in.

Can anyone see what would be causing this issue? I've already spent a good 2 hours going over the Dancer documents in case I missed something, but haven't had any luck.

Thanks

In reply to Dancer sessions and before hook by monktopher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.