This guide assumes you have a basic understanding of how Pionia Security works. If you are new to Pionia, you can start by going through the API Tutorial guide.
Our target is to create a simple authentication system using JWT. We will use the Firebase/JWT package to illustrate how to create a simple authentication system.
This authentication should be able to intercept every request and attempt to authenticate the user.
We have a JwtUtility class that handles all our JWT logic.
getUserByUsername method fetches a user by username or email from the database. It also checks if the user is active. If the user is not found or not active, it throws an exception.
In normal circumstances, this method returns everything from the system_user table, including the password hash. However, if
$withPassword is set to false, it returns everything except the password hash. This is useful when you want to return the user object to the client.
jwtSettings method returns the JWT settings from the settings.ini file.
generateToken method generates a JWT token for the user. It fetches the user by username, generates a token, and updates the last login date in the database.
decodeToken method decodes the token and returns the decoded token.
We shall use this utility class in our JwtAuthenticationBackend.php file and in our login action.
In our JwtAuthenticationBackend.php in the authentications folder, replace the authenticate method with the following code:
We get the bearer_key from the settings.ini file. This is to make it easy to change the name of the Authorization header.
We check if the Authorization header is empty or does not start with the bearer_key. If it does not, we return null and the request will proceed but
unauthenticated.
Otherwise, we create a new ContextUserObject and decode the token. We then fetch the user by username and set the authenticated property to true and the user property to the user object we got from the database.
We return the ContextUserObject. This is what must be returned by the authenticate method.
In our settings.ini file, add the following settings:
Still in the settings.ini file, let’s register our Authentication Backend:
In our services directory, create a new file UserService.php. In normal circumstances, you should have the UserService class
already created for you. But if it’s not, you can create it in two ways. You can either create it manually or use the Pionia CLI to generate it for you.
Using the Pionia CLI:
Select Basic in the options provided by entering 1 or just hitting enter since it’s the default.
On the next action, you can write register,login to generate the register and login actions.
And you should have the new service created for you in the services directory.
However, I created mine manually, so what I have as login will be equivalent to your loginUser and register will be equivalent to your registerUser.
We have a UserService class that extends BaseRestService. This class has two actions: login and register.
In the login action, we require the username and password fields.
We then fetch the user by username and verify the password. If the password is correct, we generate a token and return it.
In the register action, we require the username, password, email, first_name, and last_name fields.
We set the role_code to USER if it’s not provided.
We validate the email and password fields. asEmail checks if we have a valid email and asPassword checks if the password passes
the minimum requirements(at least 1 special character, at least 1 capital letter, at least 1 digit, length of at least 8).
We then hash the password and save the user to the database.
We check if the username and email are already taken.
If everything is okay, we create the user in transaction and return the user object.
We define the columns that should be returned upon successful creation of the user.
We shall also need to register our UserService in the switch which shall handle henceforth all our requests.
Create a switch if it doesn’t exist in your switches directory. This can be created manually or using Pionia Cli
Using Pionia CLI
You must target the version the switch is targeting, the above targets version 2 which can be accessed on /api/v2/.
The above command creates V2Switch.php in the switches directory.
You then have to register it in your routes.php file.
Under normal circumstances, the MainApiSwitch.php that ships with the template is enough!
And add to your registerServices method in the MainApiSwitch.php file the following code:
This is a simple way to create an authentication system using JWT in Pionia. You can extend this to include more features like password reset, email verification, etc. You can also use other JWT libraries like lcobucci/jwt or spomky-labs/jose if you prefer.
Remember to always hash your passwords before saving them to the database. You can use the password_hash function in PHP.