Authentication example from Mojolicious::Plugin::Authentication

2,078 views
Skip to first unread message

mimosinnet

unread,
May 15, 2012, 1:15:49 PM5/15/12
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

Ben van Staveren

unread,
May 15, 2012, 9:32:03 PM5/15/12
to mojol...@googlegroups.com
Hi,

That's a nice one, do you have any objections to me including it in the
distribution?
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mojolicious/-/tH-zDPUDN6QJ.
> To post to this group, send email to mojol...@googlegroups.com.
> To unsubscribe from this group, send email to
> mojolicious...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mojolicious?hl=en.

--
Ben van Staveren
phone: +62 81 70777529
email: benvans...@gmail.com

Message has been deleted

mimosinnet

unread,
May 16, 2012, 7:32:49 AM5/16/12
to mojol...@googlegroups.com
Please do! I will be delighted! Thanks for mentioning it!

Cheers!

El dimecres 16 de maig de 2012 3:32:03 UTC+2, Ben van Staveren va escriure:
> mojolicious+unsubscribe@googlegroups.com.

bvn13

unread,
May 16, 2012, 7:59:34 AM5/16/12
to mojol...@googlegroups.com
вторник, 15 мая 2012 г., 21:15:49 UTC+4 пользователь mimosinnet написал:
%= form_for '/entrada' => (method => 'post') => begin
<form method="post" action="/entrada">


IMO, the seconds string duplicates the first one. 

Ben van Staveren

unread,
May 16, 2012, 10:25:35 AM5/16/12
to mojol...@googlegroups.com
No prob, I've got issues writing clear documentation so seeing a nice and
simple example that shows pretty much entirely how it all works is great to
have :D

I'll roll it into the distribution and pop it to CPAN, probably this weekend
if time permits. Up to my eyeballs into regular dayjob stuff :(

On 05/16/2012 06:31 PM, mimosinnet wrote:
> Please do! I will be delighted! Thanks for mentioning it!
>
> El dimecres 16 de maig de 2012 3:32:03 UTC+2, Ben van Staveren va escriure:
>
> <https://groups.google.com/d/msg/mojolicious/-/tH-zDPUDN6QJ>.
> > To post to this group, send email to mojol...@googlegroups.com
> <mailto:mojol...@googlegroups.com>.
> > To unsubscribe from this group, send email to
> > mojolicious...@googlegroups.com
> <mailto:mojolicious%2Bunsu...@googlegroups.com>.
> > For more options, visit this group at
> > http://groups.google.com/group/mojolicious?hl=en
> <http://groups.google.com/group/mojolicious?hl=en>.
>
> --
> Ben van Staveren
> phone: +62 81 70777529
> email: benvans...@gmail.com <mailto:benvans...@gmail.com>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mojolicious/-/DPLWWVtNWXkJ.

mimosinnet

unread,
May 16, 2012, 11:25:48 AM5/16/12
to mojol...@googlegroups.com


El dimecres 16 de maig de 2012 13:59:34 UTC+2, bvn13 va escriure:

Thanks for spotting the error! I first wrote the html version and then rewrote it using  tag helpers. This was left out! Cheers!

Nathan Waddell

unread,
Jun 17, 2013, 5:44:58 PM6/17/13
to mojol...@googlegroups.com
I would love to see some examples for Mojolicious as opposed to Mojolicious::Lite. Much of the mojo documentation for plugins focuses on M::Lite.


On Wednesday, May 16, 2012 9:25:35 AM UTC-5, Ben van Staveren wrote:
No prob, I've got issues writing clear documentation so seeing a nice and
simple example that shows pretty much entirely how it all works is great to
have :D

I'll roll it into the distribution and pop it to CPAN, probably this weekend
if time permits. Up to my eyeballs into regular dayjob stuff :(

Ben van Staveren

unread,
Jun 17, 2013, 6:16:51 PM6/17/13
to mojol...@googlegroups.com
Holy reply to an old email batman...

I'll have to work on that :)
> email: benvans...@gmail.com <javascript:>
>
> --
> 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 mojolicious...@googlegroups.com.
> To post to this group, send email to mojol...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages