Here is the full code:
#!/usr/bin/env perl
use Mojolicious::Lite;
use lib 'lib';
use MyUsers;
app->secrets(['Mojolicious rocks']);
# Helper to lazy initialize and store our model object
helper users => sub { state $users = MyUsers=>new };
any '/' => sub {
my $c = shift;
# Querry parameters
my $user = $c->param('user') || '';
my $pass = $c->param('pass') || '';
# check password
return $c->render unless $c->users->check($user, $pass);
# Store username in session
$c->session(user => $user);
$c->flash(message => 'Thanks for logging in.');
# redirect to protected page with 302 respons
$c->redirect_to('protected');
} => 'index';
group {
under sub {
my $c = shift;
# redirect to main with 302 response if not logged in
return 1 if $c->session('user');
$c->redirect_to('index');
return undef;
};
#protected page
get '/protected';
};
#Logout
get '/logout' => sub {
my $c = shift;
$c->session(expires => 1);
#redirect to main with 302
$c->redirect_to('index');
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
%= form_for index => begin
% if (param 'user' ) {
<b>Wrong name or password, plea try againm.</b><b>
% }
Name:<br>
%= text_field 'user'
<br>Password:<br>
%= submit_button 'Login'
% end
@@ protected.html.ep
% layout 'default';
% if (my $msg = flash 'message' ) {
<b><%= $msg %></b><br>
% }
Welcome <%= session 'user' %>.<br>
%= link_to Logout => 'logout'
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title>Login Manager</title></head>
<body><%= content %></body>
</html>
On Saturday, August 2, 2014 5:09:22 PM UTC-7, sri wrote:
>
> Thanks for the insight, that was really helpful.
>>
>
> Please be nice, passive-aggressive beheavior is not welcome here.
>
> --
> sebastian
>
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.