FAQ
Hi *,

I want to use authentication in my catalyst app. The authentication
seems to work, but I can't access any user information as $c->user is
undefined.

My code looks like this:

use Catalyst qw/
-Debug
Static::Simple

ConfigLoader
Redirect

Session
Session::State::Cookie
Session::Store::DBI

Authentication
Authorization::Roles
/;

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

my $params = $c->req->params;
my ($user,$pass) = @{$params}{qw/username password/};

my $result = $user and $pass and $c->authenticate({
username => $user,
password => $pass,
});

if( $result ){
$c->log->debug( 'login successful: ' . $c->user );
$c->redirect( $c->uri_for( '/start' ) );
}
else{
# error message
}
}

The log says "login successful:" but nothing more.

What is wrong with my code?

- Ren?e
--

Perl-Magazin: http://perl-magazin.de
Perl-Nachrichten: http://perl-nachrichten.de

Search Discussions

  • Tomas Doran at Apr 22, 2009 at 12:00 pm

    Renee B?cker wrote:
    Hi *,

    I want to use authentication in my catalyst app. The authentication
    seems to work, but I can't access any user information as $c->user is
    undefined. <snip>
    The log says "login successful:" but nothing more.

    What is wrong with my code?
    Nothing appears wrong with that code.

    However you haven't shown us your authentication config, so we don't
    know what you're authenticating with / against.

    If you could also include the debug log of the hit which calls
    $c->authenticate and then redirects, and the hit after that it would be
    helpful.

    Cheers
    t0m
  • Walter Vargas at Apr 22, 2009 at 12:04 pm
    You loaded Session module ?

    Este mensaje ha sido enviado gracias al servicio BlackBerry de Movilnet

    -----Original Message-----
    From: Tomas Doran <bobtfish@bobtfish.net>

    Date: Wed, 22 Apr 2009 13:00:49
    To: The elegant MVC web framework<catalyst@lists.scsys.co.uk>
    Subject: Re: [Catalyst] Problem with authentication


    Renee B?cker wrote:
    Hi *,

    I want to use authentication in my catalyst app. The authentication
    seems to work, but I can't access any user information as $c->user is
    undefined. <snip>
    The log says "login successful:" but nothing more.

    What is wrong with my code?
    Nothing appears wrong with that code.

    However you haven't shown us your authentication config, so we don't
    know what you're authenticating with / against.

    If you could also include the debug log of the hit which calls
    $c->authenticate and then redirects, and the hit after that it would be
    helpful.

    Cheers
    t0m

    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/
  • Renee Bäcker at Apr 22, 2009 at 12:39 pm

    Walter Vargas schrieb:
    You loaded Session module ?
    Yes...

    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Renee Bäcker at Apr 22, 2009 at 12:39 pm

    Tomas Doran schrieb:
    Nothing appears wrong with that code.

    However you haven't shown us your authentication config, so we don't
    know what you're authenticating with / against.

    If you could also include the debug log of the hit which calls
    $c->authenticate and then redirects, and the hit after that it would be
    helpful.
    Log output:

    [info] *** Request 1 (0.167/s) [10155] [Wed Apr 22 12:56:43 2009] ***
    [debug] Body Parameters are:
    .-------------------------------------+--------------------------------------.
    Parameter | Value
    +-------------------------------------+--------------------------------------+
    password | secret

    username | reneeb
    '-------------------------------------+--------------------------------------'
    [debug] "POST" request for "root/login" from "127.0.0.1"
    [debug] Path is "root/login"
    [debug] login successful:
    [debug] Finding template to render ...
    [debug] Trying to render template
    "/dir/templates/start.tmpl" ...
    [debug] Redirecting to "http://localhost:3000/root/start"
    [info] Request took 0.167890s (5.956/s)
    .------------------------------------------------------------+-----------.
    Action | Time
    +------------------------------------------------------------+-----------+
    /root/login | 0.114807s

    /end | 0.036004s

    -> GriPS::View::TT->process | 0.033808s
    '------------------------------------------------------------+-----------'

    [info] *** Request 2 (0.333/s) [10155] [Wed Apr 22 12:56:43 2009] ***
    [debug] "GET" request for "root/start" from "127.0.0.1"
    [debug] Path is "root/start"
    [debug] Finding template to render ...
    [debug] Trying to render template
    "/dir/templates/start.tmpl" ...
    [error] Caught exception in GriPS::Controller::Root->start "Can't call
    method "user_id" on an undefined value at
    /dir/../lib/GriPS/Controller/Root.pm
    line 52."
    [info] Request took 0.110155s (9.078/s)
    .------------------------------------------------------------+-----------.
    Action | Time
    +------------------------------------------------------------+-----------+
    /root/start | 0.002196s

    /end | 0.006856s

    -> GriPS::View::TT->process | 0.004974s
    '------------------------------------------------------------+-----------'


    and this is the configuration:

    authentication:
    default_realm: default
    realms:
    default:
    credential:
    class: Password
    password_field: passwd
    password_type: clear
    store:
    class: DBIx::Class
    user_class: DB::grips_user
    role_relation: rolenames
    role_field: role
    Cheers
    t0m
    Thanks!

    - Renee
    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Ben Vinnerd at Apr 22, 2009 at 12:48 pm
  • Tomas Doran at Apr 22, 2009 at 1:02 pm

    Ben Vinnerd wrote:
    Should...

    user_class: DB::grips_user

    ...be....

    user_model: DB::grips_user
    Good call!

    Cheers
    t0m
  • Renee Bäcker at Apr 22, 2009 at 1:26 pm

    Tomas Doran schrieb:
    Ben Vinnerd wrote:
    Should...

    user_class: DB::grips_user

    ...be....

    user_model: DB::grips_user
    Good call!
    Great. It doesn't work, but I'm one step further. Thanks!
    Now the session is created.
    Cheers
    t0m - Ren?e
    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Renee Bäcker at Apr 22, 2009 at 1:53 pm

    Renee B?cker schrieb:
    Tomas Doran schrieb:
    Ben Vinnerd wrote:
    Should...

    user_class: DB::grips_user

    ...be....

    user_model: DB::grips_user
    Good call!
    Great. It doesn't work, but I'm one step further. Thanks!
    Now the session is created.
    Why do I have to specify a "current_model"
    (GriPS::Model::DB::grips_user) in the login sub?

    I have specified a "default_model" (GriPS::Model::DB) in my config.

    And why does $c->user return an object of class GriPS::Model::DB::roles?
    I would expect that it returns an object of the user model.
    Cheers
    t0m
    - Ren?e
    - Ren?e
    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Tomas Doran at Apr 22, 2009 at 12:54 pm

    Renee B?cker wrote:
    [info] *** Request 1 (0.167/s) [10155] [Wed Apr 22 12:56:43 2009] ***
    [debug] Body Parameters are:
    .-------------------------------------+--------------------------------------.
    Parameter | Value
    +-------------------------------------+--------------------------------------+
    password | secret

    username | reneeb
    '-------------------------------------+--------------------------------------'
    [debug] "POST" request for "root/login" from "127.0.0.1"
    [debug] Path is "root/login"
    [debug] login successful:
    [debug] Finding template to render ...
    [debug] Trying to render template
    "/dir/templates/start.tmpl" ...
    [debug] Redirecting to "http://localhost:3000/root/start"
    [info] Request took 0.167890s (5.956/s)
    .------------------------------------------------------------+-----------.
    Action | Time
    +------------------------------------------------------------+-----------+
    /root/login | 0.114807s

    /end | 0.036004s

    -> GriPS::View::TT->process | 0.033808s
    '------------------------------------------------------------+-----------'
    Erm, something wacky is going on here - how come you're rendering a
    template if this is a redirect?

    Are you using RenderView, and can you show us your end action?
    [info] *** Request 2 (0.333/s) [10155] [Wed Apr 22 12:56:43 2009] ***
    [debug] "GET" request for "root/start" from "127.0.0.1"
    [debug] Path is "root/start"
    [debug] Finding template to render ...
    [debug] Trying to render template
    "/dir/templates/start.tmpl" ...
    [error] Caught exception in GriPS::Controller::Root->start "Can't call
    method "user_id" on an undefined value at
    /dir/../lib/GriPS/Controller/Root.pm
    line 52."
    I would guess this is due to the fact that your template processing
    blows up, and the hit is aborted at that point, but the current response
    is still sent (and as it has a 302 code, the response body is ignored,
    and you are redirected).

    Although I may be totally wrong about the specifics, however isn't
    anything in the log here to make out that the user data is being
    serialized into your session at the end of the hit, or restored at the
    beginning of the next hit, which is your issue..

    (Also, you need to check that $c->user returns something before calling
    methods on the user otherwise someone directly accessing /start without
    logging in first will get a 500 page)

    Cheers
    t0m
  • Renee Bäcker at Apr 22, 2009 at 1:14 pm

    Tomas Doran schrieb:
    Erm, something wacky is going on here - how come you're rendering a
    template if this is a redirect?

    Are you using RenderView, and can you show us your end action?
    the end action is just a forward to the view class.

    Although I may be totally wrong about the specifics, however isn't
    anything in the log here to make out that the user data is being
    serialized into your session at the end of the hit, or restored at the
    beginning of the next hit, which is your issue..
    do I have to do anything to ensure that the data is serialized??

    Cheers
    t0m Ren?e
    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Tomas Doran at Apr 22, 2009 at 1:45 pm

    Renee B?cker wrote:
    Tomas Doran schrieb:
    Erm, something wacky is going on here - how come you're rendering a
    template if this is a redirect?

    Are you using RenderView, and can you show us your end action?
    the end action is just a forward to the view class.
    Don't do that. Use RenderView.
    Although I may be totally wrong about the specifics, however isn't
    anything in the log here to make out that the user data is being
    serialized into your session at the end of the hit, or restored at the
    beginning of the next hit, which is your issue..
    do I have to do anything to ensure that the data is serialized??
    Not throwing an exception would be a good start. :_)

    If nothing else, delegating to the view and stuffing an error page into
    the response body for all of your redirects isn't too elegant..

    Cheers
    t0m
  • Renee Bäcker at Apr 22, 2009 at 1:58 pm

    Tomas Doran schrieb:

    the end action is just a forward to the view class.
    Don't do that. Use RenderView.
    I'm using RenderView now ;-)
    do I have to do anything to ensure that the data is serialized??
    Not throwing an exception would be a good start. :_) ;-)
    Cheers
    t0m
    - Ren?e


    --

    Perl-Magazin: http://perl-magazin.de
    Perl-Nachrichten: http://perl-nachrichten.de
  • Sean McAfee at Apr 22, 2009 at 5:05 pm
    2009/4/22 Renee Bäcker <mailinglisten@renee-baecker.de>
    Hi *,

    I want to use authentication in my catalyst app. The authentication
    seems to work, but I can't access any user information as $c->user is
    undefined.

    My code looks like this: [...]
    my $result = $user and $pass and $c->authenticate({
    username => $user,
    password => $pass,
    });
    I imagine you ought to be using "&&" instead of "and" here.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090422/1b8e0515/attachment.htm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedApr 22, '09 at 11:55a
activeApr 22, '09 at 5:05p
posts14
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2021 Grokbase