Authentication forwarding to a login page

127 views
Skip to first unread message

mjb152

unread,
Nov 6, 2014, 5:10:47 AM11/6/14
to mojol...@googlegroups.com
I'm using Mojolicious::Plugin::Authentication  and routing via  bridge, as per the example below.

How can I use this construct to remember the page I was directed from, and then go back there once I'm authenticated ?  And how could that handle several attempts at authentication ?  (in case of wrong password)


my $auth_bridge = $r->bridge('/members')->to('auth#check');
    $auth_bridge->route('/list')->to('members#list'); # only visible to logged in users

mjb152

unread,
Nov 6, 2014, 7:55:03 AM11/6/14
to mojol...@googlegroups.com
just answering my own question (slightly), I found I had to use $c->url_for to get the route which referred,  and then use flash before redirecting.

I tried using stash, but it refused to work at all with redirect,   flash worked fine.

aleksander.groschev

unread,
Nov 6, 2014, 8:30:10 AM11/6/14
to mojol...@googlegroups.com
You can see example at Galileo https://metacpan.org/source/JBERGER/Galileo-0.037/lib/Galileo/User.pm
Also you can use redirect_to method from Mojolicious::Controller with refferer:

$c->redirect_to($c->req->headers->referrer);

--
С уважением,
Александр Грошев
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

mjb152

unread,
Nov 6, 2014, 9:40:26 AM11/6/14
to mojol...@googlegroups.com
thankyou, seems that flash is used there as well.
All working now.

Николай Турнавиотов

unread,
Nov 8, 2014, 10:51:14 AM11/8/14
to mojol...@googlegroups.com
I try this:
controller auth:
# User login page view
sub login {
    my $self = shift;
    my $headers = $self->req->headers;
    $self->render(from => ($headers->header('referer') or '/'));
};

sub authenticate {
my $from = $self->every_param('from')->[0];
///if login pass ok:
       return $self->redirect_to($from);
};

login view
...
%= hidden_field 'from', $from
...

Николай Турнавиотов

unread,
Nov 8, 2014, 12:49:19 PM11/8/14
to mojol...@googlegroups.com
Oh, after login at authorize bridge sub I use flash too

sub authorization {
// if grants ok redirect to $self->current_route
//else {
        $self->flash(error => $msg );
        $self->flash(from => $self->current_route );
        return $self->redirect_to('/login');
};
};

template login:
        % if (my $fromflash = flash 'from' ) {.
            %= hidden_field 'from', $fromflash
        % } else {

            %= hidden_field 'from', $from
        % }

Martin Bower

unread,
Nov 8, 2014, 1:00:56 PM11/8/14
to mojol...@googlegroups.com

Thanks guys, I had it working.. But I hadn't seen %= hidden_field before, so I'll implement that.

--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/19U5samIW7o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.

Николай Турнавиотов

unread,
Nov 8, 2014, 5:44:34 PM11/8/14
to mojol...@googlegroups.com, martin...@gmail.com

as I see, if no flash $from at template, default current route from bridge is 'login' page, so I use if (flash 'from') for a bridge and default from, if user want login and return to some other not bridged page.
at help I see $self->redirect_to('my_url', someopt => 'somevalue');
but not test it now, may be it works such as flash, may be Sebastian says something about it.

суббота, 8 ноября 2014 г., 20:00:56 UTC+2 пользователь mjb152 написал:
Reply all
Reply to author
Forward
0 new messages