Ion Auth 4 for CodeIgniter 4

repository·4·Indexed 25 days ago

https://github.com/benedmunds/codeigniter-ion-auth

A simple, lightweight authentication library designed specifically for CodeIgniter 4 applications. It provides user authentication, group management, and password hashing using bcrypt or argon2. The library includes support for database migrations, seeds, and a transparent migration path from SHA1 hashing to modern methods.

Tokens
6.9K
Snippets
14
Records
36
Agent score
80%

What's inside Ion Auth 4

  1. Migrate from SHA1 hashing to modern methods

    4

    The sha1 hash method is no longer supported due to security concerns. However, Ion Auth provides a transparent migration path for existing users:

    • Automatic Migration: After upgrading to Ion Auth 3/4, any user logging into your application will have their password automatically migrated to the new hashing method (e.g., bcrypt or argon2) upon a successful login.
    • Identification: You can identify old SHA1-based passwords in your database by looking for entries in the password field that do not start with a $ sign.
    • Cleanup: Once most users have migrated, you may choose to invalidate any remaining users who still possess SHA1-based hashes.
  2. Install Ion Auth 4.x for development (Git method)

    4

    If you want to develop against the library directly, clone the repository and checkout the 4 branch. You must then manually register the namespace in your CodeIgniter 4 Config/Autoload.php file using the psr4 array.

    $ git clone https://github.com/benedmunds/CodeIgniter-Ion-Auth.git
    $ cd CodeIgniter-Ion-Auth
    $ git checkout 4

    In Config/Autoload.php:

    public $psr4 = [
    	...
    	'IonAuth' => ROOTPATH . 'CodeIgniter-Ion-Auth',
    	...
    ];
  3. Implement Ion Auth Controllers and Routes

    4

    To use Ion Auth in your application, you should extend the base Auth controller and define routes in Config/Routes.php.

    1. Create a Custom Controller Extend \IonAuth\Controllers\Auth. If you wish to use custom views, copy the ion-auth/Views/auth folder to your application's Views folder and uncomment the $viewsFolder property.

    2. Configure Routes Group your authentication routes under an auth prefix and point the namespace to IonAuth\Controllers.

    // App/Controllers/Auth.php
    <?php namespace App
    Controllers;
    
    class Auth extends \IonAuth\Controllers\Auth
    {
        /**
         * If you want to customize the views,
         *  - copy the ion-auth/Views/auth folder to your Views folder,
         *  - remove comment
         */
        // protected $viewsFolder = 'auth';
    }
    // Config/Routes.php
    $routes->group('auth', ['namespace' => 'IonAuth\Controllers'], function ($routes) {
    	$routes->add('login', 'Auth::login');
    	$routes->get('logout', 'Auth::logout');
    	$routes->add('forgot_password', 'Auth::forgot_password');
        // ... other routes
    });
  4. Install Ion Auth 4.x via Composer

    4

    Ion Auth 4 requires CodeIgniter 4.x, PHP 7.1, and Composer. To install it in an existing or new Composer project, you must first set the minimum stability to dev and add the repository via Git before requiring the package.

    For an existing project:

    1. Set minimum stability to dev.
    2. Add the IonAuth VCS repository.
    3. Require the package using the 4.x-dev version.

    For a new project:

    1. Initialize composer.
    2. Set minimum stability to dev.
    3. Add the IonAuth VCS repository.
    4. Require the package using the 4.x-dev version.
    # For an existing project
    $ composer config minimum-stability dev
    $ composer config repositories.ionAuth vcs git@github.com:benedmunds/CodeIgniter-Ion-Auth.git
    $ composer require benedmunds/codeigniter-ion-auth:4.x-dev
    
    # For a new project
    $ composer init
    $ composer config minimum-stability dev
    $ composer config repositories.ionAuth vcs git@github.com:benedmunds/CodeIgniter-Ion-Auth.git
    $ composer require benedmunds/codeigniter-ion-auth:4.x-dev
  5. Upgrade from Ion Auth 2 to Ion Auth 4

    4

    Upgrading from Ion Auth 2 is a multi-step process involving file replacement, configuration updates, and database migrations:

    1. Perform the standard file overwrite as described in the Ion Auth 3 upgrade steps.
    2. Review config/ion_auth.php for modified options.
    3. Run the appropriate SQL migration file for your database environment:
      • MySQL: sql/migrating_from_ionauth2/migrate.sql
      • PostgreSQL: sql/migrating_from_ionauth2/migrate.postgre.sql
      • SQL Server: sql/migrating_from_ionauth2/migrate.mssql.sql
    4. Database Cleanup: If you were not using the SHA1 hash method, you may drop the salt column from the users table.
    5. SHA1 Users: If you were using the SHA1 hash method, follow the Migrating from SHA1 guidance.
    6. Code Audit: Check your application code for modified or removed Ion_auth_model functions.
  6. Upgrade from Ion Auth 3 to Ion Auth 4

    4

    To upgrade from a previous revision of Ion Auth 3 to the current version:

    1. Download the latest Ion Auth 3 revision.
    2. Overwrite libraries/ion_auth.php and models/ion_auth_model.php with the new versions.
    3. Overwrite all files in the language/ directory with the new versions.
    4. Review config/ion_auth.php for any configuration changes or evolutions required by the new version.
  7. Upgrade Ion Auth 4

    4

    To upgrade to a newer version of Ion Auth 4:

    1. Download the latest version.
    2. Overwrite Libraries/IonAuth.php and Models/IonAuthModel.php with the new versions.
    3. Overwrite all files in the Languages/ directory.
    4. Review Config/IonAuth.php for any new configuration options or changes.

    Note: If you are upgrading from Ion Auth 2, refer to the UPGRADING.md file included in the package.

  8. Install and use Ion Auth 4

    4

    Ion Auth 4 provides simple and lightweight authentication specifically for CodeIgniter 4 applications.

    Note: This version is not backwards compatible with previous versions of Ion Auth, although the database schema remains compatible for migration purposes.

    To get started, refer to the following files in the repository:

    • Installation instructions: INSTALLING.md
    • Detailed usage guide: USERGUIDE.md
    • Upgrade instructions: UPGRADING.md

    For quick implementation reference, the repository includes example controllers and views that demonstrate how to integrate the library without requiring custom base controllers (like MY_Controller).

  9. Set up Ion Auth Database (Migrations and Seeds)

    4

    Ion Auth requires database tables. Use the provided migrations and seeds to set up the relational database.

    1. Run Migrations Execute the migration command using the IonAuth namespace. Ensure that Config\Migrations is set to enabled => true in your application.

    2. Run Seeds You can use the IonAuthSeeder to insert default data. Note the difference in path separators between Windows and Linux.

    Migration Command: php spark migrate -n IonAuth

  10. Install Ion Auth 4

    4

    To install Ion Auth 4 in your CodeIgniter 4 application, follow these steps:

    1. Download: Get the latest version from the GitHub zipball.
    2. Copy Files: Copy the package files to your application. You can either:
      • Copy specific files to their corresponding folders (e.g., IonAuth/Config/IonAuth.php to app/Config/IonAuth.php).
      • Copy the entire directory structure into app/ThirdParty/IonAuth/.
    3. Run Migrations: Execute the migration file located in Database/Migrations/ using the following command:
      $ php spark migrate:latest -n IonAuth
    4. Seed Default Data: Insert default data. Ensure Config\Migrations:enabled is set to true in your configuration.

    Windows:

    $ php spark db:seed IonAuth\Database\Seeds\IonAuthSeeder

    Linux:

    $ php spark db:seed IonAuth\\Database\\Seeds\\IonAuthSeeder

    Default Credentials:

    • Email: admin@admin.com
    • Password: password