Build a Laravel API With Authentication Using JWT

Let’s build a PHP API with a security layer built-in

JIMOH Mofoluwasho
9 min readDec 10, 2020
Photo by Masaaki Komori on Unsplash.

The Laravel framework gives us a lot of things out of the box. One of such things is Authentication. Authentication and authorization are very important when designing an application. When coding an API, you should be able to authenticate users (finding out who the user is), authorize users (allowing a user to only access specific routes based on some privileges.

In this short tutorial, I am going to show how you can provide authentication for your API in Laravel. There are different ways of achieving this. Some of the ways are:

1. Laravel Authentication (Laravel’s built in Authentication)

2. Laravel guards: using a JWT library (We shall be using this in the tutorial)

3. Laravel Socialite

4. Laravel Passport

5. Laravel Sanctum

In this tutorial, we shall be looking at the Token-based authentication. Token based authentication is very common these days. Used for authenticating web and mobile clients. It makes it very easy to work with devices across different platforms. When a user is authenticated, the token is stored on the user’s browser. Every request to protected routes by the user will require…

--

--

JIMOH Mofoluwasho

Mechanical Engineer, Full Stack Developer (Laravel, Nodejs, React, React Native). I am a Software Engineer who is passionate about building digital products.