*EDIT: check this post for an easy auth module i made myself

I searched the internet for a “GOOD” documentation about the auth module in kohana.
Couldnt find one… (not even on the official website….). [official documentation]
The example they include in the download of kohana isnt working at all (misses classes and so on….)

So i made an simple example.

Let’s start:

I assume that u have the auth tables allready correct inserted into youre database.
(if not go to modules/auth/views/auth/install.php query should be in the php source…)

Create a file login_test.php
place it in: application/controllers

CTR-C and CTRL-V the code in that file.
Goto: youredomain.nl/login_test

<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Made by:
 * Remorse.nl
 */
class Login_test_Controller extends Controller {

	// Do not allow to run in production
	const ALLOW_PRODUCTION = FALSE;

	public function index()
	{
		//just redirect
		url::redirect('login_test/login');
	}

	public function create()
	{
		echo '
			Create form:
email: name: pass:
'; //get the post data $form = $_POST; // Create new user $user = ORM::factory('user'); //set all the form field in the user class //so that we can use $user->save() that inserts a new record to the db //html form-field names must be exactly the same as the db-column names foreach ($form as $key => $val){ $user->$key = $val; } //ORM::factory('role', 'login') returns orm object and get's value from colum with name=login //$user->add makes a relation between $user and role orm model returned by ORM::factory('role', 'login') if ($user->save() AND $user->add(ORM::factory('role', 'login'))) { //login($username,$password) Auth::instance()->login($form['username'], $form['password']); //could also be like this: //Auth::instance()->login($user, $form['password']); // Redirect to the login page url::redirect('login_test/login'); } } public function login() { // user is logged in.... if (Auth::instance()->logged_in()) { echo 'u are logged in.... <a href="'.url::base().'login_test/logout">Logout</a>'; } else { echo ' <a href="'.url::base().'login_test/create">Click here to create a user Login form:
name: pass:
'; $form = $_POST; if($form){ // Load the user $user = ORM::factory('user', $form['username']); // orm user object or $form['username'] could be used if (Auth::instance()->login($user, $form['password'])) { // Login successful, redirect // or do some other things u like url::redirect('login_test/login'); } else { echo 'login_failed Invalid username or password.'; } } } } public function logout() { // Force a complete logout Auth::instance()->logout(TRUE); // Redirect back to the login page url::redirect('login_test/login'); } } // End Auth Controller

If you wanna know about it:

I used this .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !index\.php
RewriteRule ^(.*)$ index.php/$1 [L]

yeah!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis
  • NuJIJ