<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laravel API Authentication | ServerAvatar</title>
	<atom:link href="https://serveravatar.com/tag/laravel-api-authentication/feed/" rel="self" type="application/rss+xml" />
	<link>https://serveravatar.com</link>
	<description>The first, Fully Hybrid Cloud Hosting Solution.</description>
	<lastBuildDate>Wed, 24 Sep 2025 06:26:06 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://serveravatar.com/wp-content/uploads/2025/02/cropped-favicon-32x32.png</url>
	<title>Laravel API Authentication | ServerAvatar</title>
	<link>https://serveravatar.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel API Authentication: Secure Your Endpoints Effectively</title>
		<link>https://serveravatar.com/laravel-api-authentication/</link>
		
		<dc:creator><![CDATA[Meghna Meghwani]]></dc:creator>
		<pubDate>Sat, 26 Jul 2025 12:05:43 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Guide]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[API Security]]></category>
		<category><![CDATA[Laravel API Authentication]]></category>
		<category><![CDATA[Laravel Middleware]]></category>
		<category><![CDATA[Laravel Sanctum]]></category>
		<category><![CDATA[Laravel Tips]]></category>
		<category><![CDATA[Secure Laravel APIs]]></category>
		<guid isPermaLink="false">https://serveravatar.com/?p=26695</guid>

					<description><![CDATA[<p>Laravel API Authentication is essential when you&#8217;re building APIs with Laravel, as securing your endpoints is absolutely necessary. In today&#8217;s digital world, APIs are the bridges between applications, but they can also be the weakest link if not properly protected. Laravel, being one of the most popular PHP frameworks, provides multiple ways to secure APIs, [&#8230;]</p>
<p>The post <a href="https://serveravatar.com/laravel-api-authentication/">Laravel API Authentication: Secure Your Endpoints Effectively</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img fetchpriority="high" decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28182" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-2048x253.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p><strong>Laravel API Authentication is essential when you&#8217;re building APIs with Laravel, as securing your endpoints is absolutely necessary.</strong> In today&#8217;s digital world, APIs are the bridges between applications, but they can also be the weakest link if not properly protected. Laravel, being one of the most popular PHP frameworks, provides multiple ways to secure APIs, and understanding these options is critical for developers looking to build robust, secure applications.</p>



<p>In this guide, we will walk you through everything you need to know about Laravel API authentication. Whether you are beginner or experienced developer, this article will help you to implement the most effective and scalable authentication techniques that Laravel offers.</p>



<h2 class="wp-block-heading">What Is API Authentication?</h2>



<p>API authentication is process of verifying identity of user or system that is attempting to access your API. Without authentication, anyone can access your endpoints, making significant risk for your application.</p>



<p>In Laravel, authentication allows only verified users or systems to make requests, ensuring your data and services stay safe.</p>



<h2 class="wp-block-heading">Why Laravel for API Development?</h2>



<p>Laravel has quickly become favorite for API development due to its:</p>



<ul class="wp-block-list">
<li>Elegant syntax and structure</li>



<li>Built-in support for RESTful API creation</li>



<li>Robust authentication packages</li>



<li>Extensive documentation</li>



<li>Strong community support</li>
</ul>



<p>It offers multiple ways for implement authentication, depending on use case, scalability needs, and preferred architecture.</p>



<h2 class="wp-block-heading">Understanding Laravel Sanctum</h2>



<p>Laravel Sanctum is lightweight authentication system built for SPAs (Single Page Applications), mobile applications, and simple token-based APIs.</p>



<p><strong>Key Features:</strong></p>



<ul class="wp-block-list">
<li>Token-based authentication</li>



<li>CSRF protection</li>



<li>Easy to use with frontend frameworks like Vue.js or React</li>



<li>Cookie-based session management for SPAs</li>
</ul>



<p><strong>How Sanctum Works:</strong></p>



<p>Sanctum generates personal access token for every user. When making API request, the client must include this token in the Authorization header:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Authorization: Bearer your-token-here</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">Authorization:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Bearer</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">your-token-here</span></span></code></pre></div>



<p><strong>When to Use Sanctum:</strong></p>



<ul class="wp-block-list">
<li>If you are building single page application (SPA).</li>



<li>When you prefer simplicity over complex OAuth flows.</li>



<li>When you need first party authentication for web applications.</li>
</ul>



<h2 class="wp-block-heading">Laravel Passport: OAuth2 Implementation</h2>



<p>Laravel Passport provide full OAuth2 server implementation for your Laravel application.</p>



<p><strong>Benefits:</strong></p>



<ul class="wp-block-list">
<li>Handles full OAuth2 flows (Authorization Code, Client Credentials, etc.)</li>



<li>Great for third party application</li>



<li>Built on top of League OAuth2 Server</li>
</ul>



<h2 class="wp-block-heading">Basic Setup:</h2>



<p><strong>Step 1: Install <a href="https://laravel.com/" target="_blank" rel="noopener nofollow" title="">Laravel</a> (If not installed)</strong></p>



<ul class="wp-block-list">
<li>Make sure you have installed Composer. After that install Laravel Installer globally:</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>composer global require laravel/installer</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">composer</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">global</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">require</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">laravel/installer</span></span></code></pre></div>



<p>Make sure ~/.composer/vendor/bin is in your system’s PATH.</p>



<p>Now, create a new Laravel project:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>laravel new laravel-backend
cd laravel-backend</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">laravel</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">new</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">laravel-backend</span></span>
<span class="line"><span style="color: #DCDCAA">cd</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">laravel-backend</span></span></code></pre></div>



<p>Alternatively, you can also use &#8220;composer create-project laravel/laravel laravel-backend&#8221; command.</p>



<p><strong>Step 2: Install Passport<br></strong>Install Passport using Composer:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>composer require laravel/passport</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">composer</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">require</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">laravel/passport</span></span></code></pre></div>



<p><strong>Step 3: Publish Configuration and Migration Files</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan vendor:publish --tag=passport-config
php artisan vendor:publish --tag=passport-migrations</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">vendor:publish</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--tag=passport-config</span></span>
<span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">vendor:publish</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--tag=passport-migrations</span></span></code></pre></div>



<p>This will publish:</p>



<ul class="wp-block-list">
<li>Passport config file (config/passport.php)</li>



<li>Migration files for tokens, clients, etc.</li>
</ul>



<p><strong>Step 4: Run Migrations</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan migrate</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">migrate</span></span></code></pre></div>



<p><strong>Step 5: Install Passport</strong></p>



<p>This command creates encryption keys and personal access &amp; password grant clients:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan passport:install</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">passport:install</span></span></code></pre></div>



<p>You’ll see output with<strong>&nbsp;client ID&nbsp;</strong>and&nbsp;<strong>secret keys</strong>. Please note these down if you use them for testing OAuth2 flows.</p>



<p><strong>Step 6: Configure Authentication Guards</strong><br>Open config/auth.php, and update the api guard to use passport:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>'guards' => [
    'api' => &#91;
        'driver' => 'passport',
        'provider' => 'users',
    &#93;,
],</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">&#39;guards&#39;</span><span style="color: #D4D4D4"> =&gt; [</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">&#39;api&#39;</span><span style="color: #D4D4D4"> =&gt; &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">&#39;driver&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;passport&#39;,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">&#39;provider&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;users&#39;,</span></span>
<span class="line"><span style="color: #D4D4D4">    &#93;,</span></span>
<span class="line"><span style="color: #D4D4D4">],</span></span></code></pre></div>



<p><strong>Step 7: Add PassportServiceProvider (for Laravel &lt; 10)</strong></p>



<p>If you&#8217;re using Laravel &lt; 10 and it&#8217;s not auto-discovered, register it manually in config/app.php:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>'providers' => &#91;
    Laravel\Passport\PassportServiceProvider::class,
&#93;,</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">&#39;providers&#39;</span><span style="color: #D4D4D4"> =&gt; &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">Laravel\Passport\PassportServiceProvider::class,</span></span>
<span class="line"><span style="color: #D4D4D4">&#93;,</span></span></code></pre></div>



<p><strong>Step 8: Enable Passport in AuthServiceProvider</strong></p>



<p>Open app/Providers/AuthServiceProvider.php and add the following in the boot() method:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>use Laravel\Passport\Passport;

public function boot()
{
    Passport::routes();
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Laravel</span><span style="color: #D7BA7D">\P</span><span style="color: #CE9178">assport</span><span style="color: #D7BA7D">\P</span><span style="color: #CE9178">assport</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">boot</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">Passport::routes</span><span style="color: #D4D4D4">();</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p><strong>Step 9: Create API Route in routes/api.php</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Route::middleware('auth:api')->group(function () {
    Route::get('/user', &#91;ApiController::class, 'getUser'&#93;);
});</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #4EC9B0">Route</span><span style="color: #D4D4D4">::</span><span style="color: #DCDCAA">middleware</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&#39;auth:api&#39;</span><span style="color: #D4D4D4">)-&gt;</span><span style="color: #DCDCAA">group</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> () {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #4EC9B0">Route</span><span style="color: #D4D4D4">::</span><span style="color: #DCDCAA">get</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&#39;/user&#39;</span><span style="color: #D4D4D4">, &#91;</span><span style="color: #4EC9B0">ApiController</span><span style="color: #D4D4D4">::</span><span style="color: #569CD6">class</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;getUser&#39;</span><span style="color: #D4D4D4">&#93;);</span></span>
<span class="line"><span style="color: #D4D4D4">});</span></span></code></pre></div>



<p><strong>Step 10: Create the API Controller</strong><br>Generate the controller:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan make:controller ApiController</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">make:controller</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">ApiController</span></span></code></pre></div>



<p>Then open &#8220;app/Http/Controllers/ApiController.php&#8221;:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function getUser(Request $request)
    {
        return response()->json($request->user());
    }
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">namespace</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">App\Http\Controllers</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">use</span><span style="color: #D4D4D4"> Illuminate\Http\</span><span style="color: #4EC9B0">Request</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">class</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">ApiController</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">extends</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">Controller</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #569CD6">public</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">getUser</span><span style="color: #D4D4D4">(</span><span style="color: #4EC9B0">Request</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #DCDCAA">json</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;</span><span style="color: #DCDCAA">user</span><span style="color: #D4D4D4">());</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p><strong>Step 11: Add API Key to Config</strong><br>Add API key in config/services.php:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>'api_key' => env('API_KEY'),</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #CE9178">&#39;api_key&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #DCDCAA">env</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&#39;API_KEY&#39;</span><span style="color: #D4D4D4">),</span></span></code></pre></div>



<p>Then add API key in .env file:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>API_KEY=your-secret-api-key</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">API_KEY</span><span style="color: #D4D4D4">=</span><span style="color: #CE9178">your-secret-api-key</span></span></code></pre></div>



<p><strong>Step 12: Create API Key Middleware</strong></p>



<p>Generate middleware by using below command:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan make:middleware ApiKeyMiddleware</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">make:middleware</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">ApiKeyMiddleware</span></span></code></pre></div>



<p>Now open &#8220;app/Http/Middleware/ApiKeyMiddleware.php&#8221; and add the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class ApiKeyMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $apiKey = $request->header('X-API-KEY');

        if ($apiKey !== config('services.api_key')) {
            return response()->json(&#91;'message' => 'Unauthorized'&#93;, 401);
        }

        return $next($request);
    }
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">namespace</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">App</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\M</span><span style="color: #CE9178">iddleware</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Closure</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Illuminate</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\R</span><span style="color: #CE9178">equest</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">class</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">ApiKeyMiddleware</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">handle</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">Request</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #CE9178">,</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Closure</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$next</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">$apiKey</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;</span><span style="color: #CE9178">header</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">&#39;X-API-KEY&#39;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">$apiKey</span><span style="color: #D4D4D4"> !== </span><span style="color: #CE9178">config</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">&#39;services.api_key&#39;</span><span style="color: #D4D4D4">)) {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(&#91;</span><span style="color: #CE9178">&#39;message&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;Unauthorized&#39;</span><span style="color: #D4D4D4">&#93;, 401);</span></span>
<span class="line"><span style="color: #D4D4D4">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$next</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p><strong>Step 13: Register Middleware</strong><br>Open &#8220;app/Http/Kernel.php&#8221;, and register your middleware:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>protected $routeMiddleware = &#91;
    // ...
    'api.key' => \App\Http\Middleware\ApiKeyMiddleware::class,
&#93;;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">protected</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$routeMiddleware</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">//</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">...</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">&#39;api.key&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #D7BA7D">\A</span><span style="color: #CE9178">pp</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\M</span><span style="color: #CE9178">iddleware</span><span style="color: #D7BA7D">\A</span><span style="color: #CE9178">piKeyMiddleware::class,</span></span>
<span class="line"><span style="color: #D4D4D4">&#93;;</span></span></code></pre></div>



<p><strong>Step 14: Protect Routes with API Key</strong><br>In &#8220;routes/api.php&#8221;:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Route::middleware('api.key')->get('/custom-data', function () {
    return &#91;'data' => 'Secured using API Key'&#93;;
});</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">Route::middleware(</span><span style="color: #DCDCAA">&#39;api.key&#39;</span><span style="color: #D4D4D4">)-&gt;get(</span><span style="color: #DCDCAA">&#39;/custom-data&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> () {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> &#91;</span><span style="color: #CE9178">&#39;data&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;Secured using API Key&#39;&#93;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">});</span></span></code></pre></div>



