File Coverage

blib/lib/CatalystX/SimpleLogin/TraitFor/Controller/Login/WithRedirect.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect;
2 5     5   41651 use MooseX::MethodAttributes::Role;
  5         15  
  5         54  
3 5     5   165389 use namespace::autoclean;
  5         15  
  5         48  
4              
5             requires qw/
6             redirect_after_login_uri
7             /;
8              
9             around 'redirect_after_login_uri' => sub {
10             my ($orig, $self, $c, @args) = @_;
11             if (!$c->can('session')) {
12             $c->log->warn('No $c->session, cannot do ' . __PACKAGE__);
13             return $self->$orig($c, @args);
14             }
15             return $c->session->{redirect_to_after_login}
16             ? delete $c->session->{redirect_to_after_login}
17             : $self->$orig($c, @args);
18             };
19              
20             before login_redirect => sub {
21             my ($self, $c, $message) = @_;
22             $c->flash->{error_msg} = $message; # FIXME - Flash horrible
23             $c->session->{redirect_to_after_login}
24             = $c->req->uri->as_string;
25             };
26              
27             1;
28              
29             __END__
30              
31             =head1 NAME
32              
33             CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect - redirect
34             users who login back to the page they originally requested.
35              
36             =head1 SYNOPSIS
37              
38             package MyApp::Controller::NeedsAuth;
39              
40             use Moose;
41             use namespace::autoclean;
42              
43             # One needs to inherit from Catalyst::Controller in order
44             # to get the Does('NeedsLogin') functionality.
45             BEGIN { extends 'Catalyst::Controller'; }
46              
47             sub inbox : Path Does('NeedsLogin') {
48             # Redirects to /login if not logged in
49             my ($self, $c) = @_;
50              
51             $c->stash->{template} = "inbox.tt2";
52              
53             return;
54             }
55              
56             # Turn on in config
57             MyApp->config('Contoller::Login' => { traits => 'WithRedirect' });
58              
59             =head1 DESCRIPTION
60              
61             Provides the C<login>
62             action with a wrapper to redirect to a page which needs authentication, from which the
63             user was previously redirected. Goes hand in hand with L<Catalyst::ActionRole::NeedsLogin>
64              
65             =head1 WRAPPED METHODS
66              
67             =head2 redirect_after_login_uri
68              
69             Make it use and extract C<< $c->session->{redirect_to_after_login} >>
70             if it exists.
71              
72             =head1 METHODS
73              
74             =head2 $controller->login_redirect($c, $message)
75              
76             This sets the error message to $message and sets
77             C<< $c->session->{redirect_to_after_login} >> to the current URL.
78              
79             =head1 SEE ALSO
80              
81             =over
82              
83             =item L<CatalystX::SimpleLogin::Controller::Login>
84              
85             =item L<CatalystX::SimpleLogin::Form::Login>
86              
87             =back
88              
89             =head1 AUTHORS
90              
91             See L<CatalystX::SimpleLogin> for authors.
92              
93             =head1 LICENSE
94              
95             See L<CatalystX::SimpleLogin> for license.
96              
97             =cut
98