首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mojolicious登录

Mojolicious登录
EN

Stack Overflow用户
提问于 2017-11-30 18:44:34
回答 1查看 1.1K关注 0票数 2

我正在寻找在莫灼里的认证。我有2页momcorp1和momcorp2,但是我不能在页面之间出现错误,有人知道如何做到这一点。

我在谈论“下面”,但我不明白该怎么做。

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

这是代码,当1点链接到momcorp 2时,显示错误。

代码语言:javascript
复制
#!/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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-01 05:55:46

这是你想要的一个例子

代码语言:javascript
复制
#!/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>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47580535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档