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.

In reply to [SOLVED] Catalyst $c->req->param() issue? by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.