gpt4 book ai didi

authentication - Mojolicious 基本登录

转载 作者:行者123 更新时间:2023-12-02 00:39:47 25 4
gpt4 key购买 nike

我正在 Mojolicious 中寻找身份验证。我有 2 页 momcorp1 和 momcorp2,但我不能通过在多页之间,有人知道该怎么做。

我正在阅读有关“under”的内容,但我不知道该怎么做。

另一种形式是使用 -Mojolicious::Plugin::Authentication - 但更困难。

这是代码,当 1 点击指向 momcorp 2 的链接时,显示错误。

#!/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>

关于authentication - Mojolicious 基本登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com