πŸ”₯🀩 (#35) Implementing Shield Authentication in CodeIgniter 4 REST APIs | Adding a New Project API

Create mind-blowing REST APIs with CodeIgniter 4 using Shield authentication! In this tutorial, we’ll tackle the add new project API. After nailing user authentication APIs, we dive into project development. By decoding user input, we ensure data security and seamless API functionality. Cheers to successful project creation! πŸš€ #CodeIgniter #RESTAPIs #ShieldAuthentication

Introduction

In this video, we will create the "Add New Project" API, continuing our development of RESTful APIs using Shield authentication. We have previously completed the user authentication APIs for registration, login, profile, and log out. Now, we will focus on developing the API for the "Project Controller".

Development Process

To create the "Add New Project" API, we will work on the "add project" method inside the "project controller.php" file. This method will receive project data as well as the authorization token value inside the header.

Data Requirements

For creating a new project, we need data for user ID, name, and budget. The user ID value will be obtained from the authorization token, while the project name and budget will be obtained as form data.

API Development

We start by defining the validation rules for creating a project. All the fields are required, so the validation rules for "name" and "budget" will be "required".

FieldValidation Rule
NameRequired
BudgetRequired

If the validation fails, we prepare the response with status as false, an error message, and the validation errors. If the validation passes, we proceed to interact with the project table using the model.

Inserting Project Data

We retrieve the user ID from the authorization token using the "or" helper function and create a project object to interact with the project table. The project data is prepared with the user ID, name, and budget, and then inserted into the project table.

Access Control

The "Add New Project" API is a protected route that requires the authorization token to be passed inside the header.

Testing the API

We test the API using Postman, passing the authorization token in the header and the project name and budget in the body. We receive appropriate responses for valid and invalid inputs, confirming that the API is functioning correctly.

Successful Creation

Upon successful creation, the project data is saved in the project table.

Conclusion

In this session, we have successfully developed the "Add New Project" API and tested its functionality using Postman. This API is now ready for use in our CodeIgniter 4 RESTful application. Thank you for watching, and stay tuned for the next video on listing user-wise projects.

About the Author

About the Channel:

Share the Post:
en_GBEN_GB