Being a backend developer, I would surely accept that third-party API is one thing that is being consumed in everyday work to integrate the functionality of providers in a project. API is an interface or code that acts as a mediator between two different programs. Also, people use it to complete mobile app development.
Usually, mobile apps are not connected to any database; that’s why many backend developers provide the data through API. Here, one that needs to be focused on is security. Whenever data is shared through API, it needs to be secured so that no third person can access it or make any changes.
Well! Creating an authenticated API is not a cakewalk. You can create it in different languages like Java, .net, node.js, or PHP.
Don’t know how to create authenticated APIs? Do not worry! Today we are going to learn the creation of Authenticated API in Laravel, which is a popular PHP framework used in most companies.
Now without any further ado, let’s jump onto the steps following which you can easily create an Authenticated API in Laravel.
Let’s begin!
Firstly, you need to open the terminal, but make sure you have Composer installed in your PC.
After opening the terminal, its time to create a Laravel project with the following command-
“composer create-project laravel/laravel laravel-api”
Now, open your project in your favorite code editor. To help you understand each step. I am using Visual studio code editor.
Then type in composer require Laravel/passport. This command will download the official Passport package from Composer Package Manager. To better understand, you can refer to the below-added screenshot-
Navigate to your .env file and update your DB settings.
Now go to your terminal and run the artisan command added below-
“php artisan migrate”
Once this command is in process, you will see that this will add all the required tables to your database.
Now it’s time to run PHP artisan passport: install command in terminal. It will help you to create the encryption key which is required for generating secure tokens.
Open app/User.php file and add the following code-
After the addition of code, open app/Providers/AuthServiceProvider.php file and again run the code added below-
Now, open config/auth.php and find ‘guards’ => and under this you will get ‘api’ => and under this, you need to change ‘driver’ to ‘passport’.
Now, you have to open routes/api.php and run the below-added code just as shown in the screenshot below-
Again, return to the terminal and type php artisan make:controller AuthController. This specific command will help you in creating a controller file that helps in handling different requests for example, login and register.
Once the controllers are created, you need to add the below-added code-
After creating the controller add the below code in it .
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppUser;
class AuthController extends Controller {
public function register(Request $request){
$request->validate([
‘name’ => ‘required|string’,
’email’ => ‘required|string|email|unique:users’,
‘password’ => ‘required|string|confirmed’
]);
$user = new User([
‘name’ => $request->name,
’email’ => $request->email,
‘password’ => bcrypt($request->password)
]);
$user->save();
return response()->json([
‘message’ => ‘Successfully created user!’
], 201);
}
public function login(Request $request){
$request->validate([
’email’ => ‘required|string|email’,
‘password’ => ‘required|string’
]);
$credentials = request([’email’, ‘password’]);
if(!Auth::attempt($credentials))
return response()->json([
‘message’ => ‘Unauthorized’
], 401);
$user = $request->user();
$tokenResult = $user->createToken(‘token’);
$token = $tokenResult->token;
$token->save();
return response()->json([
‘access_token’ => $tokenResult->accessToken,
‘token_type’ => ‘Bearer’
]);
}
public function get_user(Request $request){
return response()->json($request->user());
}
}
You are almost done with the coding part. Now, it’s time to test the Laravel API. Don’t know how to do it? Need not fret! Just follow the below added steps-
That’s it folks ! This is how you can create your own API’s.
Did you know that you can also create custom objects using API?
Hopefully, you have learned how to create your own API’s by following this step-by-step guide. Well! The screenshots and examples covered in this blog cover all the points, which will surely help you to keep a note of each pointer.
So, what are you waiting for? Feel free to explore the application and use it in a secure way.
Still need any help in using Laravel or any other web development activities? Get in touch with our web experts.