[Catalyst] tutorial - adding "auth" function

Sarah Berry berry.sarah at gmail.com
Wed Jun 7 23:01:56 CEST 2006


Hi,

I've been trying to learn Catalyst by going through the tutorial. I'm stuck
on the "exercise for the reader" in which I'm supposed to add an "auto"
action in tutorial.pm, to force the user to log in before s/he can view any
other pages. I searched the list archives, and someone else with a similar
problem was told,
"Your auto action redirects *every* access to /users/login, which
includes calls of /users/login. So a check for action or path should do
the trick."
I haven't been able to figure out how to check for the path. Here is what I
have so far (2 versions of the code):

    sub auto : Private {
        my ( $self, $c) = @_;
        if ( !$c->user_exists) {
            $c->res->redirect( '/users/login' ); # require login
            $c->res->body("redirecting to login ... ");
            return 0; # abort request and go immediately to end()
        }
        return 1; # success; carry on to next action
    }

    sub end : Private {
        my ($self, $c) = @_;
        $c->forward('tutorial::View::TToolkit') unless $c->res->body();
    }

Using that code, a call to any page gets redirected to /users/login, but the
page doesn't display -- just this message:

"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."

The other code I've tried is this:

    sub auto : Private {
        my ( $self, $c, $path_args) = @_;
        if ( !$c->user_exists && $path_args != "/users/login" ) {
            $c->res->redirect( '/users/login' ); # require login
            $c->res->body("redirecting to login ... ");
            return 0; # abort request and go immediately to end()
        }
        $c->stash->{message} = $path_args;
        return 1; # success; carry on to next action
    }

I can tell from the page that displays the stashed message that $path_args
is blank. So I can access any page and I am never redirected to a login.
So, how can I tweak this auto function to force users to log in?

thanks in advance,
Sarah
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20060607/fdd9e7e0/attachment.htm 


More information about the Catalyst mailing list