Search code examples
formsauthenticationormmodelfuelphp

Basic user authentication example in FuelPHP


I've just started using Fuel and I'm trying to implement basic user authentication and registration using the auth package.

I have a controller called auth.php in which I have action_login, action_register and action_logout functions. These functions (currently) only call views which generate the html forms for the login and registration process (but they don't currently do anything).

I've looked at the documentation and tried to find some tutorials online, but it seems to be lacking. Do I need a user model? Do I need to use orm? I have user table, which I generated with oil. Do I need a secure token for use with the forms? What's the simplest method of writing this?


Solution

  • Here are the steps to create an login and registration system

    Note these steps are just an example and I have not added steps to create Views because your post shows that you have already achieved it

    1. Copy the simpleauth.php file from /home/aravind/ietscholarship/fuel/packages/auth to fuel/app/config folder . and do all configurations which you need in that file inside fuel/app/config/simpleauth.php
    2. Create table using this SQL statement

      CREATE TABLE IF NOT EXISTS users ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(255) NOT NULL, password varchar(255) NOT NULL, email varchar(255) NOT NULL, profile_fields text NOT NULL, group int(11) NOT NULL, last_login int(20) NOT NULL, login_hash varchar(255) NOT NULL, created_at int(11) NOT NULL, updated_at int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1834 ;

    This can also be done using oil generate command and specifying same fields in the above create statement

    3 . create a Model_User class in side Fuel/app/classes/model folder to post the code its little lengthy so I have created a github gist here

    4 . and create your login , logout and register action in fuel/app/classes/controller and I have created a gist use this

    5 . Create routes in fuel/app/config/routes.php as below

     'login' => 'users/login',
        'logout' => 'users/logout',
        'register' => 'users/register',
    

    And thats all your system is ready

    In fuel php almost all DB access is done using ORM package , which makes the developers life very easy when it comes to handle any CRUD operations .

    So to use Auth package you have to use ORM package .

    Also Last but not Least One of the most working stable example fuel php application could be found in github its the repository created for fuelphp crash course by University of central florida you can just fork that and play around those existing functionality to makeyourself comfortable with fuelphp .

    here is the link to the repository

    fuelphp-crash-course

    also here are some of the answers to your question

    Do I need a secure token for use with the forms?

    I dont think so you need always its up to your requirements

    Hope this helps you to get started