Login in CI4 Complete Code

Controller:

Password:

1. Created a Helper Function
created helper functions for password hashing and verification to reuse them across your application.

Create a file in app/Helpers, e.g., app/Helpers/password_helper.php
hash_password($password)

verify_password($input_password, $stored_hash)

Load this helper in your application:

helper('password');

2. Hash Password During Registration

$password = $this->request->getPost('password');
$hashed_password = hash_password($password);

 

3. Verify Password During Login

$password = $this->request->getPost('password');
verify_password($input_password, $user->password)

If password verify will return 1, else blank.

Redirect (Back to request page):

return redirect()->back()->with('error', 'Invalid credentials!');

Form validation error
if(!$validation->withRequest($this->request)->run()) {
$validationErrors = $validation->listErrors();
// Redirect back with validation errors
return redirect()->back()->with('error', $validationErrors);
}

 

 

Model:

Not used

View:

 

Password Helper:

app/Helpers/password_helper.php

 

Leave a Reply

Your email address will not be published. Required fields are marked *