Using Auth module in Kohana 3.2

This post is shown incomplete example for using Auth module in Kohana 3.2

  1. Besides installing Kohana as normal. bootstrap.php has be modified in order to enable these modules that are Auth, Database and Orm. File-based authentication seems to be available also but this post will be focus on using orm and database only.

    Kohana::modules(array(
        'auth'       => MODPATH.'auth',       // Basic authentication
        // 'cache'      => MODPATH.'cache',      // Caching with multiple backends
        // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
        'database'   => MODPATH.'database',   // Database access
        // 'image'      => MODPATH.'image',      // Image manipulation
        'orm'        => MODPATH.'orm',        // Object Relationship Mapping
        // 'unittest'   => MODPATH.'unittest',   // Unit testing
        // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
        ));
    
  2. Provide configuration files in application/config. There should be auth.php and database.php copied from /modules.
  3. Create a database using SQL file named auth-schema-mysql.sql, which is included in Kohana 3.2. ( I made a mistake by spending time to find it using Google. ). Then change database.php to conform your database.
  4. Create application/view/template/default.php

    <?php echo $content;?>
    
  5. Create application/view/pages/login.php
    <?php echo form::open('/user/login'); ?>
     <?php echo form::label('username', 'Username:'); ?>
     <?php echo form::input('username'); ?>
     <?php echo form::label('password', 'Password:'); ?>
     <?php echo form::password('password'); ?>
     <?php echo form::submit('submit', 'Login'); ?>
    <?php echo form::close(); ?>
    

    (This is based on http://docs.kohanaphp.com/addons/auth#mysql_schema

  6. Create a user controller by inheriting Template controller as follow
    <?php defined('SYSPATH') or die('No direct script access.');
    
    class Controller_User extends Controller_Template
    {
     public $template = 'templates/default';
    
     public function action_login() 
     {
     $this->auto_render = FALSE; 
     $post = $this->request->post();
     $success = Auth::instance()->login($post['username'], $post['password']);
    
     if ($success)
     {
     $this->response->body('OK');
     }
     else
     {
     $this->response->body('FAIL');
     }
     }
     
     public function action_login_form()
     {
     $this->template->content = View::factory('pages/login');
     }
     
    } 
    
    

    At this state, you must be able to access URL /user/login_form and try to login and it should always report FAIL

  7. Add a user by directly insert a user to the table Users in MySQL. However the password must be generated from Auth::instance()->hash. Moreover users_roles must be added. Normally you should add 1,1.
  8. Everything should works now. However the controller still did not check whether the user is logged in. Register action is still not provided. I think what is left is not difficult to do by consulting an API reference.

Tags: , , , , , ,

One thought on “Using Auth module in Kohana 3.2

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s