Last active
August 29, 2015 14:21
-
-
Save nisei/d90acdfeb243f98974fe to your computer and use it in GitHub Desktop.
easy session by mojoliciou
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Test::Web::Controller::Base; | |
use Mojo::Base 'Mojolicious::Controller'; | |
sub init { | |
my $self = shift; | |
$self->check_valid_session; | |
} | |
sub check_valid_session { | |
my $self = shift; | |
my $path_info = $self->{tx}->{req}->{env}->{PATH_INFO}; | |
unless ($self->session('user')) { | |
$self->redirect_to('/') unless $path_info eq '/'; | |
} | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% layout 'default'; | |
% title 'Test Mojolicious'; | |
<div id="index"> | |
<div id="page-index-title"></div> | |
<div id="page-index-login-error"> | |
<span style="color:#ff0000;"><%= $login_message %></span> | |
</div> | |
% if (!$user_name) { | |
<form action="/" method="post"> | |
<input type="text" name="login_id" placeholder="user id" maxlength="20" size="20"/><br /> | |
<input type="text" name="password" placeholder="password" maxlength="20" size="20"/><br /> | |
<input type="hidden" name="type" value="login"/> | |
<input type="submit" value="login" /> | |
</form> | |
% } else { | |
<form class="mt20" action="/" method="post"> | |
<input type="hidden" name="type" value="logout"/> | |
<input type="submit" value="logout" /> | |
</form> | |
% } | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Test::Web::Controller::Top; | |
use Mojo::Base qw(Mojolicious::Controller); | |
use base qw(Test::Web::Controller::Base); | |
use Test::Web::Model::User; | |
use Test::Config; | |
sub index { | |
my $self = shift; | |
my $login_id = $self->param('login_id'); | |
my $password = $self->param('password'); | |
my $request_method = $self->{tx}->{req}->{env}->{REQUEST_METHOD}; | |
my $login_message; | |
if ($request_method eq 'POST' && $self->param('type') eq 'login') { | |
if (Test::Web::Model::User->check_user_auth($login_id, $password)) { | |
$self->session(user => $login_id); | |
} else { | |
$login_message = "login failed"; | |
$self->session(expires => 1); | |
} | |
} | |
elsif ($request_method eq 'POST' && $self->param('type') eq 'logout') { | |
$self->session(expires => 1); | |
$self->redirect_to('/'); | |
} | |
$self->stash(user_name => Test::Web::Model::User->get_user_name($self->session('user'))); | |
$self->stash(title => Test::Config->get('site')->{name}); | |
$self->stash(login_message => $login_message); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment