Guest User

Untitled

a guest
Dec 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. use Mojolicious::Lite;
  3.  
  4. use lib 'lib';
  5. use MyUsers;
  6.  
  7. # Make signed cookies secure
  8. app->secret('Mojolicious rocks!');
  9.  
  10. my $users = MyUsers->new;
  11. app->helper(users => sub { return $users });
  12.  
  13. # Main login action
  14. any '/' => sub {
  15. my $self = shift;
  16.  
  17. # Query or POST parameters
  18. my $user = $self->param('user') || '';
  19. my $pass = $self->param('pass') || '';
  20.  
  21. # Check password and render "index.html.ep" if necessary
  22. return $self->render unless $self->users->check($user, $pass);
  23.  
  24. # Store user name in session
  25. $self->session(user => $user);
  26.  
  27. # Store a friendly message for the next page in flash
  28. $self->flash(message => 'Thanks for logging in!');
  29.  
  30. # Redirect to protected page with a 302 response
  31. $self->redirect_to('protected');
  32. } => 'index';
  33.  
  34. # A protected page auto rendering "protected.html.ep"
  35. get '/protected' => sub {
  36. my $self = shift;
  37.  
  38. # Redirect to main page with a 302 response if user is not logged in
  39. return $self->redirect_to('index') unless $self->session('user');
  40. } => 'protected';
  41.  
  42. # Logout action
  43. get '/logout' => sub {
  44. my $self = shift;
  45.  
  46. # Expire and in turn clear session automatically
  47. $self->session(expires => 1);
  48.  
  49. # Redirect to main page with a 302 response
  50. $self->redirect_to('index');
  51. } => 'logout';
  52.  
  53. app->start;
  54. __DATA__
  55.  
  56. @@ layouts/default.html.ep
  57. <!doctype html><html>
  58. <head><title>Login Manager</title></head>
  59. <body><%= content %></body>
  60. </html>
  61.  
  62. @@ index.html.ep
  63. % layout 'default';
  64. <%= form_for index => begin %>
  65. <% if (param 'user') { %>
  66. <b>Wrong name or password, please try again.</b><br>
  67. <%;
  68. }
  69. %>
  70. Name:<br>
  71. <%= text_field 'user' %><br>
  72. Password:<br>
  73. <%= password_field 'pass' %><br>
  74. <%= submit_button 'Login' %>
  75. <% end %>
  76.  
  77. @@ protected.html.ep
  78. % layout 'default';
  79. <% if (my $message = flash 'message') { %>
  80. <b><%= $message %></b><br>
  81. <% } %>
  82. Welcome <%= session 'user' %>!<br>
  83. <%= link_to Logout => 'logout' %>
Add Comment
Please, Sign In to add comment