To create a login functionality for an Expense Manager App using CodeIgniter 4, you’ll need to set up the necessary controllers, views, and models. Here’s a step-by-step guide to implementing the login feature:
1. Database Setup
Create a database table named login_info
for storing user information.
CREATE TABLE `login_info` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`country_code` varchar(10) NOT NULL,
`mobile_number` varchar(100) NOT NULL,
`branch_id` varchar(10) DEFAULT NULL,
`image_url` text NOT NULL,
`role` varchar(255) NOT NULL,
`access_group_id` varchar(10) NOT NULL,
`city` varchar(255) NOT NULL,
`secret_token` varchar(255) NOT NULL,
`ip_address` varchar(255) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`status` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2. Create the Model
Create a model named CustomerModel.php
in the app/Models/customer
directory.
class CustomerModel extends Model {
protected $db;
public function __construct(ConnectionInterface &$db) {
$this->db =& $db;
$this->table = 'customer_info';
}
public function addEntry($data) {
$this->db
->table($this->table)
->insert($data);
return $this->db->insertID();
}
public function updateEntry($where, $data) {
return $this->db
->table($this->table)
->where($where)
->set($data)
->update();
}
public function deleteEntry($where) {
return $this->db
->table($this->table)
->where($where)
->delete();
}
public function getEntry($where) {
return $this->db
->table($this->table)
->where($where)
->get()
->getRow();
}
public function getEntryList($where = 0, $start = 0, $limit = 0, $columnName = 0, $orderBy = 0) {
$builder = $this->db->table($this->table);
if($where)
$builder->where($where);
if($columnName && $orderBy)
$builder->orderBy($columnName, $orderBy);
if($limit)
$builder->limit($limit, $start);
return $builder
->get()
->getResult();
}
}
Create the Controller
Create a controller named LoginApi.php
in the app/Controllers/api/v1
directory.
class LoginApi extends BaseController {
public function __construct() {
$db = db_connect();
$this->login = new LoginModel($db);
$this->ip_address = $_SERVER['REMOTE_ADDR'];
$this->datetime = date("Y-m-d H:i:s");
}
public function index() {
}
public function login() {
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
$where = [
'email' => $email,
'password' => md5($password),
'status' => '1',
];
$result = $this->login->getEntry($where);
if($result) {
$json = [
'status' => true,
'message' => 'Logged in successfully',
'result' => $result,
];
} else {
$json = [
'status' => false,
'message' => 'Something went wrong. Please try again!',
];
}
echo json_encode($json);
}
}
3. Configure Routes
Update your app/Config/Routes.php
file to add routes for the login functionality.
// Routes
$routes->group('api', function($routes) {
// Login
$routes->group('v1', function($routes) {
// Login
$routes->post('login', 'api/v1/LoginApi::login');
});
});
NOTE: Please replace the forward slash in Routes.php with the backward slash in your code.
This basic implementation provides a secure login feature for the Expense Manager App using CodeIgniter 4.