<h2 class="wp-block-heading">Ideal Use Case:</h2>



<p>Laravel Passport is ideal for:</p>



<ul class="wp-block-list">
<li>Third-party app integration (e.g., allow other apps to access your API securely)</li>



<li>Large-scale applications needing full OAuth2 flows</li>



<li>Multi-client systems (e.g., web, mobile, desktop clients)</li>



<li>When you need token scopes, personal access tokens, or authorization codes</li>
</ul>



<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28184" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-2048x254.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading">JWT Authentication with Laravel</h2>



<p>JSON Web Tokens (JWT) is another popular method to securing Laravel APIs.</p>



<p><strong>Why Use JWT?</strong></p>



<ul class="wp-block-list">
<li>Stateless authentication</li>



<li>Scalable for microservices</li>



<li>No session storage required</li>



<li>JWT is a great for distributed systems where sessions are impractical.</li>
</ul>



<p><strong>Recommended Packages:</strong></p>



<ul class="wp-block-list">
<li><strong>tymon/jwt-auth</strong>&nbsp;is the most widely used JWT package for Laravel.</li>
</ul>



<h2 class="wp-block-heading">Setup Guide:</h2>



<p><strong>Step 1: Install Laravel</strong></p>



<p>If you don’t already have a Laravel project, create one using below command:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>composer create-project laravel/laravel jwt-auth-api
cd jwt-auth-api</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">composer</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">create-project</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">laravel/laravel</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">jwt-auth-api</span></span>
<span class="line"><span style="color: #DCDCAA">cd</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">jwt-auth-api</span></span></code></pre></div>



