CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect - redirect


CatalystX-SimpleLogin documentation Contained in the CatalystX-SimpleLogin distribution.

Index


Code Index:

NAME

Top

CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect - redirect users who login back to the page they originally requested.

SYNOPSIS

Top

    package MyApp::Controller::NeedsAuth;

    sub something : Path Does('NeedsLogin') {
        # Redirects to /login if not logged in
    }

    # Turn on in config
    MyApp->config('Contoller::Login' => { traits => 'Login::WithRedirect' });

DESCRIPTION

Top

Provides the login action with a wrapper to redirect to a page which needs authentication, from which the user was previously redirected. Goes hand in hand with Catalyst::ActionRole::NeedsLogin

WRAPPED METHODS

Top

redirect_after_login_uri

Make it use and extract $c->session->{redirect_to_after_login} if it exists.

METHODS

Top

$controller->login_redirect($c, $message)

This sets the error message to $message and sets $c->session->{redirect_to_after_login} to the current URL.

SEE ALSO

Top

CatalystX::SimpleLogin::ControllerRole::Login
CatalystX::SimpleLogin::Form::Login

AUTHORS

Top

See CatalystX::SimpleLogin for authors.

LICENSE

Top

See CatalystX::SimpleLogin for license.


CatalystX-SimpleLogin documentation Contained in the CatalystX-SimpleLogin distribution.

package CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect;
use MooseX::MethodAttributes::Role;
use Moose::Autobox;
use namespace::autoclean;

requires qw/
    redirect_after_login_uri
/;

around 'redirect_after_login_uri' => sub {
    my ($orig, $self, $c, @args) = @_;
    if (!$c->can('session')) {
        $c->log->warn('No $c->session, cannot do ' . __PACKAGE__);
        return $self->$orig($c, @args);
    }
    return $c->session->{redirect_to_after_login}
        ? delete $c->session->{redirect_to_after_login}
        : $self->$orig($c, @args);
};

before login_redirect => sub {
    my ($self, $c, $message) = @_;
    $c->flash->{error_msg} = $message; # FIXME - Flash horrible
    $c->session->{redirect_to_after_login}
        = $c->uri_for(
            $c->action,
            $c->req->captures,
            $c->req->args->flatten,
            $c->req->parameters,
            )
            ->as_string;
};

1;

__END__