在Mojolicious方面需要帮助

Ver*_*Lom 5 authentication perl routing mojolicious

我有" 与","控制器显示 "方法和" AUTHS "控制器与" 检查如果用户被认证,它返回1"的方法.我有"默认"页面(" / profile ").

我需要重定向到/如果用户已经过身份验证,并且如果用户未经过身份验证,则将所有页面重定向到/使用授权表单.我的代码不想正常工作(基于FastNotes示例应用程序的auth):(

使用授权表单验证#create_form - html-template.

    $r->route('/')       ->to('auths#create_form')   ->name('auths_create_form');
    $r->route('/login')      ->to('auths#create')    ->name('auths_create');
    $r->route('/logout')     ->to('auths#delete')    ->name('auths_delete');
    $r->route('/signup') ->via('get') ->to('users#create_form')   ->name('users_create_form');
    $r->route('/signup') ->via('post') ->to('users#create')    ->name('users_create');
    #$r->route('/profile') ->via('get') ->to('pages#show', id => 'profile') ->name('pages_profile');

    my $rn = $r->bridge('/')->to('auths#check');
    $rn->route        ->to('pages#show', id => 'profile') ->name('pages_profile');

 $rn->route('/core/:controller/:action/:id')
    ->to(controller => 'pages',
   action  => 'show',
   id   => 'profile')
    ->name('pages_profile');

 # Route to the default page controller
 $r->route('/(*id)')->to('pages#show')->name('pages_show');
Run Code Online (Sandbox Code Playgroud)

Cof*_*ter 11

看来你要/以呈现一个登录表单一个配置文件页面.上面的代码将始终显示/登录,因为它首先命中该路由条件,并且如果您已经过身份验证,则永远不会关心.

尝试在初始路由中切换/(在不需要桥接之后的默认路由).

my $r = $self->routes;
$r->get('/' => sub {
    my $self = shift;
    # Check whatever you set during authentication
    my $template = $self->session('user') ? '/profile' : '/login';
    $self->render( template => $template );
});
Run Code Online (Sandbox Code Playgroud)

有关您示例的几点说明:

  • 如果您使用Mojolicious :: Lite作为示例,则更容易帮助调试问题.
  • 尝试使用under而不是bridge.
  • 尝试使用$ r-> get(..)而不是$ r-> route(..) - > via(..)

希望这可以帮助.