Can I set Ion auth to login by username or email

I know that I can configure and authenticate a user to log in using the username in the config, I also know that I can configure it to log in by email in the config.

Is there an easy way to set it up for automatic use?

+4
source share
9 answers

If automatic, you mean try one and then another to find out if it gives a valid income:

Login is performed in line ion_auth_model: 899

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

so that you can change this to make an β€œor” and try both columns. You will need to do this in the whole model, because there is more than just the actual username to consider, and there is a potential problem for a user having an email that is a different username (as if unlikely)

+5
source

This is possible with only a few lines of code: Suppose this is your ion_auth configuration file:

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

you need to run the login function twice. If the first attempt (with the username) failed, you can change the identifier and try again:

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

No changes to the model or library or user queries are required.

+4
source

I recently forked Ion Auth and made the necessary improvements so that it could be selected in the configuration. The plug is here:

https://github.com/zepernick/CodeIgniter-Ion-Auth

I suggested a transfer request to include it in the Ion Auth code base, but it was not accepted at this time. There was some debate about whether this code had turned into complicated code. Please leave a note for them and let them know that you will like this feature, if it is useful to you.

https://github.com/benedmunds/CodeIgniter-Ion-Auth/pull/746

+1
source

without editing ion_auth_model , you can do something like this:

given that you already have this config:

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

and you have this on the controller:

 // log the user in public 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 check, and then if it failed, set the email column as an identifier and check it:

 // log the user in public 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 } 

advantage: more compatibility with any new ionAuth update since you have not changed the core files. the disadvantage of this is that it must query the database twice.


Authentication controller code modified from: ionAuth Auth ion example

Discussions on ionAuth Repo:

+1
source

use 'email' in 'identity' ion_auth config, then add this code after $ query in the line ion_auth_model 866

 if($query->num_rows() == 0){ $query = $this->db->select($this->identity_column . ', username, email, id, password, active, last_login') ->where('username', $this->db->escape_str($identity)) ->limit(1) ->get($this->tables['users']); } 
0
source

I think it will be easier to check if $ identity var is an email. If this is not a letter, then you set the column to "username". Something like that:

 $check_column = valid_email($identity) ? $this->identity_column : 'username'; $query = $this->db->select('username, email, id, password, active, last_login') ->where($check_column, $this->db->escape_str($identity)) ->limit(1) ->get($this->tables['users']); 

In this case, you will need the downloaded email_helper.

It works for me.

0
source

Put it on your controller

 if ($this->form_validation->run() !== FALSE) { $remember = (bool) $this->input->post('remember'); $this->ion_auth_model->identity_column = 'username/email'; if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember)) { $this->session->set_flashdata('message', $this->ion_auth->messages()); } redirect('auth/login'); } 

Edit ion_auth_model.php. find the login () function and update the code using the following code.

 public function login($identity, $password, $remember=FALSE) { $this->trigger_events('pre_login'); if (empty($identity) || empty($password)) { $this->set_error('login_unsuccessful'); return FALSE; } $this->trigger_events('extra_where'); //just add this (starting this line) if ($this->identity_column == "username/email") { $fieldname = explode('/', $this->identity_column); $query = $this->db->select($fieldname[0] . ', username, email, id, password, active, last_login') ->where($fieldname[0], $identity) ->limit(1) ->get($this->tables['users']); $this->identity_column = $fieldname[0]; if ($query->num_rows() === 0) { $query = $this->db->select($fieldname[1] . ', username, email, id, password, active, last_login') ->where($fieldname[1], $identity) ->limit(1) ->get($this->tables['users']); $this->identity_column = $fieldname[1]; } } else { $query = $this->db->select($this->identity_column . ', username, email, id, password, active, last_login') ->where($this->identity_column, $identity) ->limit(1) ->get($this->tables['users']); } //up to this line if($this->is_time_locked_out($identity)) { //Hash something anyway, just to take up time $this->hash_password($password); $this->trigger_events('post_login_unsuccessful'); $this->set_error('login_timeout'); return FALSE; } if ($query->num_rows() === 1) { $user = $query->row(); $password = $this->hash_password_db($user->id, $password); if ($password === TRUE) { if ($user->active == 0) { $this->trigger_events('post_login_unsuccessful'); $this->set_error('login_unsuccessful_not_active'); return FALSE; } $this->set_session($user); $this->update_last_login($user->id); $this->clear_login_attempts($identity); if ($remember && $this->config->item('remember_users', 'ion_auth')) { $this->remember_user($user->id); } $this->trigger_events(array('post_login', 'post_login_successful')); $this->set_message('login_successful'); return TRUE; } } //Hash something anyway, just to take up time $this->hash_password($password); $this->increase_login_attempts($identity); $this->trigger_events('post_login_unsuccessful'); $this->set_error('login_unsuccessful'); return FALSE; } 
0
source

You can do this without changing the kernel code. Just change the identity column on the fly if a valid email address is present. NOTE: ion_auth_model not ion_auth .

 public function check_login() { if (!$this->input->is_ajax_request()) { exit('No direct script access allowed'); } $this->form_validation->set_rules('username', str_replace(':', '', $this->lang->line('login_identity_label')), 'required'); $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required'); if ($this->form_validation->run() == false) { $this->form_validation->json_errors(); } $identity = $this->input->post('username'); if ($this->form_validation->valid_email($identity)) { $this->ion_auth_model->identity_column = 'email'; } else { $this->ion_auth_model->identity_column = 'username'; } if ($this->ion_auth->login($identity, $this->input->post('password'), false)) { encode_response('success'); } else { encode_response('error', $this->ion_auth->errors()); } } 
0
source

I have a similar answer to Mohannad Najjar, but to avoid double querying the database, you can check if the email is valid thanks to the email assistant and enter the user into the system by email when the email is valid and the username is with username when it is not.

 $this->load->helper('email'); if(!valid_email($this->input->post('identity')) { $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember); } else { $this->ion_auth_model->identity_column = 'email'; $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember); } 

It also prevents, for example, a double attempt to enter the database in the event of a failed login if you use it.

0
source

Source: https://habr.com/ru/post/1436656/


All Articles