Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use Mojolicious::Lite;
- use lib 'lib';
- use MyUsers;
- # Make signed cookies secure
- app->secret('Mojolicious rocks!');
- my $users = MyUsers->new;
- app->helper(users => sub { return $users });
- # Main login action
- any '/' => sub {
- my $self = shift;
- # Query or POST parameters
- my $user = $self->param('user') || '';
- my $pass = $self->param('pass') || '';
- # Check password and render "index.html.ep" if necessary
- return $self->render unless $self->users->check($user, $pass);
- # Store user name in session
- $self->session(user => $user);
- # Store a friendly message for the next page in flash
- $self->flash(message => 'Thanks for logging in!');
- # Redirect to protected page with a 302 response
- $self->redirect_to('protected');
- } => 'index';
- # A protected page auto rendering "protected.html.ep"
- get '/protected' => sub {
- my $self = shift;
- # Redirect to main page with a 302 response if user is not logged in
- return $self->redirect_to('index') unless $self->session('user');
- } => 'protected';
- # Logout action
- get '/logout' => sub {
- my $self = shift;
- # Expire and in turn clear session automatically
- $self->session(expires => 1);
- # Redirect to main page with a 302 response
- $self->redirect_to('index');
- } => 'logout';
- app->start;
- __DATA__
- @@ layouts/default.html.ep
- <!doctype html><html>
- <head><title>Login Manager</title></head>
- <body><%= content %></body>
- </html>
- @@ index.html.ep
- % layout 'default';
- <%= form_for index => begin %>
- <% if (param 'user') { %>
- <b>Wrong name or password, please try again.</b><br>
- <%;
- }
- %>
- Name:<br>
- <%= text_field 'user' %><br>
- Password:<br>
- <%= password_field 'pass' %><br>
- <%= submit_button 'Login' %>
- <% end %>
- @@ protected.html.ep
- % layout 'default';
- <% if (my $message = flash 'message') { %>
- <b><%= $message %></b><br>
- <% } %>
- Welcome <%= session 'user' %>!<br>
- <%= link_to Logout => 'logout' %>
Add Comment
Please, Sign In to add comment