{"id":450,"date":"2021-03-27T22:00:00","date_gmt":"2021-03-27T22:00:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=450"},"modified":"2023-10-18T11:00:44","modified_gmt":"2023-10-18T11:00:44","slug":"laravel-sanctum-an-in-depth-guide","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/laravel-sanctum-an-in-depth-guide\/","title":{"rendered":"Laravel Sanctum: An In-Depth Guide"},"content":{"rendered":"\n<p>Laravel Sanctum is a package provided by the Laravel framework for API authentication. It offers a straightforward way to implement API token-based authentication and secure your API routes in Laravel. In this guide, we&#8217;ll explore Laravel Sanctum in detail, covering its syntax, providing a demo example, and discussing its pros and cons.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<ol>\n<li><strong>Introduction to Laravel Sanctum<\/strong><\/li>\n\n\n\n<li><strong>Syntax and Basic Usage<\/strong><\/li>\n\n\n\n<li><strong>Demo: Implementing Laravel Sanctum<\/strong><\/li>\n\n\n\n<li><strong>Pros of Laravel Sanctum<\/strong><\/li>\n\n\n\n<li><strong>Cons of Laravel Sanctum<\/strong><\/li>\n\n\n\n<li><strong>Conclusion<\/strong><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction to Laravel Sanctum<\/h2>\n\n\n\n<p>Laravel Sanctum is an official Laravel package that simplifies API authentication by allowing you to issue API tokens for users or clients. It is particularly useful for securing API routes, providing stateless authentication, and handling Single Page Application (SPA) authentication scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Syntax and Basic Usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1. Installation<\/h3>\n\n\n\n<p>To use Laravel Sanctum, you need to install it via Composer. Run the following command:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"composer require laravel\/sanctum\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">composer <\/span><span style=\"color: #F97583\">require<\/span><span style=\"color: #B392F0\"> laravel<\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #B392F0\">sanctum<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>After installation, register the Sanctum service provider in your <code>config\/app.php<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"'providers' =&gt; [\n    \/\/ ...\n    Laravel\\Sanctum\\SanctumServiceProvider::class,\n],\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FFAB70\">&#39;providers&#39;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">=&gt;<\/span><span style=\"color: #B392F0\"> [<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #6B737C\">\/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #79B8FF\">Laravel<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Sanctum<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">SanctumServiceProvider<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #BBBBBB\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">]<\/span><span style=\"color: #BBBBBB\">,<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Next, publish Sanctum&#8217;s configuration and migration files:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"php artisan vendor:publish --tag=sanctum-config\nphp artisan vendor:publish --tag=sanctum-migrations\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php artisan vendor:publish <\/span><span style=\"color: #F97583\">--<\/span><span style=\"color: #B392F0\">tag<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\">sanctum<\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #B392F0\">config<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">php artisan vendor:publish <\/span><span style=\"color: #F97583\">--<\/span><span style=\"color: #B392F0\">tag<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\">sanctum<\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #B392F0\">migrations<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Finally, run the migrations to create the necessary database tables:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"php artisan migrate\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php artisan migrate<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2.2. Generating API Tokens<\/h3>\n\n\n\n<p>You can generate API tokens for users or clients. This is typically done in your authentication process. For instance, if you want to issue tokens to authenticated users upon login, you might do something like this in your controller:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use Laravel\\Sanctum\\PersonalAccessToken;\n\n$token = $user-&gt;createToken('token-name');\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Laravel<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Sanctum<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">PersonalAccessToken<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$token <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $user<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">createToken<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;token-name&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This generates a new token for the user with the given name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3. Protecting Routes<\/h3>\n\n\n\n<p>To protect your API routes using Sanctum, you can apply the <code>auth:sanctum<\/code> middleware. For example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.900001525878906px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Route::middleware('auth:sanctum')-&gt;get('\/api\/protected', function () {\n    \/\/ Your protected route logic here\n});\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">Route<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">middleware<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;auth:sanctum&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">get<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;\/api\/protected&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> () {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #6B737C\">\/\/ Your protected route logic here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This middleware ensures that only authenticated users with valid API tokens can access the route.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Demo: Implementing Laravel Sanctum<\/h2>\n\n\n\n<p>Let&#8217;s demonstrate how to implement Laravel Sanctum in a simple API. We&#8217;ll create a basic API with user registration, token issuance, and a protected route.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1. Set Up User Model and Migration<\/h3>\n\n\n\n<p>Create a <code>User<\/code> model with a migration:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:model User -m\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php artisan make:model User <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #B392F0\">m<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Define the user table structure in the migration file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2. Run Migrations<\/h3>\n\n\n\n<p>Run the migrations to create the users table:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"php artisan migrate\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><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 min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php artisan migrate<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3.3. Implement User Registration<\/h3>\n\n\n\n<p>Create an API route for user registration and a controller method to handle it. In the controller, create a new user and issue a token for them using Sanctum.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.4. Create a Protected Route<\/h3>\n\n\n\n<p>Define a protected route with the <code>auth:sanctum<\/code> middleware, which ensures that only authenticated users can access it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.5. Testing<\/h3>\n\n\n\n<p>Use tools like Postman or cURL to test the registration and protected route endpoints. Authenticate by sending the token as a header in the request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Pros of Laravel Sanctum<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1. Simplicity<\/h3>\n\n\n\n<p>Laravel Sanctum is easy to set up and use, making it accessible for developers of all experience levels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2. SPA Authentication<\/h3>\n\n\n\n<p>It&#8217;s designed for secure SPA authentication, providing stateful CSRF protection to safeguard against common web application attacks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3. Official Laravel Package<\/h3>\n\n\n\n<p>Being an official Laravel package means it&#8217;s well-maintained and follows Laravel&#8217;s coding standards.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4. Active Community<\/h3>\n\n\n\n<p>With a Laravel package, Sanctum benefits from a large, active community that contributes to its development and documentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Cons of Laravel Sanctum<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1. Limited to API Authentication<\/h3>\n\n\n\n<p>Sanctum is primarily designed for API authentication and may not cover all authentication scenarios, such as OAuth2 flows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2. Learning Curve<\/h3>\n\n\n\n<p>While Sanctum is relatively simple, there may still be a learning curve for those new to Laravel or API authentication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.3. Not Ideal for All Use Cases<\/h3>\n\n\n\n<p>For applications with complex authentication requirements, such as single sign-on (SSO), other packages like Laravel Passport may be more suitable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Conclusion<\/h2>\n\n\n\n<p>Laravel Sanctum is a valuable package for adding API token-based authentication to your Laravel applications, especially when building APIs and SPAs. It offers simplicity, security, and the advantage of being an official Laravel package. While it may not cover all authentication scenarios, it excels in the context of stateless API authentication and is well-suited for many projects. By following the installation and usage steps provided, you can quickly enhance the security of your Laravel API with Sanctum.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel Sanctum is a package provided by the Laravel framework for API authentication. It offers a straightforward way to implement API token-based authentication and secure your API routes in Laravel. In this guide, we&#8217;ll explore Laravel Sanctum in detail, covering its syntax, providing a demo example, and discussing its pros and cons. Table of Contents [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57,11],"tags":[44,3,4,58],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/450"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":2,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}