Skip to content

Instantly share code, notes, and snippets.

@gideondsouza
Last active December 22, 2015 07:48
Show Gist options
  • Save gideondsouza/6440173 to your computer and use it in GitHub Desktop.
Save gideondsouza/6440173 to your computer and use it in GitHub Desktop.
login snippet from cookbook.pod
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