Can I set Ion auth to login by username OR Email Can I set Ion auth to login by username OR Email codeigniter codeigniter

Can I set Ion auth to login by username OR Email


If by automatic, you mean try one and then the other to see if either gives a valid return:

The login occurs in ion_auth_model line: 899

->where($this->identity_column, $this->db->escape_str($identity))

so you could change this to do an "or" and try both columns. You would need to do this throughout the model because there is more than just the actual login to consider & there's the potential issue of a user having an email that is another user's username (however unlikely)


Its possible with only a few code rows:Lets assume that this is your ion_auth config file:

$config['identity'] = 'username'; // A database column which is used to login with

you have to run the login function twice. If the first try (with username) didn't success, you can change the identifier and retry:

$this->ion_auth_model->identity_column = "email";

No modifications in model or library or custom querys neccesary


without having to edit ion_auth_model, you can do something like this:

considering you already have this config:

 $config['identity'] = 'username';

and you have this on your controller:

// log the user inpublic function login(){...    if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))    {    //if the login is successful

you can let it to check and then if it's not successful, set the email as identity column and check for it:

// log the user inpublic function login(){...    // check for username    $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);    if(! $login) { // username is not successful        $this->ion_auth_model->identity_column = 'email';        // check for email        $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);    }    if( $login ) {         // successful    } else {         // both failed    }

the advantage of this: more compatibility with any new update to ionAuth since you didn't change the core files. the downside of this is that's it's have to double query the database.


Auth controller code modified from: ionAuth Auth Controller Example

Discussions on ionAuth Repo: