The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MojoX::Auth::Simple - Perl extension for login authentication for Mojolicious

VERSION

Version 0.04.02

SYNOPSIS

    use MojoX::Auth::Simple;
    use Mojolicious::Lite;
    use MojoX::Session;
    use DBI;

    # fill in you $dbh details here...
    my $db_host = "...";
    my $db_name = "...";
    my $dsn     = "DBI:mysql:database=$db_name;host=$db_host;";
    my $user    = "...";
    my $pass    = "...";
    my $dbh     = DBI->connect($dsn, $user, $pass, {'RaiseError' => '1'});

    plugin session => {
      stash_key => 'session',
      transport => MojoX::Session::Transport::Cookie->new,
      store => MojoX::Session::Store::Dbi->new(dbh  => $dbh),
      expires_delta => 900,
    };

    any [qw/get post/] => '/' => sub {
      my $self       = shift;
      my $page_title = "Index - not logged in";
      my $template   = "index";
      my $layout     = "default";
      my $session    = $self->stash('session');
      my $auth       = MojoX::Auth::Simple->new();
      $auth->load($session);
      $page_title = 'Index - logged in' if $auth->is_logged_in();
      $self->stash(date => $date,
                   page_title => $page_title,
                   logged_in  => $auth->session->data->{logged_in},
                   template   => $template,
                   layout     => $layout,
          );
    } => 'index';

    @@ index.html.ep
    <h2>My appliction content goes here</h2><br>
    <h3><a href="<%= url_for 'index' %><%="?from_url=$this_url" %>">Index</a></h3>
    <h3><a href="<%= url_for 'edit' %><%="?from_url=$this_url" %>">Edit</a></h3>

    @@ layouts/default.html.ep
    <html>
      <head>
        <title>My Application - <%= $page_title %> - Default layout</title>
      </head>
      <body>
        <!-- Header region begin -->
        <% if('true' eq $logged_in) { %>
        <div>
          <div>Logged in; <form action="<%= url_for 'logout' %>" method="POST">
            <input type="submit" value="Logout"></form></div>
        <% } else { %>
          <div>Not logged in; <form action="<%= url_for 'login' %>" method="POST">
            <input type="submit" value="Login"></form></div>
        <% } %>
        </div>
        <!-- Content region begin -->
        <%= content %>
        <!-- Content region end -->
      </body>
    </html>

DESCRIPTION

The aim of this mobule is to provide a framework to allow a simple authentication model for Mojolicious.

This module will change and become a plugin like MojoX::Session.

EXPORT

None by default.

SUBROUTINES/METHODS

MojoX::Auth::Simple inherits all methods from Mojo::Base and implements the following new ones.

new

    my $auth = MojoX::Auth::Simple->new();

Returns new MojoX::Auth object.

log_in

    my $auth = MojoX::Auth::Simple->new();
    $auth->log_in();

Sets the logged_in key in the session store to 'true' and adds the uid key in the session store to the uid of the logged in user.

log_out

    $auth->load($session);
    $auth->log_out();

Sets the logged_in key in the session store to 'false'.

is_logged_in

    $auth->load($session);
    $name = $auth->is_logged_in();

Returns the name or uid of the user that is logged in, or an empty string if they are not.

load

    $auth->load($session);

Adds the current session to the auth object to use it as a persistant store.

SEE ALSO

Please read the man pages for MojoX::Session to see how we are storing the basic auth info in the session hash in the stash.

AUTHOR

Kim Hawtin

BUGS

Please report any bugs or feature requests to bug-mojox-auth-simple at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Auth-Simple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MojoX::Auth::Simple

ACKNOWLEDGEMENTS

Thanks to Justin Hawkins for help with the building module and to Andy Kirkpatrick for debugging.

COPYRIGHT AND LICENSE

Copyright (C) 2010 by Kim Hawtin

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.