<p><strong>Step 2: Install JWT Auth Package</strong><br>Use Composer to install the &#8220;tymon/jwt-auth&#8221; package:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>composer require tymon/jwt-auth</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">composer</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">require</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">tymon/jwt-auth</span></span></code></pre></div>



<p><strong>Step 3: Publish Config File</strong><br>This publishes the JWT config file (config/jwt.php):</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">vendor:publish</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--provider=</span><span style="color: #CE9178">&quot;Tymon\JWTAuth\Providers\LaravelServiceProvider&quot;</span></span></code></pre></div>



<p><strong>Step 4: Generate JWT Secret</strong><br>Run below command to generate the JWT secret key and automatically add it to your .env file:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan jwt:secret</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">jwt:secret</span></span></code></pre></div>



<p><strong>Step 5: Update User Model</strong><br>Open &#8220;app/Models/User.php&#8221; and implement the JWTSubject interface:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    // ...

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Tymon</span><span style="color: #D7BA7D">\J</span><span style="color: #CE9178">WTAuth</span><span style="color: #D7BA7D">\C</span><span style="color: #CE9178">ontracts</span><span style="color: #D7BA7D">\J</span><span style="color: #CE9178">WTSubject</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">class</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">User</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">extends</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Authenticatable</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">implements</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">JWTSubject</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">//</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">...</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">getJWTIdentifier</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$this</span><span style="color: #D4D4D4">-&gt;</span><span style="color: #CE9178">getKey</span><span style="color: #D4D4D4">();</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">getJWTCustomClaims</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> [];</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p><strong>Step 6: Create AuthController</strong><br>This controller will handle login, registration, logout, and token refresh.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>php artisan make:controller AuthController</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">php</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">artisan</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">make:controller</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">AuthController</span></span></code></pre></div>



