Last active
December 22, 2015 07:48
-
-
Save gideondsouza/6440173 to your computer and use it in GitHub Desktop.
login snippet from cookbook.pod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Dancer2; | |
use strict; | |
use warnings; | |
set session => "Simple"; | |
hook before => sub { | |
if (!session('user') && request->dispatch_path !~ m{^/login}) { | |
forward '/login', { requested_path => request->dispatch_path }; | |
} | |
}; | |
get '/secret' => sub { | |
return "Top Secret Stuff here"; | |
}; | |
get '/login' => sub { | |
# Display a login page; the original URL they requested is available as | |
# params->{'requested_path'}, so could be put in a hidden field in the form | |
template 'login', { path => params->{'requested_path'} }; | |
}; | |
post '/login' => sub { | |
# Validate the username and password they supplied | |
if (params->{user} eq 'bob' && params->{pass} eq 'letmein') { | |
session user => params->{user}; | |
redirect params->{path} || '/'; | |
} else { | |
redirect '/login?failed=1'; | |
} | |
}; | |
dance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment