Mocholic action of a dynamic route depending on the state

I am using Mojolicious :: Plugin :: Authentication to authenticate in my application. I am trying to configure a route for the slash '/', which will have one controller / action if it is authenticated and another if not (i.e. you go to another page depending on whether you are authenticated.) I do not sure how to do it. Here are some of the things I've tried:

$r->any('/')->to(cb => sub {
    my $self = shift;

    if ( $self->is_user_authenticated ) {
      $self->redirect_to('member#index');
    }
    else {
      $self->redirect_to('guest#index');
    }
});

AND...

my $logged_in = $r->under (sub {
    my $self = shift;

    if (!$self->session("username")) {
        return undef;
    }
    else {
        return 1;
    }
});

if ( $logged_in ) {
    $logged_in->get('/')-to(controller => 'Member', action => 'index');
}
else {    
    $r->get('/')->to(controller => 'Guest', action => 'index');
}

I do not need to use Mojolicious :: Plugin :: Authentication. I could easily set the session token and check it myself. In any case, the problem remains: how to create a dynamic action for a given route?

Adding

Forgot to add, I also tried:

my $auth = $r->under('/' => sub {
    my $self = shift;

    # Authenticated
    return 1 if $self->is_user_authenticated;

    # Not authenticated
    return undef;
});

$auth->get('/')->to('member#index');

# Routes related to non-members
$r->get('/')->to('guest#index');

Adding another part

, - , , ,

$r->any('/')->to(cb => sub {
    my $self = shift;

    if ( $self->is_user_authenticated ) {
      $self->render('member/index');
    }
    else {
      $self->render('guest/login');
    }
});

. , , / .

+4
1

Mojo - , , , :

http://mojolicio.us/perldoc/Mojolicious#HOOKS

, , before_dispatch()

, .

+3

All Articles