#!/usr/bin/env perl use Mojolicious::Lite; # A small example authentication helper # this just returns true if the 'user' and 'pass' params are correct # normally you'd not check quite this way of course helper authenticate => sub { my $c = shift; return ($c->param('user') eq 'authenticated' and $c->param('pass') eq 'pass'); }; # Group the things needing login group { under sub { my $c = shift; # authenticate the user if you can, and return 1 if he's authenticated $c->session->{authenticated} = 1 if $c->authenticate(); return 1 if $c->session->{authenticated}; # we only get here if the user isn't authenticated $c->render(template => 'login'); return 0; }; any '/needs_login' => sub { shift->render(template => 'authenticated'); }; }; get '/logout' => sub { my $c = shift; delete $c->session->{authenticated}; $c->render(template => 'index'); }; get '/' => sub { my $c = shift; $c->render(template => 'index'); }; app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Welcome'; Welcome to the Mojolicious real-time web framework! Do you want to see a <%= link_to "secret" => '/needs_login' %>? @@ layouts/default.html.ep <%= title %> <%= content %> @@ login.html.ep % layout 'default'; % title 'Log in'; %= form_for url_for() => (method => 'POST') => begin %= text_field 'user' %= password_field 'pass' %= submit_button %= end @@ authenticated.html.ep % layout 'default'; % title 'Some secret webpage'; You logged in successfully! Do you want to <%= link_to "Log out" => '/logout' %>?