0
votes

I'm developing a e commerce site where I need to handle frontend and backend parts of the applications. Therefore, I 'm going to handle customer logins and admin user logins. As this is a e commerce site, most people recommend to store users and customers separately considering security. If this was not e commerce type, I would go with user groups method and store all users in a single user table and manage privileges accordingly.

My problem is how should I use same Ion Auth authentication library for frontend and backend while keeping customer table and user table separately. You can understand that underneath problem is customer and admin session handling with same auth library.

I think HMVC also can not solve this problem. Currently I'm thinking of duplicating application folder or developing two standalone CI applications for frontend and backend.

I have very little experience in this type of applications. Can anyone guide me to the correct path? Thanks in advance.

1

1 Answers

0
votes

Changing the table that ion_auth uses to authenticate 'users' can easily be accomplished. And it can be done dynamically at run-time.

The name of the table used for authentication is defined in the configuration file application\config\ion_auth.php with this line of code.

$config['tables']['users'] = 'users';

However, the default table name can be whatever you like. If, in the database, you changed the name of the table 'users' to 'customers' you would change the config file to this:

$config['tables']['users'] = 'customers';

Now customers is the default table used to authenticate login.

But we want two authentication tables, so another table for admin purposes is created and named 'admin_users'.

All we need to do is change the value of $config['tables']['users'] to 'admin_users'. This would be done after the ion_auth library is loaded but before any ion_auth methods are used. The only thing that complicates making the change is the way ion_auth's config is loaded into memory.

To prevent name collisions an extra index is added to ion_auth's set of configuration items. So, in memory, the ion_auth config item $config['tables']['users'] is actually $config['ion_auth']['tables']['users']. You need to address this extra index when dynamically setting a new table name.

Here is some code that handles the extra index and dynamically changes the authentication table to 'admin_users'.

  // get array at $config['ion_auth']['tables']
  $ion_tables = $this->config->item('tables', 'ion_auth');
  // dynamically change item 'users' in that array
  $ion_tables['users'] = "admin_users";

This code would only be needed in Controllers that perform administrative functions. Nothing needs to happen in Controllers for "customers" because that is the default.