Authenticate user

From Ben van Staveren

validate_user => sub {
  my ($c, $username, $password, $extra) = (@_);

  if(MyUsers->login($username, $password) == 1) {
    return $username;
  } else {
    $c->stash(login_error_message => 'Invalid credentials');
    return undef;
  }
};

load_user => sub {
   my ($c, $uid) = (@_);

   return MyUsers->get_user_data_for_username($uid);
};

And then you want to:

my $r = $self->routes->bridge('/')->to('login#check');
$r->get('/protected')->to('....');

The 'check' method in login can simply do this:

sub check {
  my $self = shift;

  return 1 if $self->is_user_authenticated;
  $self->render('login_form') and return 0;
}

Anything now routed under $r goes through the login check. If you want "public" and "private" bits, do it like this:

my $public = $self->routes->any('/')->to('public_site#index);
my $private = $public->bridge('/')->to('login#check');

$private->get('/')->to('private_site#index');

Async page prep

From s at bykov.odessa.ua

$app->hook(
    around_dispatch => sub {
      my ($next, $c) = @_;

      # grab a title from mojolicio.us
      $c->ua->get(
        'http://mojolicio.us' => sub {
          my ($ua, $tx) = @_;

          my $res = $tx->success;
          my $mytitle = $res ? $res->dom->at("title")->text : "Error";

          # stash
          $c->stash(mytitle => $mytitle);

          # Now we are ready
          $next->();
        }
      );


    }
  );

Render pdf from memory

From Pavel Goloborodko

$self->res->headers
    ->content_disposition("attachment;filename=${name}.pdf");
$self->res->headers->content_type('application/pdf');
$self->render_data( $pdf );