stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE Fixed!

Working code:

=head2 index log them in, and if next_page is present redirect them to that page Thanks plu! =cut sub index : Private { my ( $self, $c ) = @_; # Get the username and password from form my $username = $c->req->param('username'); my $password = $c->req->param('password'); $c->flash->{next_page} ||= $c->req->param('next_page'); # If the username and password values were found in form if ( $username && $password ) { # Attempt to log the user in if ( $c->login( $username, $password ) ) { my $redirect_url; if ( defined $c->flash->{next_page} ) { $redirect_url = $c->uri_for( $c->flash->{next_page} ) } else { $redirect_url = $c->uri_for( '/users/view', $username +); } # If successful, then let them use the application $c->res->redirect( $redirect_url ); } else { # Set an error message $c->stash->{error_msg} = "Bad username or password."; $c->stash->{template} = 'login.tt2'; return; } } # If either of above don't work out, send to the login page $c->stash->{template} = 'login.tt2'; }

Howdy all,

This problem is (hopefully) pretty Catalyst specific.

What I'm trying to do is emulate what YouTube (and many many other sites have I'm sure) does with logins. If you aren't logged in and attempt to view a video that's restricted by age, etc., you click on a link/button that directs you to the login page. You log in and voila, you are taken back to the video/page you were originally viewing (granted your credentials are legit, but I digress). This is done by setting a query parameter (I shall call it "next_page") to the (relative) url of the page you were just viewing, and then the application redirects you to said page.

Sounds pretty simple? It should be. Here is the code I have started with:

sub index : Private { my ( $self, $c ) = @_; # Get the username and password from form my $username = $c->req->param('username'); my $password = $c->req->param('password'); # If the username and password values were found in form if ( $username && $password ) { # Attempt to log the user in if ( $c->login( $username, $password ) ) { my $redirect_url = $c->req->param('next_page') ? $c->uri_for( $c->req->param('next_page') ) : $c->uri_for( '/users/view', $username ); $c->log->debug( "Redirect:" . $redirect_url ); # If successful, then let them use the application $c->res->redirect( $redirect_url ); } else { # Set an error message $c->stash->{error_msg} = "Bad username or password."; $c->stash->{template} = 'login.tt2'; return; } } # If either of above don't work out, send to the login page $c->stash->{template} = 'login.tt2'; }

It appears, however, that $redirect_url is automatically being set to $c->uri_for( '/users/view', $username ); as opposed to $c->uri_for( $c->req->param('next_page' );, and nothing this is being displayed in the debug information on the Catalyst development server terminal screen:

[info] OnTheBeach powered by Catalyst 5.7012 You can connect to your server at http://devin-desktop:3000 [info] *** Request 1 (0.001/s) [19945] [Mon Aug 4 22:02:12 2008] *** [debug] Query Parameters are: .-------------------------------------+------------------------------- +-------. | Parameter | Value + | +-------------------------------------+------------------------------- +-------+ | next_page | /forum/create/1 + | '-------------------------------------+------------------------------- +-------' [debug] "GET" request for "login" from "127.0.0.1" [debug] Path is "login" [debug] Rendering template "login.tt2" [debug] Found sessionid "95b2f79cba1750949c5bb6dbc44089a0dca767c7" in +cookie [debug] Restored session "95b2f79cba1750949c5bb6dbc44089a0dca767c7" [debug] Applying HTML page layout wrappers to login.tt2 [info] Request took 0.150287s (6.654/s) .----------------------------------------------------------------+---- +-------. | Action | Tim +e | +----------------------------------------------------------------+---- +-------+ | /auto | 0.0 +00386s | | /login/index | 0.0 +00297s | | /end | 0.1 +27140s | | -> OnTheBeach::View::TT->process | 0.1 +23267s | '----------------------------------------------------------------+---- +-------' [info] *** Request 7 (0.009/s) [19945] [Mon Aug 4 22:02:14 2008] *** [debug] Body Parameters are: .-------------------------------------+------------------------------- +-------. | Parameter | Value + | +-------------------------------------+------------------------------- +-------+ | password | lairdo + | | submit | Submit + | | username | dhoss + | '-------------------------------------+------------------------------- +-------' [debug] "POST" request for "login" from "127.0.0.1" [debug] Path is "login" [debug] Found sessionid "95b2f79cba1750949c5bb6dbc44089a0dca767c7" in +cookie [debug] Restored session "95b2f79cba1750949c5bb6dbc44089a0dca767c7" [debug] Successfully authenticated user 'dhoss'. [debug] Redirect:http://localhost:3000/users/view/dhoss [debug] Redirecting to "http://localhost:3000/users/view/dhoss" [info] Request took 0.026401s (37.877/s) .----------------------------------------------------------------+---- +-------. | Action | Tim +e | +----------------------------------------------------------------+---- +-------+ | /auto | 0.0 +00275s | | /login/index | 0.0 +07231s | | /end | 0.0 +00975s | '----------------------------------------------------------------+---- +-------' [info] *** Request 8 (0.010/s) [19945] [Mon Aug 4 22:02:14 2008] *** [debug] "GET" request for "users/view/dhoss" from "127.0.0.1" [debug] Path is "users/view" [debug] Arguments are "dhoss" [debug] Rendering template "users/view.tt2" [debug] Found sessionid "95b2f79cba1750949c5bb6dbc44089a0dca767c7" in +cookie [debug] Restored session "95b2f79cba1750949c5bb6dbc44089a0dca767c7" [debug] Applying HTML page layout wrappers to users/view.tt2 [info] Request took 0.039015s (25.631/s) .----------------------------------------------------------------+---- +-------. | Action | Tim +e | +----------------------------------------------------------------+---- +-------+ | /auto | 0.0 +00197s | | /users/view | 0.0 +02659s | | /end | 0.0 +20642s | | -> OnTheBeach::View::TT->process | 0.0 +17903s | '----------------------------------------------------------------+---- +-------'

Ideas anyone? This is a pretty simple feature and I don't know what's catching so that it's not working.

Thanks everyone!

meh.

Replies are listed 'Best First'.
Re: Catalyst $c->req->param() issue?
by actualize (Monk) on Aug 05, 2008 at 04:53 UTC

    use "auto" in root.pm for managing authentication. It's in the catalyst tutorial here. As stated it is a global mechanism for making sure users don't make it into the application without authentication. It runs right after begin but before any other action. You can probably write something under auto that checks to make sure the user has stated that they are of age.

    -Actualize

      Thanks actualize. This is reused code from one of the earlier tutorials and I haven't checked in on the newer versions.

      Much apprecieated, ++!

      meh.