Password:
1. Created a Helper Function
Created helper functions for password hashing and verification to reuse across your application.
Create a file in app/Helpers, e.g., app/Helpers/password_helper.php
password_helper.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if (!function_exists('hash_password')) { function hash_password($password) { return password_hash($password, PASSWORD_BCRYPT); } } if (!function_exists('verify_password')) { function verify_password($input_password, $stored_hash) { return password_verify($input_password, $stored_hash); } } ?> |
2. Hash Password During Registration
1 2 | $password = $this->request->getPost('password'); $hashed_password = hash_password($password); |
3. Verify Password During Login
1 2 | $password = $this->request->getPost('password'); verify_password($input_password, $user->password); |
If the password is verified, it will return 1, else blank. e.g. below
1 2 3 4 5 6 7 | if(verify_password($password, $user->password)) { // Set session or other login logic session()->set(['username' => $user->username]); return redirect()->to('/superadmin/dashboard')->with('success', 'Login successful!'); }else { return redirect()->back()->with('error', 'Invalid credentials!'); } |