Commit 74afaa19 by PotatoGim

mojolicious: completed Mojolicious::Guides::Growing.

parent dcb68c6a
package MyApp;
use Mojo::Base 'Mojolicious';
use MyApp::Model::Users;
sub startup {
my $self = shift;
$self->secrets(['Mojolicious rocks']);
$self->helper(users => sub { state $users = MyApp::Model::Users->new; });
my $r = $self->routes;
$r->any('/')->to('login#index')->name('index');
my $logged_in = $r->under('/')->to('login#logged_in');
$logged_in->get('/protected')->to('login#protected');
$r->get('/logout')->to('login#logout');
}
1;
package MyApp::Controller::Login;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $self = shift;
my $user = $self->param('user') || '';
my $pass = $self->param('pass') || '';
return $self->render unless $self->users->check($user, $pass);
$self->session(user => $user);
$self->flash(message => 'Thanks for logging in.');
$self->redirect_to('protected');
}
sub logged_in {
my $self = shift;
return 1 if $self->session('user');
$self->redirect_to('index');
return undef;
}
sub logout {
my $self = shift;
$self->session(expires => 1);
$self->redirect_to('index');
}
1;
package MyApp::Model::Users;
use strict;
use warnings;
my $USERS = {
joel => 'las3rs',
marcus => 'lulz',
sebastian => 'secr3t',
};
sub new {
bless {}, shift;
}
sub check {
my ($self, $user, $pass) = @_;
# Success
return 1 if $USERS->{$user} && $USERS->{$user} eq $pass;
# Fail
return undef;
}
1;
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
# Start command line interface for application
require Mojolicious::Commands;
Mojolicious::Commands->start_app('MyApp');
use Test::More;
use Test::Mojo;
# Load application class
my $t = Test::Mojo->new('MyApp');
$t->ua->max_redirects(1);
# Test if the HTML login form exists
$t->get_ok('/')
->status_is(200)
->element_exists('form input[name="user"]')
->element_exists('form input[name="pass"]')
->element_exists('form input[type="submit"]');
# Test login with valid credentials
$t->post_ok('/' => form => { user => 'sebastian', pass => 'secr3t' })
->status_is(200)->text_like('html body' => qr/Welcome sebastian/);
$t->get_ok('/protected')->status_is(200)->text_like('a' => qr/Logout/);
# Test if HTML login form shows up again after logout
$t->get_ok('/logout')->status_is(200)
->element_exists('form input[name="user"]')
->element_exists('form input[name="pass"]')
->element_exists('form input[type="submit"]');
done_testing();
<!DOCTYPE html>
<html>
<head><title>Login Manager</title></head>
<body><%= content %></body>
</html>
%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
% layout 'default';
% if (my $msg = flash 'message') {
<b><%= $msg %></b><br>
% }
Welcome <%= session 'user' %>.<br>
%= link_to Logout => 'logout'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment