Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Last active August 29, 2015 14:18
Show Gist options
  • Save Logioniz/bdf6f22c00fc51798c43 to your computer and use it in GitHub Desktop.
Save Logioniz/bdf6f22c00fc51798c43 to your computer and use it in GitHub Desktop.
Minimal example login, session
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Mojolicious::Lite;
get '/login' => sub { shift->render } => 'login';
post '/verify' => sub {
my $c = shift;
my $name = $c->param('name') // '';
my $pass = $c->param('pass') // '';
# to verify that user have correct pass
my $users_info = {todd => {pass => 'qwerty', group => 'super user'}};
if (($users_info->{$name}{pass} // '') eq $pass) {
my $group = $users_info->{$name}{group};
$c->session(user => $name, group => $group);
} else {
$c->flash(msg => 'invalid pair (user, pass)');
return $c->redirect_to('login');
}
$c->redirect_to('foo')
} => 'verify';
# check, next routes will go through this function (this is feature of Mojolicious::Lite)
under '/myapp' => sub {
my $c = shift;
my $user = $c->session('user');
return 1 if defined $user;
$c->redirect_to('login');
return 0;
};
get '/foo' => sub { shift->render } => 'foo';
app->start;
__DATA__
@@ login.html.ep
<form action="<%= url_for('verify') %>" method="POST">
% if (flash('msg')) {
<label><%= flash('msg') %></label><br>
% }
<input type="text" value="" placeholder="name" name="name">
<input type="text" value="" placeholder="pass" name="pass">
<input type="submit">
</form>
@@ foo.html.ep
%= session('user')
<br>
%= session('group')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment