Integrating Google Two-Factor Authentication with CodeIgniter 3

In this post we will show you Integrating Google Two-Factor Authentication with CodeIgniter 3, hear for Integrating Google Two-Factor Authentication with CodeIgniter 3 we will give you demo and example for implement.

Integrating Google Two-Factor Authentication with CodeIgniter extend is a genuinely simple process. The two-factor Authentication will help you to secure your administrator login board with an additional layer of assurance. Regardless of the possibility that someone got your username and secret word they can’t sign in unless they know your key or get your telephone which has google Authenticator application.

So how about we begin. The initial step is downloading Google Authenticator PHP library from GitHub. It is an outsider library yet since it has BSD permit you would you be able to unreservedly in your business application. You can download it from here.

When you download it at that point duplicate GoogleAuthenticator.php document to your application/libraries envelope. At that point open that document in any manager and change its class name to GoogleAuthenticator.

Presently we have our CodeIgniter library. Presently utilize it in your login work this way,

Code for Integrating Google Two-Factor Authentication in CodeIgniter 3

Add this code in your controller for Integrating Google Two-Factor Authentication

function login(){

        $this->load->library('GoogleAuthenticator');
        
        // Form validation codes
        $form_config = array(
            array(
                'field' => 'uername',
                'label' => 'user Name',
                'rules' => 'trim|required|xss_clean',
                'errors' => array('required' => 'Enter Valid Username', 'xss_clean' => 'cant inject malicious code')
            ),
            array(
                'field' => 'password',
                'label' => 'password',
                'rules' => 'trim|required|xss_clean',
                'errors' => array('required' => 'Enter Valid Password.', 'xss_clean' => 'cant inject malicious code')
            ),
            array(
                'field' => 'token',
                'label' => 'Two-factor token',
                'rules' => 'trim|xss_clean|max_length[6]',
                'errors' => array('required' => 'Enter Valid Two Factor Code.', 'xss_clean' => 'cant inject malicious code')
            ),

        );

	$this->form_validation->set_rules($form_config);
	if ($this->form_validation->run() == FALSE) 
	{
		// go to login page again again 
		$this->load->view('loginpage');
	}
	else
	{
		// Form validation conform 
		// code of 
		// Google Two-Factor Authentication

		$google_obj = new GoogleAuthenticator();
		$secret_key = "QFD5GdURKAFG8GF8" ; 
		// $google_obj->createSecret();
		$one_code = $this->input->post('token');
		
		$token = $google_obj->getCode($secret_key);
				
		$Resultcheck = $google_obj->verifyCode($secret_key, $one_code, 2); 
		// 2 = 2*30 sec clock tolerance
		if (!$Resultcheck)
		{
			// index function load login page view
			$this->index('Two-factor token Failed'); 
		}
		else
		{    
			// Two-factor code success and now steps 
			// for username and password verification
		} 
	}
}

You can create a new key by calling below function.

$google_obj->createSecret();

Once you create a key download Google Authenticator from google play store and install your key.

Note for Integrating Google Two-Factor Authentication with CodeIgniter

The calculation is time-based. Another one of a kind code is produced at regular intervals and each code is legitimate for 90 seconds as it were. So you should guarantee that your telephone time and your server time is precisely same else, it won’t work.Comment beneath on the off chance that you experienced any issue or need any assistance.

Hope this code and post will helped you for implement Integrating Google Two-Factor Authentication with CodeIgniter. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org