Build a Laravel API With Authentication Using JWT
Let’s build a PHP API with a security layer built-in
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…