Skip to content

Commit 1ad01f3

Browse files
committed
Laravel rest api with passport
1 parent f883a7b commit 1ad01f3

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

README.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Here, we are going to implement REST API with Passport authentication.
44
We will build a CRUD for Blog's API using Laravel Passport Authentication.
55

66
#### **What is REST API?**
7-
Rest API (Representational State Transfer) api's are web standards base architecture and uses HTTP Protocol for exchanging data between applications or systems.
7+
Rest API (Representational State Transfer) api's are web standards-based architecture and uses HTTP Protocol for exchanging data between applications or systems.
88
In RESTFUL web service HTTP methods like GET, POST, PUT and DELETE can be used to perform CRUD operations.
99

1010
#### **What is Passport?**
@@ -18,7 +18,6 @@ The passport package will register its own database migrations. This command wil
1818

1919
#### **How to implement Passport in REST API?**
2020
In below example, we will create CRUD operation for Blog's API using passport authentication.
21-
So, let's start.
2221

2322
**1) Create laravel project.**
2423

@@ -30,11 +29,11 @@ Go to your project path in terminal and run below command for passport authentic
3029

3130
composer require laravel/passport
3231

33-
After successfully installing the package, we are required to get default migration for creating new passport tables in our database. so let's run bellow command:
32+
After the successful installation of a package, we required to get default migration for creating new passport tables in our database. let's run below command:
3433

3534
php artisan migrate
3635

37-
Next, we need to install passport using command, Using passport:install command, it will create token keys for security. So let's run bellow command:
36+
Next, we need to install the Passport using command, and it will create token keys for security. let's run bellow command:
3837

3938
php artisan passport:install
4039
@@ -48,7 +47,7 @@ In user model, use HasApiTokens trait class of passport.
4847
use Illuminate\Foundation\Auth\User as Authenticatable;
4948
use Illuminate\Notifications\Notifiable;
5049
use Laravel\Passport\HasApiTokens;
51-
50+
5251
class User extends Authenticatable
5352
{
5453
use Notifiable,HasApiTokens;
@@ -133,9 +132,9 @@ Change config/auth.php file.
133132
.....
134133
]
135134

136-
**4) Add Blog table and model**
135+
**4) Create Blog table and model**
137136

138-
Create migration file and model for Blog table by below command.
137+
Create migration file and model for the Blog table by below command.
139138

140139
php artisan make:migration create_blogs_table
141140

@@ -177,7 +176,7 @@ Now open your database/migration/'your blog migration file'. and add your code h
177176
}
178177
}
179178

180-
Add below code to your Blog model file.
179+
Update below code to your Blog model file.
181180

182181
<?php
183182

@@ -205,13 +204,13 @@ Now open your database/migration/'your blog migration file'. and add your code h
205204
206205
**6) Create Controller files**
207206

208-
We will create two controller files. One for Register and another for Blogs by below command.
207+
Create two controller files. One for Register and another for Blog. Use below commands to create files.
209208

210209
php artisan make:controller RegisterController
211210
212211
php artisan make:controller BlogController
213212
214-
Add below code to register Controller.
213+
Update below code to register Controller.
215214

216215
<?php
217216

@@ -256,7 +255,7 @@ Add below code to register Controller.
256255
}
257256
}
258257

259-
And add below code to BlogController
258+
Update below code to BlogController
260259

261260
<?php
262261

@@ -359,7 +358,7 @@ And add below code to BlogController
359358
{
360359
$blog = Blog::find($id);
361360

362-
//Check if blog found or not.
361+
//Check if the blog found or not.
363362
if (is_null($blog)) {
364363
$message = 'Blog not found.';
365364
$status = false;
@@ -429,15 +428,15 @@ Now run below command in terminal:
429428

430429
**8) Run APIs**
431430

432-
Let's run out api's through postman.
431+
Let's run API through postman.
432+
433+
Run authentication API to get passport access token & copy the token and use the same in Header of other CRUD APIs.
434+
Follow all screenshots to understand how to generate a token and use it in APIs to authenticate a user.
433435

434436
Test Register API:
435437

436438
![picture](img/register.png)
437439

438-
Let's test blog's CRUD. but before that, we need authentication to perform CRUD operation. because we used passport authentication.
439-
So, we will access 'token' from register API's response. and add the token to each API's headers like below:
440-
441440
![picture](img/headers.png)
442441

443442
Add Blog:
@@ -461,15 +460,15 @@ Delete Blog:
461460
![picture](img/deleteBlog.png)
462461

463462

464-
Now, we are going to write a test cases for register and CRUD api.
463+
Writing test cases for the register and CRUD APIs.
465464

466-
So, let's make test files by below command.
465+
Commands to create test case file :
467466

468467
php artisan make:test RegisterTest
469468

470469
php artisan make:test BlogTest
471470

472-
Add below code to `tests/Feature/RegisterTest.php` file.
471+
Update below code to `tests/Feature/RegisterTest.php` file.
473472

474473
<?php
475474

@@ -531,7 +530,7 @@ Add below code to `tests/Feature/RegisterTest.php` file.
531530
}
532531
}
533532

534-
Add below code to `tests/Feature/BlogTest.php` file.
533+
Update below code to `tests/Feature/BlogTest.php` file.
535534

536535
<?php
537536

@@ -724,11 +723,16 @@ Add below code to `tests/Feature/BlogTest.php` file.
724723
}
725724
}
726725

727-
now run the test cases to terminal by below command:
726+
Run the test cases to terminal by below command:
728727

729728
php vendor/bin/phpunit /tests/Feature/RegisterTest.php
730729

731730
php vendor/bin/phpunit /tests/Feature/BlogTest.php
731+
732+
That’s it. Happy Coding :)
733+
734+
Hope this blog helps to understand how to use a passport package with laravel to create secure APIs.
735+
732736

733737

734738

0 commit comments

Comments
 (0)