route & bridge

18 views
Skip to first unread message

Miguel Prz

unread,
Jul 25, 2013, 3:06:45 AM7/25/13
to perl...@googlegroups.com
I need to protect some routes, so if a public (non authenticated) user try to access, the web app redirects him to the login screen. I saw the routes & bridges documentation, so I wrote:

$r->add('/player', {bridge=>1, to=>'Login#login'});
$r->add('/player/:tid', 'Player#show');

The login method returns a template that shows the user+pass authentication form.

But, when I try to access to http://..../player/123, it doesn't show the login form. It executes the Login#login, forgets the template returned by this function, and executes Player#show. Is this the expected behaviour?

Now I am thinking on build some plack middleware to check the protected URLs, and redirect to login form to authenticate if needed.

I'd like to know what is the better approach to resolve this use case with Kelp.

Thanks!

Stefan Geneshky

unread,
Jul 25, 2013, 11:59:23 AM7/25/13
to perl...@googlegroups.com
The bridges must return a true/false value to allow or disallow the running of the following routes. When you return a template in your bridge, you're effectively returning a true value. You need to change the logic of your bridge to return 1 or 0. Also, accessing the bridge itself doesn't make sense.

Your bridge can also render but it always has to return 1 or 0 at the end. Here is an example:

sub check_login {
    my $self = shift;
    if ($self->user_is_authenticated) {
        return 1
    }
    else {
        $self->template('authentication.tt');
        return 0;
    }
}

Stefan

niceperl

unread,
Jul 25, 2013, 12:58:37 PM7/25/13
to perl...@googlegroups.com
thanks Stefan

but something it's not working because I get a "401 - Unauthorized" message in the browser when the check_login bridge returns 0,  and "authentication.tt" wasn't shown


2013/7/25 Stefan Geneshky <stefa...@gmail.com>

Stefan Geneshky

unread,
Jul 25, 2013, 1:12:49 PM7/25/13
to perl...@googlegroups.com
You need to also set the response code. So your template should be:

$self->res->set_code(200)->template('authentication')

Stefan

Stefan Geneshky

unread,
Jul 25, 2013, 1:13:13 PM7/25/13
to perl...@googlegroups.com
That's because the bridge automatically sets code 401, so you want to override that.

niceperl

unread,
Jul 25, 2013, 1:25:47 PM7/25/13
to perl...@googlegroups.com
Now it works perfect! thanks for your help, Stefan 



2013/7/25 Stefan Geneshky <stefa...@gmail.com>
Reply all
Reply to author
Forward
0 new messages