FuelPHP ログイン認証

 ・config.php に 'auth' を追加する。

 app/config/config.php

  'always_load' => array(

   'packages' => array(

         'orm',

         'auth',

   ),

  ),


・auth.php, simpleauth.php を /packages/auth/config フォルダから

 /app/config フォルダへコピーする。


・auth.php を編集する。

  'salt' に適当な文字列を設定する。

  'salt' => 'teketeke',


・simpleauth.php を編集する。

 'login_hash_salt' に適当な文字列を設定する。

 'login_hash_salt' => 'tonton',

 

 認証に 'users' テーブル以外を使用する場合は、'table_name' に設定する。

 

 ※usersテーブルには下記列が必要。

  username varchar(50)

  password varchar(255)

  group int defautl 1

  email varchar(255)

  last_login varchar(25)

  login_hash varchar(255)

  profile_fields text

  created_at timestamp

  updated_at timestamp


  ※mysqlで timestamp を作成すると、「ON UPDATE CURRENT_TIMESTAMP」の制約が付いてしまうので、

 下記のようにして変更する。

  alter table users change created_at created_at timestamp not null default current_timestamp;


  ※Auth::create_user()で created_at に値を設定するところでエラーが発生する場合

  packages/auth/classes/auth/login/simpleauth.php の create_user() で

   'created_at' => \Date::forge()->get_timestamp(),

  の行をコメントする。

  また、update_user() の

   $update['updated_at'] = \Date::forge()->get_timestamp();

  の行もコメントする。

 

・app/classes/controller/login.php 作成

 <?php

 class Controller_Login extends Controller

 {

  public function action_index()

  {

   //すでにログイン済であればログイン後のページへリダイレクト

   Auth::check() and Response::redirect('welcome');

   //エラーメッセージ用変数初期化

   $error = null;

   //ログイン用のオブジェクト生成

   $auth = Auth::instance();

   //ログインボタンが押されたら、ユーザ名、パスワードをチェックする

   if (Input::post()) {

    if ($auth->login(Input::post('username'), Input::post('password'))) {

     // ログイン成功時、ログイン後のページへリダイレクト

     Response::redirect('welcome/');

    }else{

     // ログイン失敗時、エラーメッセージ作成

     $error = 'ユーザ名かパスワードに誤りがあります';

    }

   }

   //ビューテンプレートを呼び出し

   $view = View::forge('login/index');

   //エラーメッセージをビューにセット

   $view->set('error', $error);

   return $view;

  }

 }


・app/views/login/index.php 作成

 <div class="container">

 <div class="row">

  <h3>ログイン画面サンプル</h3>

  <?php echo Form::open(array('class' => 'form-horizontal'));?>

  <?php if (isset($error)): ?>

   <p class="alert alert-warning"><?php echo $error ?></p>

  <?php endif ?>

  <div class="form-group">

   <label for="form_name" class="col-sm-4 control-label">ユーザ名</label>

   <div class="col-sm-8">

    <?php echo Form::input('username');?>

   </div>

  </div>

  <div class="form-group">

   <label for="form_name" class="col-sm-4 control-label">パスワード</label>

    <div class="col-sm-8">

     <?php echo Form::password('password');?>

    </div>

  </div>

  <div class="form-group">

   <div class="col-sm-offset-4 col-sm-8">

    <?php echo Form::submit('submit', 'ログイン', array('class' => 'btn btn-success'));?>

   </div>

  </div>

  <?php echo Form::close();?>

 </div>

 </div>


・未ログイン時の認証チェックを、コントローラのbefore()メソッドに記載する。

 public function before() {

  // 未ログイン時、ログインページへリダイレクト

  if (!Auth::check()) {

   Response::redirect('/login');

  }

 }


・app/classes/controller/logout.php 作成

 <?php

 class Controller_Logout extends Controller

 {

  public function action_index()

  {

   //ログイン用のオブジェクト生成

   $auth = Auth::instance();

   $auth->logout();

   Response::redirect('/');

  }

 }