mimosinnet
unread,May 15, 2012, 1:15:49 PM5/15/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mojol...@googlegroups.com
After some hours and more reading, managed to get an authentication example. Get the stash printed to have some idea of what is getting on. Posting it here if it can be helpful for other newbies or if there is some improvements or some further reading, Cheers!
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojolicious::Plugin::Authentication;
use Data::Dumper;
plugin 'authentication', {
autoload_user => 0,
load_user => sub {
my ($app, $uid) = @_;
return {
'username' => 'mojo',
'password' => 'licious',
'name' => 'Mojolicious'
} if($uid eq 'userid');
return undef;
},
validate_user => sub {
my ($app, $username, $password, $extradata) = @_;
return 'userid' if($username eq 'mojo' && $password eq 'licious');
return undef;
},
};
helper stash_var => sub {
my $self = shift;
return Dumper($self->stash);
};
get '/' => sub {
my $self = shift;
my ($title, $url) = ("Home page, directed to the login", '/login');
$self->render(titol => $title, stash => $self->stash_var, url => $url );
} => 'basica';
get '/login' => sub { shift->render('login') };
post '/entrada' => sub {
my $self = shift;
my ($u, $p) = ($self->req->param('u'), $self->req->param('p'));
if ($self->authenticate($u, $p)) {
my ($title, $url) = ("Welcome Mojolicious, you are in. Let's see if we can go to another page", '/entrada/auth');
$self->render(titol => $title, stash => $self->stash_var, url => $url);
}
my ($title, $url) = ("You do not have access to this page", '/');
$self->render(titol => $title, stash => $self->stash_var, url => $url );
} => 'basica';
get '/entrada/auth' => (authenticated => 1) => sub {
my $self = shift;
my ($title, $url) = ("Congratulations, you are authenticated!", "/entrada/still_in");
$self->render(titol => $title, stash => $self->stash_var, url => $url);
} => 'basica';
get '/entrada/still_in' => (authenticated => 1) => sub {
my $self = shift;
my ($title, $url) = ("Still in! Logging out and back to previous page to get 'page not found'!", "/entrada/auth");
$self->logout;
$self->render(titol => $title, stash => $self->stash_var, url => $url);
} => 'basica';
app->start;
__DATA__
@@basica.html.ep
%= t h1 => $titol
%= $stash;
<p>Go to <%= link_to $url => $url %> web address.<p>
@@login.html.ep
%= t h1 => 'Login'
%= form_for '/entrada' => (method => 'post') => begin
<form method="post" action="/entrada">
Username: <%= text_field 'u' %>
Password: <%= text_field 'p' %>
%= submit_button 'entra'
%= end