ここはそんなに難しいことはなさそう。
やること
- ログイン
- ログアウト
ログイン
Auth::login()を使えば割と簡単にできた。
- Auth::get_user_id()はarrayが返ってくるのは注意が必要。int/falseで返してほしいなあ。
View
ユーザー名とパスワードをpostして判定する。emailは必要なさそう。
<?php if (Session::get_flash('message')): ?>
<div class="row alert alert-danger">
<p><?php echo Session::get_flash('message') ?></p>
</div>
<?php endif; ?>
<div class="row" style="padding: 2rem;">
<?php echo Form::open(['action' => 'user/login', 'method' => 'post']); ?>
<div class="form-group">
<label for="username">ユーザー名</label>
<?php echo Form::input('username', null, ['id' => 'username', 'class' => 'form-controll']); ?>
</div>
<div class="form-group">
<label for="password">パスワード</label>
<?php echo Form::password('password', null, ['id' => 'password', 'class' => 'form-controll']); ?>
</div>
<?php echo form::button('submit', null, ['class' => 'btn btn-primary']); ?>
<?php echo Form::close(); ?>
</div>
Controller
テンプレート設定の処理は冗長だけど今回は大目に見て。。。
public function post_login()
{
if (empty($_POST['username']) || empty($_POST['password'])) {
Session::set_flash('message', '入力は全て必須です');
$data["subnav"] = array('register'=> 'active' );
$this->template->title = 'User » Register';
$this->template->content = View::forge('user/login', $data);
return;
}
if (! Auth::login($_POST['username'], $_POST['password'])) {
Session::set_flash('message', 'ログインに失敗しました');
$data["subnav"] = array('register'=> 'active' );
$this->template->title = 'User » Register';
$this->template->content = View::forge('user/login', $data);
return;
}
Response::redirect('/');
}
ログアウト
View
<a href="/user/logout">ログアウト</a>
Controller
public function action_logout()
{
Auth::logout();
$data["subnav"] = array('logout'=> 'active' );
$this->template->title = 'User » Logout';
$this->template->content = View::forge('user/logout', $data);
}