CodeIgniter 4 Tutorial part 1 ~ RESTful API JWT Authentication

Mantan Programmer
Geek Culture
Published in
4 min readMay 18, 2022

Hello, how are you all, friends, this time we will discuss Restful API with JWT Authentication. Previously for the Codeigniter 4 tutorial we discussed Login and Register using Codeigniter 4.

Before we continue we will discuss what is called an API and JWT.

For those who like to watch videos, you can visit my video here Rest Api Login and Register With Codeigniter 4:

API stands for Application Program Interface, API is an interface that allows applications to exchange data. To make it clearer, an API is a collection of functions that can be used by programmers to build software and applications.

JWT stands for JSON Web Token, it is an open standard (RFC 7519) that defines a compact and self-contained way to securely transmit information between parties as JSON objects. JWT is generally used for Authorization, Information Exchange and others.

Now we will create the application.

Step 1: Install CodeIgniter 4

Install via composer:

composer create-project codeigniter4/appstarter ci-4-jwt

Step 2: Change CodeIgniter Environmen

Env. CodeIgniter default is production, this is a security feature to add security to the application. Next we change the name of the env file to .env add a period to your env. After being renamed, then open the .env file and we will change some commands.

.env

CI_ENVIRONMENT = development

Step 3: Configure Database

After setting up the env, it will then configure the database. You can configure it in .env or in the configuration file located in app/Config/Database.php. For this tutorial we will configure it in app/Config/Database.php.

app/Config/Database.php.

Step 4: Model and Migration

Model — The class that represents the database table.

Migration — like version control for a database that allows us to change and share the database schema with your team.

Run this command in Terminal or CMD to create the model:

php spark make:model UserModel

open the created model in app/Models/UserModel.php. In the file you can see the configuration options, you can read the documentation to learn more about the configuration options. Now we will update the configuration:

app/Models/UserModel.php

After creating the model, we will then create the migration file.

Run this command in Terminal or CMD to create the migration:

php spark make:migration AddUser

Open the migration file created in app/Database/Migrations/ and paste the following code:

Run the migration by executing the migrate command:

php spark migrate

Step 5: Install JWT Package

Then we will install the jwt package using composer:

composer require firebase/php-jwt

After installing the jwt package, add JWT_SECRET to the .env . file

.env

#--------------------------------------------------------------------
# JWT
#--------------------------------------------------------------------
JWT_SECRET = 'JWT SECRET KEY SAMPLE HERE'

Step 6: Create Controllers

The controller is the person responsible for receiving the Request and returning the Response.

Run this command in Terminal or CMD to create a Controller:

After running the command, it will create a file located in app/Controllers. Open the file and enter the following code:

app/Controllers/Login.php

app/Controllers/Register.php

app/Controllers/User.php

Step 7: Create Controller Filter

Filter Controller is a Class that allows us to perform an action before or after it is executed.

We are now going to create a Filter which will be used to check if the request is allowed and has authorization. Run this command in Terminal or CMD:

php spark make:filter AuthFilter

After running the command, it will create a file located in app/Filters. Open this file and enter the following code:

After creating the filter, we must add it to the filter configuration located in app/Config/Filters.php. We will create an alias for our filter.

app/Config/Filters.php

Step 8: Register Routes

Open the configuration file in app/Config/Routes.php and register this route:

$routes->group("api", function ($routes) {
$routes->post("register", "Register::index");
$routes->post("login", "Login::index");
$routes->get("users", "User::index", ['filter' => 'authFilter']);
});

Step 9: Run the Application

Now that we have completed the above steps, we will now run the application. To run the application, run this command:

php spark serve

Screenshots:

/api/register

/api/login

/api/users

That’s the tutorial that I can convey, hopefully it will be useful for all of my friends. Thank you for visiting my blog.

Don’t forget to visit the other tutorials:

Remove Public and Index.php from Codeigniter URL 4

Codeigniter 4 Tutorial ~ Login and Register

Thank you.

--

--