Kohana v3 :: Auth Module

I was just about to throw my monitor across the room when I finally got the Auth Module working:

  1. IMPORTANT – do not use the complete v3 download from the main Kohana site (It simply doesn’t work that way), instead git the project, the core, and all the modules individually from github: http://github.com/kohana.
  2. Note that the ORM and Auth Modules have been forked and have their own repositories, but the links are the main Kohana git page
  3. Create the necessary database tables (MySql):
    CREATE TABLE IF NOT EXISTS `roles` (
    `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` varchar(32) NOT NULL,
    `description` varchar(255) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation');
    INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.');CREATE TABLE IF NOT EXISTS `roles_users` (
    `user_id` int(10) UNSIGNED NOT NULL,
    `role_id` int(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`user_id`,`role_id`),
    KEY `fk_role_id` (`role_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `email` varchar(127) NOT NULL,
    `username` varchar(32) NOT NULL DEFAULT '',
    `password` varchar(255) NOT NULL,
    `logins` int(10) UNSIGNED NOT NULL DEFAULT '0',
    `last_login` int(10) UNSIGNED,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_username` (`username`),
    UNIQUE KEY `uniq_email` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    CREATE TABLE IF NOT EXISTS `user_tokens` (
    `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` int(11) UNSIGNED NOT NULL,
    `user_agent` varchar(40) NOT NULL,
    `token` varchar(32) NOT NULL,
    `created` int(10) UNSIGNED NOT NULL,
    `expires` int(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_token` (`token`),
    KEY `fk_user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ALTER TABLE `roles_users`
    ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
    ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;
    ALTER TABLE `user_tokens`
    ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
  4. The only database change I made is to change the User.password field to varchar 255 to allow for large hashed passwords.
  5. Ensure that your database is configured correctly – per my previous post on Kohana’s Database Session.
  6. Then follow the tutorial on the Wiki for the Auth fork git repository: http://wiki.github.com/biakaveron/kohana-auth.

Leave a Reply