<p>Now open &#8220;app/Http/Controllers/AuthController.php&#8221; and add the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Tymon\JWTAuth\Facades\JWTAuth;

class AuthController extends Controller
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), &#91;
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string|min:6',
        &#93;);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 400);
        }

        $user = User::create(&#91;
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        &#93;);

        $token = JWTAuth::fromUser($user);

        return response()->json(compact('user', 'token'), 201);
    }

    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');

        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(&#91;'error' => 'Invalid credentials'&#93;, 401);
        }

        return response()->json(compact('token'));
    }

    public function logout()
    {
        JWTAuth::invalidate(JWTAuth::getToken());

        return response()->json(&#91;'message' => 'Successfully logged out'&#93;);
    }

    public function me()
    {
        return response()->json(JWTAuth::user());
    }

    public function refresh()
    {
        return response()->json(&#91;
            'token' => JWTAuth::refresh(JWTAuth::getToken())
        &#93;);
    }
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">namespace</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">App</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\C</span><span style="color: #CE9178">ontrollers</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Illuminate</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\R</span><span style="color: #CE9178">equest</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">App</span><span style="color: #D7BA7D">\M</span><span style="color: #CE9178">odels</span><span style="color: #D7BA7D">\U</span><span style="color: #CE9178">ser</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Illuminate</span><span style="color: #D7BA7D">\S</span><span style="color: #CE9178">upport</span><span style="color: #D7BA7D">\F</span><span style="color: #CE9178">acades</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ash</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Illuminate</span><span style="color: #D7BA7D">\S</span><span style="color: #CE9178">upport</span><span style="color: #D7BA7D">\F</span><span style="color: #CE9178">acades</span><span style="color: #D7BA7D">\V</span><span style="color: #CE9178">alidator</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Tymon</span><span style="color: #D7BA7D">\J</span><span style="color: #CE9178">WTAuth</span><span style="color: #D7BA7D">\F</span><span style="color: #CE9178">acades</span><span style="color: #D7BA7D">\J</span><span style="color: #CE9178">WTAuth</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">class</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">AuthController</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">extends</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Controller</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">register</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">Request</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">$validator</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">Validator::make</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">$request-&gt;all</span><span style="color: #D4D4D4">(), &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;name&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;required|string|max:255&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;email&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;required|string|email|unique:users&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;password&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;required|string|min:6&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">        &#93;);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #DCDCAA">$validator-&gt;fails</span><span style="color: #D4D4D4">()) {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">$validator-&gt;errors</span><span style="color: #D4D4D4">(), 400);</span></span>
<span class="line"><span style="color: #D4D4D4">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">$user</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">User::create</span><span style="color: #D4D4D4">(&#91;</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;name&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;name,</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;email&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;email,</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;password&#39;</span><span style="color: #D4D4D4"> =&gt; Hash::make(</span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;password),</span></span>
<span class="line"><span style="color: #D4D4D4">        &#93;);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">$token</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">JWTAuth::fromUser</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$user</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">compact(</span><span style="color: #DCDCAA">&#39;user&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;token&#39;</span><span style="color: #D4D4D4">)</span><span style="color: #CE9178">,</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">201</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">login</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">Request</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">$credentials</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">=</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$request</span><span style="color: #D4D4D4">-&gt;</span><span style="color: #CE9178">only</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">&#39;email&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;password&#39;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (!</span><span style="color: #9CDCFE">$token</span><span style="color: #D4D4D4"> = JWTAuth::attempt(</span><span style="color: #9CDCFE">$credentials</span><span style="color: #D4D4D4">)) {</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(&#91;</span><span style="color: #CE9178">&#39;error&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;Invalid credentials&#39;</span><span style="color: #D4D4D4">&#93;, 401);</span></span>
<span class="line"><span style="color: #D4D4D4">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">compact(</span><span style="color: #DCDCAA">&#39;token&#39;</span><span style="color: #D4D4D4">));</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">logout</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">JWTAuth::invalidate(JWTAuth::getToken(</span><span style="color: #D4D4D4">));</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(&#91;</span><span style="color: #CE9178">&#39;message&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;Successfully logged out&#39;</span><span style="color: #D4D4D4">&#93;);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">me</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(</span><span style="color: #DCDCAA">JWTAuth::user</span><span style="color: #D4D4D4">());</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">public</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">function</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">refresh</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">response</span><span style="color: #D4D4D4">()-&gt;</span><span style="color: #CE9178">json</span><span style="color: #D4D4D4">(&#91;</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #CE9178">&#39;token&#39;</span><span style="color: #D4D4D4"> =&gt; JWTAuth::refresh(JWTAuth::getToken())</span></span>
<span class="line"><span style="color: #D4D4D4">        &#93;);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p><strong>Step 7: Define API Routes</strong><br>Open &#8220;routes/api.php&#8221; and add the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>use App\Http\Controllers\AuthController;

Route::post('register', &#91;AuthController::class, 'register'&#93;);
Route::post('login', &#91;AuthController::class, 'login'&#93;);

Route::middleware('auth:api')->group(function () {
    Route::post('logout', &#91;AuthController::class, 'logout'&#93;);
    Route::get('me', &#91;AuthController::class, 'me'&#93;);
    Route::post('refresh', &#91;AuthController::class, 'refresh'&#93;);
});</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">use</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">App</span><span style="color: #D7BA7D">\H</span><span style="color: #CE9178">ttp</span><span style="color: #D7BA7D">\C</span><span style="color: #CE9178">ontrollers</span><span style="color: #D7BA7D">\A</span><span style="color: #CE9178">uthController</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">Route::post(</span><span style="color: #DCDCAA">&#39;register&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> &#91;AuthController::class, </span><span style="color: #CE9178">&#39;register&#39;&#93;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #DCDCAA">Route::post(</span><span style="color: #DCDCAA">&#39;login&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> &#91;AuthController::class, </span><span style="color: #CE9178">&#39;login&#39;&#93;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #DCDCAA">Route::middleware(</span><span style="color: #DCDCAA">&#39;auth:api&#39;</span><span style="color: #D4D4D4">)-&gt;group(</span><span style="color: #DCDCAA">function</span><span style="color: #D4D4D4"> () {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">Route::post(</span><span style="color: #DCDCAA">&#39;logout&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> &#91;AuthController::class, </span><span style="color: #CE9178">&#39;logout&#39;&#93;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">Route::get(</span><span style="color: #DCDCAA">&#39;me&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> &#91;AuthController::class, </span><span style="color: #CE9178">&#39;me&#39;&#93;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">Route::post(</span><span style="color: #DCDCAA">&#39;refresh&#39;</span><span style="color: #DCDCAA">,</span><span style="color: #D4D4D4"> &#91;AuthController::class, </span><span style="color: #CE9178">&#39;refresh&#39;&#93;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">});</span></span></code></pre></div>



<p><strong>Step 8: Update auth.php Configuration<br></strong>Open &#8220;config/auth.php&#8221; and change the api guard to use jwt:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>'guards' => [
    'api' => &#91;
        'driver' => 'jwt',
        'provider' => 'users',
    &#93;,
],</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">&#39;guards&#39;</span><span style="color: #D4D4D4"> =&gt; [</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">&#39;api&#39;</span><span style="color: #D4D4D4"> =&gt; &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">&#39;driver&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;jwt&#39;,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">&#39;provider&#39;</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #CE9178">&#39;users&#39;,</span></span>
<span class="line"><span style="color: #D4D4D4">    &#93;,</span></span>
<span class="line"><span style="color: #D4D4D4">],</span></span></code></pre></div>



<h2 class="wp-block-heading">Ideal Use Case for JWT</h2>



<p>JWT is ideal for:</p>



<ul class="wp-block-list">
<li>APIs used by mobile apps or JavaScript frontends (like React, Vue)</li>



<li>Single Page Applications (SPAs)</li>



<li>Decoupled frontend &amp; backend projects</li>



<li>Microservices communication</li>



<li>Stateless authentication without server-side sessions</li>
</ul>



<h2 class="wp-block-heading">Token vs Session Authentication</h2>



<p>Understanding the difference will helps you to make the right decision for your application:</p>



<p><strong>FeatureToken AuthenticationSession Authentication</strong>Storage | Client-side | Server-side (Laravel sessions)</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th><strong>Feature</strong></th><th><strong>Token Authentication</strong></th><th><strong>Session Authentication</strong></th></tr></thead><tbody><tr><td><strong>Storage</strong></td><td>Client-side</td><td>Server-side (Laravel sessions)</td></tr><tr><td><strong>Stateless</strong></td><td>Yes</td><td>No</td></tr><tr><td><strong>Scalability</strong></td><td>High</td><td>Medium</td></tr><tr><td><strong>Best For</strong></td><td>APIs, mobile apps</td><td>Traditional web apps</td></tr></tbody></table></figure>



<p>For APIs, token-based (Stateless) authentication is typically a better option.</p>



<p><strong>Tip:</strong>&nbsp;Always group your sensitive routes under appropriate middleware to prevent unauthorized access.</p>



<h2 class="wp-block-heading">Best Practices for API Security in Laravel</h2>



<p>Here are some rules you can follow for securing your Laravel APIs:</p>



<ul class="wp-block-list">
<li><strong>Always use HTTPS</strong>: Prevent token hijacking via man-in-the-middle attacks.</li>



<li><strong>Validate all inputs</strong>: Prevent SQL injection and other common attacks.</li>



<li><strong>Rate limit your APIs</strong>: Use Laravel’s throttle middleware to avoid abuse.</li>



<li><strong>Keep tokens short-lived</strong>: Use refresh tokens where necessary.</li>



<li><strong>Use strong hashing</strong>: Laravel uses Bcrypt/Argon2 for password hashing by default.</li>



<li><strong>Log suspicious activity</strong>: Keep an eye on repeated failed logins or abnormal access.</li>
</ul>



<h2 class="wp-block-heading">Common Mistakes and How to Avoid Them</h2>



<p>Let’s go over few frequent security slip-ups:</p>



<ul class="wp-block-list">
<li><strong>Storing Tokens in Local Storage:&nbsp;</strong>This can expose tokens to XSS attacks. Use&nbsp;<strong>HTTP-only cookies</strong>&nbsp;instead.</li>



<li><strong>Not Rotating or Expiring Tokens:&nbsp;</strong>Tokens should have&nbsp;<strong>limited lifespan</strong>. Consider implementing refresh tokens.</li>



<li><strong>Using Weak Secret Keys:&nbsp;</strong>Always generate strong, unique app keys using &#8220;php artisan key:generate&#8221;.</li>



<li><strong>Missing CORS Configuration:&nbsp;</strong>Unconfigured CORS can open your API to cross-origin attacks. Configure it wisely in cors.php.</li>
</ul>



<h2 class="wp-block-heading">Frequently Asked Questions (FAQs)</h2>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>1. What is the best authentication method for Laravel APIs?</strong></h3><div class="aioseo-faq-block-answer">
<p>It depends on your project’s needs. If you’re building a single-page application (SPA) or first-party frontend, Laravel Sanctum is best option due to its simplicity. For third-party integrations and complex OAuth2 flows, Laravel Passport is ideal. For stateless, token-based systems, JWT is solid choice.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>2. Can I use Sanctum and Passport together in the same Laravel app?</strong></h3><div class="aioseo-faq-block-answer">
<p>Technically yes, but it’s not recommended. Each is built for different use cases. For simple token-based authentication you can use Sanctum and for full OAuth2-based authentication you can use Passport. Mixing both can lead to complexity and conflicts.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>3. Is Sanctum secure enough for production APIs?</strong></h3><div class="aioseo-faq-block-answer">
<p>Absolutely. Laravel Sanctum is secure for production environments as long as you follow best practices like using HTTPS, setting proper CORS policies, and storing tokens safely (preferably in HTTP-only cookies).</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>4. What is the difference between Sanctum and Passport in Laravel?</strong></h3><div class="aioseo-faq-block-answer">
<p><strong>Sanctum</strong> is built for mobile apps and SPAs.<br><strong>Passport</strong> is a ised for third-party authentication and it is complete OAuth2 server.<br>Sanctum is a lightweight and easier to implement, while Passport provides more control and flexibility for advanced use cases.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question"><strong>5. How do I protect Laravel API routes from unauthorized access?</strong></h3><div class="aioseo-faq-block-answer">
<p>Use middleware like auth:sanctum or auth:api depending on your setup. You can also use Laravel’s policy and gate system to apply role-based access controls on specific endpoints.</p>
</div></div>



<div style="font-family: 'Public Sans', sans-serif; background: #fff; border: 1px solid #e1eaff; padding: 40px 35px; border-radius: 16px; margin: 60px 0; box-shadow: 0 10px 32px rgba(0, 0, 0, 0.05); max-width: 860px; margin-left: auto; margin-right: auto; text-align: center;">

  <h2 style="font-size: 28px; font-weight: 800; color: #0b2b53; margin-top: 0; margin-bottom: 16px;">
    Stop Wasting Time on Servers. Start Building Instead.
  </h2>

  <p style="font-size: 17px; color: #333; font-weight: 500; margin: 0 0 12px;">
    You didn’t start your project to babysit servers.
    Let <strong>ServerAvatar</strong> handle deployment, monitoring, and backups — so you can focus on growth.
  </p>

  <p style="font-size: 16px; color: #555; font-weight: 400; margin-bottom: 24px;">
    Deploy <strong>WordPress, Laravel, N8N</strong>, and more in minutes.
    No DevOps required. No command line. No stress.
  </p>

  <div style="display: inline-flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px;">
    <a href="https://app.serveravatar.com" target="_blank" rel="noopener"
       style="background: linear-gradient(135deg, #007bff, #0056d2); color: #fff; text-decoration: none; padding: 10px 20px; font-size: 14px; font-weight: 600; border-radius: 8px; text-align: center;">
      🚀 Try Free
    </a>
    <a href="https://serveravatar.com/" target="_blank" rel="noopener"
       style="background: #f5f9ff; color: #0056d2; text-decoration: none; padding: 10px 20px; font-size: 14px; font-weight: 600; border-radius: 8px; border: 1px solid #d2e3ff; text-align: center;">
      🌐 Learn More
    </a>
  </div>

  <p style="font-size: 14px; color: #888; margin-top: 10px;">
    Trusted by 10,000+ developers and growing.
  </p>
</div>



<p></p><p>The post <a href="https://serveravatar.com/laravel-api-authentication/">Laravel API Authentication: Secure Your Endpoints Effectively</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
