1. Go to this page and download the library: Download rafaelwendel/ci4auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rafaelwendel / ci4auth example snippets
namespace Config;
use CI4Auth\Config\BaseAuthConfig;
class AuthConfig extends BaseAuthConfig
{
public string $authModel = 'App\Models\UserModel';
public string $findUserMethod = 'findUser';
public string $passwordField = 'password';
public string $sessionAuthCheckKey = 'logged_in';
public bool $enableRoles = true;
public string $roleKey = 'user_role';
public array $sessionData = [
'logged_in' => true,
'user_id' => 'user.id',
'user_email'=> 'user.email',
'user_role' => 'user.role' // Required since enableRoles is true
];
public bool $destroySessionOnLogout = false;
public string $notAuthenticatedRedirect = 'auth/login';
public string $notAuthorizedRedirect = 'dashboard/unauthorized';
}
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['email', 'password', 'role', 'name'];
/**
* Finds a user by their identity (email)
*
* @param string $identity
* @return array|object|null
*/
public function findUser(string $identity)
{
return $this->where('email', $identity)->first();
}
}
namespace App\Controllers;
class AuthController extends BaseController
{
public function login()
{
$authService = service('auth');
if ($this->request->is('post')) {
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
// Attempt authentication
if ($authService->login($email, $password)) {
return redirect()->to('dashboard')->with('success', 'Welcome back!');
}
// Authentication failed
return redirect()->back()->with('error', 'Invalid email or password.');
}
return view('login_view');
}
}
namespace App\Controllers;
class AuthController extends BaseController
{
public function logout()
{
service('auth')->logout();
// Redirect to login page
return redirect()->to('login')->with('success', session()->getFlashdata('logout_message'));
}
}
// Only users with 'admin' role can access the administration panel
$routes->get('admin/settings', 'AdminController::settings', ['filter' => 'auth:admin']);
// Users with either 'admin' OR 'manager' roles can access reports
$routes->group('reports', ['filter' => 'auth:admin,manager'], function($routes) {
$routes->get('/', 'ReportsController::index');
$routes->get('export', 'ReportsController::export');
});
// Example: app/Language/en/Auth.php
return [
'logoutFlashMessage' => 'You have successfully signed out.',
'notAuthenticatedFlashMessage' => 'Access denied. Please sign in first.',
'notAuthorizedFlashMessage' => 'You do not have the
bash
php spark auth:install
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.