{"id":525,"date":"2023-01-30T19:06:36","date_gmt":"2023-01-30T19:06:36","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=525"},"modified":"2023-01-30T19:07:19","modified_gmt":"2023-01-30T19:07:19","slug":"how-to-implement-json-web-token-jwt-authentication-in-laravel","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/how-to-implement-json-web-token-jwt-authentication-in-laravel\/","title":{"rendered":"How to implement JSON Web Token (JWT) authentication in Laravel"},"content":{"rendered":"<div class=\"post-content\">\n<blockquote class=\"wp-block-quote\">\n<p>Here&#8217;s an example of how to implement JSON Web Token (JWT) authentication in Laravel:<\/p>\n<\/blockquote>\n\n\n\n<h4>Install the required packages:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require tymon\/jwt-auth\n<\/code><\/pre>\n\n\n\n<h4>Add the service provider in config\/app.php:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>'providers' =&gt; &#91;\n    ...\n    Tymon\\JWTAuth\\Providers\\LaravelServiceProvider::class,\n],\n<\/code><\/pre>\n\n\n\n<h4>Publish the configuration file:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan vendor:publish --provider=\"Tymon\\JWTAuth\\Providers\\LaravelServiceProvider\"\n<\/code><\/pre>\n\n\n\n<h4>Generate the secret key:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan jwt:secret\n<\/code><\/pre>\n\n\n\n<h4>Add the middleware to the app\/Http\/Kernel.php file:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>protected $routeMiddleware = &#91;\n    ...\n    'jwt.auth' =&gt; \\Tymon\\JWTAuth\\Middleware\\GetUserFromToken::class,\n    'jwt.refresh' =&gt; \\Tymon\\JWTAuth\\Middleware\\RefreshToken::class,\n];\n<\/code><\/pre>\n\n\n\n<h4>Create a LoginController with the following code:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse Tymon\\JWTAuth\\Facades\\JWTAuth;\n\nclass LoginController extends Controller\n{\n    public function login(Request $request)\n    {\n        $credentials = $request-&gt;only('email', 'password');\n\n        if (! $token = JWTAuth::attempt($credentials)) {\n            return response()-&gt;json(&#91;'error' =&gt; 'invalid_credentials'], 400);\n        }\n\n        return response()-&gt;json(compact('token'));\n    }\n}\n<\/code><\/pre>\n\n\n\n<h4>Add a route in the web.php file to point to the LoginController:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::post('login', 'LoginController@login');\n<\/code><\/pre>\n\n\n\n<h4>To protect a route, add the &#8216;jwt.auth&#8217; middleware:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::get('protected', function() {\n    return response()-&gt;json(&#91;\n        'message' =&gt; 'Access granted'\n    ]);\n})-&gt;middleware('jwt.auth');\n<\/code><\/pre>\n\n\n\n<p>This is a basic example of how to integrate JWT authentication into a Laravel application. You can customize and extend the implementation based on your specific needs.<\/p>\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example of how to implement JSON Web Token<a href=\"https:\/\/mouryasolutions.in\/blog\/how-to-implement-json-web-token-jwt-authentication-in-laravel\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/525"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=525"}],"version-history":[{"count":2,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":527,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/525\/revisions\/527"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}