[Catalyst] stuck on tutorial's suggested variation

Jason Kohles email at jasonkohles.com
Tue Dec 4 16:05:31 GMT 2007


On Dec 4, 2007, at 10:22 AM, Michael Higgins wrote:

> All --
>
> Basically, I'm stuck and any leg up would be appreciated.
>
> From Catalyst Tutorial - Part 4: Authentication
>
> "NOTE: You could easily use a single controller here. For example, you
> could have a User controller with both login and logout actions.
> Remember, Catalyst is designed to be very flexible, and leaves such
> matters up to you, the designer and programmer."
>
> So. "Easily" hasn't come to me. If I set up a controller called  
> 'User',
> then the "login logic" further down the tutorial page:
>
>            if ($c->login($username, $password)) {
>
> what should $c->login look like if the "login" sub is part of
> Controller/User?
>
$c->login and your login method are not the same method, $c->login is  
inherited from Catalyst::Plugin::Authentication::Credential::Password.


> There is so much documentation, I really don't know where to find this
> information.
>
> What is happening is:
>
> "The page isn't redirecting properly
>
> Firefox has detected that the server is redirecting the request for
> this address in a way that will never complete.
>
>    *   This problem can sometimes be caused by disabling or refusing
> to accept cookies."
>
This suggests that your redirection to logging in is being triggered  
every time, rather than just when necessary.

>
> Use of uninitialized value in string eq
> at /home/col/charley/bolmaker/script/../lib/bolmaker/Controller/ 
> Root.pm
> line 56. [info]
>
> Which is:
>    if ($c->controller eq $c->controller('login')) {
>        return 1;
>    }
>
> So, $c is undefined? That's bad. :(
>
No, if $c were undefined you would get "attempt to call method  
controller on undefined value" or something along those lines.  What  
is undefined here is the result of $c->controller('login'), because  
you don't have a controller named login.  What it seems you really  
want here is:

if ( $c->controller eq $c->controller( 'User' ) ) {

> *** Request 4 (0.010/s) [19336] [Tue Dec  4 07:01:50 2007]
> *** [debug] "GET" request for "user/login" from
> "127.0.0.1" [debug] Arguments are "user/login" [debug] ***Root::auto
> User not found, forwding to /login [debug] Redirecting to
> "http://localhost:3000/user/login" [info] Request took 0.026145s
> (38.248/s)
>
> Anyway, if I could "easily" put this logic in one controller, what
> would that look like?
>
Probably something along these lines:

package MyApp::Controller::User;
use base qw( Catalyst::Controller );
__PACKAGE__->config->{ 'namespace' } = '';

sub login : Local {
	my ( $self, $c ) = @_;

	my $username = $c->request->params->{ 'username' } || '';
	my $password = $c->request->params->{ 'password' } || '';
	if ( $username && $password ) {
		if ( $c->login( $username, $password ) ) {
			$c->response->redirect( $c->uri_for( '/somewhere' ) );
			return;
		} else {
			$c->stash->{ 'error_msg' } = 'Bad username or password.';
		}
	}
	$c->stash->{ 'template' } = 'user/login.tt2';
}

sub logout : Local {
	my ( $self, $c ) = @_;

	$c->logout;
	$c->response->redirect( $c->uri_for( '/' ) );
}

1;

-- 
Jason Kohles, RHCA RHCDS RHCE
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Catalyst mailing list