This content originally appeared on DEV Community and was authored by Em leons
Authentication is the keystone of any secure web application. In the world of PHP frameworks, CodeIgniter 4 has gained significant traction due to its lightweight and powerful features. Recently, CodeIgniter introduced Shield, a powerfully built authentication library that makes securing your application easier than ever. In this article, we'll explore how to set up user authentication using CodeIgniter 4 Shield, step by step.
What is CodeIgniter 4 Shield?
CodeIgniter 4 Shield is an authentication and authorization library specifically designed for CodeIgniter 4. It provides a comprehensive set of features that simplify the process of securing your web application. With Shield, you can implement user registration, login, password management, roles, permissions, and much more.
Getting Started:
Before diving into the code, let's ensure your development environment is set up correctly. You'll need a working CodeIgniter 4 installation. If you haven't already installed CodeIgniter 4, you can do so via Composer:
composer create-project codeigniter4/appstarter userAuth
Once your CodeIgniter 4 application is set up, you'll need to install the Shield package:
cd userAuth
cp env .env
nano .env //setup database
To install CI shield Run the following command:
composer require codeigniter4/shield
You can read more from the documentation here
After installing Shield, you'll need to set it up the configuration files and database migrations to customize settings accordingly Run the following command:
php spark shield:setup
Follow the command line steps to complete setup. Once shield setup completes it will generate necessary files,database tables and configurations ready for you to go.
if setup completed successfully to start a development serve Run the following command:
php spark serve
Now open you web browser visit you project always runs on port 8080
For login page open
http://localhost:8080/login
For registration page open
http://localhost:8080/register
Create a filter
php spark make:filter AuthFilter
inside AuthFilter.php
public function before(RequestInterface $request, $arguments = null)
{
if (auth()->loggedIn()) {
$user = auth()->user();
// Do something.
}else{
return redirect()->to('/login');
}
}
You can read more from the documentation here
This content originally appeared on DEV Community and was authored by Em leons
Em leons | Sciencx (2024-08-14T21:08:32+00:00) How to Authenticate Users Codeigniter Shield. Retrieved from https://www.scien.cx/2024/08/14/how-to-authenticate-users-codeigniter-shield/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.