Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: first steps with Mojolicious::Lite

by trippledubs (Deacon)
on Jun 16, 2020 at 01:48 UTC ( #11118114=note: print w/replies, xml ) Need Help??


in reply to Re^2: first steps with Mojolicious::Lite
in thread first steps with Mojolicious::Lite

Hi Discipulus,

I have really looked forward to assisting you in some trivial matter because of the MAJOR enlightenment and joy you add to Perlmonks!!

You really need to look at under because it is exactly what you want, I'm pretty sure!

I'll try to provide some code specific to your example, but this is from a small web site (like 200 occasional users)

my $onlyLoggedIn = $r->under('/admin' => \&loggedIn); $onlyLoggedIn->post('uploadFile')->to('files#insert'); $onlyLoggedIn->delete('files')->to('files#delete'); sub loggedIn { my $c = shift; if ($c->session('login')) { return 1; } $c->render( template => 'login', title => 'website title', status => 401, ); return 0; }; # elsewhere sub login { my $self = shift; my $name = $self->param('user'); my $password = $self->param('password'); my $responseCode = 401; # Pretty sure this hashes the param and checks it against the +hashed database entry if (Something::Model::Users::login($name,$password)) { # $self->signed_cookie(loggedIn => 1); $self->session(expiration => 60*60*10); $self->session(login => $name); $responseCode = 200; $self->app->log->warn("$name logged in."); } else { $self->app->log->warn("Invalid login - '$name'"); } $self->render(data => '',status => $responseCode); }

So we have all urls /admin/whatever for only authenticated users.

Something I recently learned

$self->helper( onlyauth => sub { my ($c,$block) = @_; if ($c->session('login')) { return $block->() if $block; } });

So I can use this in my templates!! I have an admin bar at the top of every regular page that only logged in users see

%= onlyauth begin %= include 'adminbar' % end

Replies are listed 'Best First'.
Re^4: first steps with Mojolicious::Lite
by Anonymous Monk on Jun 16, 2020 at 09:24 UTC
    Manual shows layout ... A daddy template

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11118114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-10-29 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (266 votes). Check out past polls.

    Notices?