Mojolicious基本登录 [英] Mojolicious basic login with
本文介绍了Mojolicious基本登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Mojolicious中寻找认证.我有2页momcorp1和momcorp2,但我无法通过 在翻页之间,有人知道该怎么做.
iam looking for authentification in Mojolicious. I have 2 pages momcorp1 and momcorp2, But i cant pas between over pages, someone know how do this.
我在说"under",但我不知道这是怎么做的.
Iam reaading about "under", but i dont undertah how do this.
执行此操作的另一种形式是使用-Mojolicious :: Plugin :: Authentication-但难度更大.
The other form do this is use -Mojolicious::Plugin::Authentication - but is more dificult.
这是代码,当单击1链接到momcorp 2时,显示错误.
This is the code, when 1 click link to momcorp 2, show error.
#!/usr/bin/env perl
use Mojolicious::Lite;
helper auth => sub {
my $self = shift;
return 1 if
$self->param('username') eq 'user1' and
$self->param('password') eq 'user1';
};
get '/login'=> sub { shift->render('login') };
under sub {
my $self = shift;
return 1 if $self->auth;
$self->render(text => 'denied');
return;
};
post 'momcorp' => sub { shift->render(template => 'momcorp1') };
post '/momcorp/carol' => sub { shift->render(template => 'momcorp2')
};
app->start
__DATA__
@@ login.html.ep
%= t h1 => 'login'
%= form_for '/momcorp' => (method => 'post') => begin
username: <%= text_field 'username' %>
password: <%= text_field 'password' %>
%= submit_button 'log in'
%= end
@@ momcorp1.html.ep
%= t h1 => 'momcorp1'
<a href="/momcorp/carol">Link to 2</a>
@@ momcorp2.html.ep
%= t h1 => 'momcorp2'
<a href="/momcorp">Link to 1</a>
推荐答案
以下是您想要的示例
#!/usr/bin/env perl
use Mojolicious::Lite;
helper auth => sub {
my $c = shift;
return 1 if
$c->param('username') eq 'user1' and
$c->param('password') eq 'pass1';
return 0;
};
get '/'=> sub { shift->render } => 'index';
post '/login' => sub {
my $c = shift;
if ($c->auth) {
$c->session(auth => 1);
return $c->redirect_to('t1');
}
$c->flash('error' => 'Wrong login/password');
$c->redirect_to('index');
} => 'login';
get '/logout' => sub {
my $c = shift;
delete $c->session->{auth};
$c->redirect_to('index');
} => 'logout';
under sub {
my $c = shift;
return 1 if ($c->session('auth') // '') eq '1';
$c->render(text => 'denied');
return undef;
};
get '/test1' => sub { shift->render } => 't1';
get '/test2' => sub { shift->render } => 't2';
app->start;
__DATA__
@@ index.html.ep
%= t h1 => 'login'
% if (flash('error')) {
<h2 style="color:red"><%= flash('error') %></h2>
% }
%= form_for login => (method => 'post') => begin
username: <%= text_field 'username' %>
password: <%= text_field 'password' %>
%= submit_button 'log in'
%= end
@@ t1.html.ep
%= t h1 => 'test1'
<a href="<%= url_for('t2') %>">Link to test2</a>
@@ t2.html.ep
%= t h1 => 'This is test2'
<a href="<%= url_for('logout') %>">logout</a>
这篇关于Mojolicious基本登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文