This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Create A REST API in Laravel With Authentication Using Passport

Updated on  April 26, 2018

5 Min Read
Reading Time: 5 minutes

In today’s article, I am going to create a REST API in Laravel with Authentication using Laravel Passport.

laravel rest api authentication using passport

To demonstrate the API, I am going to continue the creating the Laravel powered ToDo app. I will use Laravel Passport access token in it. The Github repo for the todoappwithlaravel has been updated to this Passport API version.

Let’s get started.

Install and Setup Laravel Passport

To install Laravel Passport, run the following command:

Once the installation finishes, add the following provider in the config/app.php file:

Now, run the following command to migrate Laravel Passport tables:

Once all the tables have been migrated, run the following command to install it completely:

Now open AuthServiceProvider.php file. In the boot() method, add the following line:

Now open config/auth.php file and set passport as your API driver.

At this point, Passport has been installed. Next, I will configure it in the Users model.

Update the Users Model

Now head to the app folder and open users.php file. Add Laravel\Passport\HasApiTokens trait in it.

I will now create a controller which will handle REST API requests.

Create API Controller for the REST API in Laravel

Now run the following Artisan command to create a new controller.

Now open the newly created file. Let’s start with creating the method for generating access tokens.

All the hard work is now over. It is time to create routes for the API.

Create Routes for API

Now open routes/api.php file and add the following routes in it.

 

Now open the VerifyCsrfTokken.php file (located in app/Http/Controllers/Middleware) and find api/* in $except

It is now time to test how the API performs!

Testing the REST API

I will use Postman to test the API.

I will start with getting an access token for a registered user. For that, send a GET request to “api/login” with parameters, email and password.

Since I have not sent the email, it shows me an error. Now, let’s send a request without errors.

Next, I will create a new todo item with the API. For this, send a POST request to “api/todo” with parameters todo,category and description.

Since I have not added the access token, I  will get an error.

A new todo will be created. Now, try to update, delete and get users. I am sure every operation will be fine.

Conclusion

In this article, I first setup Laravel Passport in an existing project. I next created routes for the API. Once done, I tested the Laravel REST API and verified that it works as intended.

If you face any problems in implementing the API, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About The Author

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.