<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Md. Nazmul Hasan Anik on Medium]]></title>
        <description><![CDATA[Stories by Md. Nazmul Hasan Anik on Medium]]></description>
        <link>https://medium.com/@anikology?source=rss-a926fd6bfa6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GjEZrqbT3LlwqgcTrwh59w.jpeg</url>
            <title>Stories by Md. Nazmul Hasan Anik on Medium</title>
            <link>https://medium.com/@anikology?source=rss-a926fd6bfa6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:26:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anikology/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implement Authorization through Sanctum in Laravel 12]]></title>
            <link>https://medium.com/@anikology/implement-authorization-through-sanctum-in-laravel-12-94f12d306a51?source=rss-a926fd6bfa6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/94f12d306a51</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[laravel-12]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Md. Nazmul Hasan Anik]]></dc:creator>
            <pubDate>Sun, 13 Apr 2025 11:16:24 GMT</pubDate>
            <atom:updated>2025-04-13T11:16:24.538Z</atom:updated>
            <content:encoded><![CDATA[<p>Here’s a complete Token-Based Authentication setup using Laravel Sanctum in Laravel 12, covering:</p><p>✅ User Registration (Sign Up)<br>✅ User Login (Sign In)<br>✅ User Logout (Revoke Tokens)</p><p><strong>Install &amp; Setup Sanctum</strong></p><pre>composer require laravel/sanctum<br><br>php artisan vendor:publish --provider=&quot;Laravel\Sanctum\SanctumServiceProvider&quot;<br><br>php artisan migrate</pre><p><strong>Create API Routes (routes/api.php)</strong></p><pre>use App\Http\Controllers\AuthController;<br>use Illuminate\Support\Facades\Route;<br><br>Route::post(&#39;/register&#39;, [AuthController::class, &#39;register&#39;]);<br>Route::post(&#39;/login&#39;, [AuthController::class, &#39;login&#39;]);<br><br>// Protected Routes (Require Token)<br>Route::middleware(&#39;auth:sanctum&#39;)-&gt;group(function () {<br>    Route::post(&#39;/logout&#39;, [AuthController::class, &#39;logout&#39;]);<br> });</pre><p><strong>Create AuthController</strong></p><pre>php artisan make:controller AuthController</pre><blockquote><strong>AuthController (app/Http/Controllers/AuthController.php)</strong></blockquote><pre>&lt;?php<br>namespace App\Http\Controllers;<br><br>use App\Models\User;<br>use Illuminate\Http\Request;<br>use Illuminate\Support\Facades\Hash;<br>use Illuminate\Validation\ValidationException;<br><br>class AuthController extends Controller<br>{<br>    // Register<br>    public function register(Request $request)<br>    {<br>        $request-&gt;validate([<br>            &#39;name&#39; =&gt; &#39;required|string|max:255&#39;,<br>            &#39;email&#39; =&gt; &#39;required|string|email|unique:users&#39;,<br>            &#39;password&#39; =&gt; &#39;required|string|min:8|confirmed&#39;,<br>        ]);<br><br>        $user = User::create([<br>            &#39;name&#39; =&gt; $request-&gt;name,<br>            &#39;email&#39; =&gt; $request-&gt;email,<br>            &#39;password&#39; =&gt; Hash::make($request-&gt;password),<br>        ]);<br><br>        return response()-&gt;json([<br>            &#39;message&#39; =&gt; &#39;User registered successfully!&#39;,<br>            &#39;user&#39; =&gt; $user,<br>        ], 201);<br>    }<br><br>    // Login (Issue Token)<br>    public function login(Request $request)<br>    {<br>        $request-&gt;validate([<br>            &#39;email&#39; =&gt; &#39;required|email&#39;,<br>            &#39;password&#39; =&gt; &#39;required&#39;,<br>        ]);<br><br>        $user = User::where(&#39;email&#39;, $request-&gt;email)-&gt;first();<br><br>        if (!$user || !Hash::check($request-&gt;password, $user-&gt;password)) {<br>            throw ValidationException::withMessages([<br>                &#39;email&#39; =&gt; [&#39;The provided credentials are incorrect.&#39;],<br>            ]);<br>        }<br><br>        $token = $user-&gt;createToken(&#39;api-token&#39;)-&gt;plainTextToken;<br><br>        return response()-&gt;json([<br>            &#39;message&#39; =&gt; &#39;Login successful!&#39;,<br>            &#39;token&#39; =&gt; $token,<br>        ]);<br>    }<br><br>    // Logout (Revoke Token)<br>    public function logout(Request $request)<br>    {<br>        $request-&gt;user()-&gt;currentAccessToken()-&gt;delete();<br><br>        return response()-&gt;json([<br>            &#39;message&#39; =&gt; &#39;Logged out successfully!&#39;,<br>        ]);<br>    }<br>}</pre><h4><strong>Test API Endpoints</strong></h4><blockquote><strong>Register (POST /api/register) :</strong></blockquote><pre>{<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;email&quot;: &quot;john@example.com&quot;,<br>    &quot;password&quot;: &quot;password123&quot;,<br>    &quot;password_confirmation&quot;: &quot;password123&quot;<br>}</pre><blockquote><strong>Response</strong></blockquote><pre>{<br>    &quot;message&quot;: &quot;User registered successfully!&quot;,<br>    &quot;user&quot;: { &quot;id&quot;: 1, &quot;name&quot;: &quot;John Doe&quot;, &quot;email&quot;: &quot;john@example.com&quot; }<br>}</pre><blockquote><strong>Login (POST /api/login)</strong></blockquote><pre>{<br>    &quot;email&quot;: &quot;john@example.com&quot;,<br>    &quot;password&quot;: &quot;password123&quot;<br>}</pre><blockquote><strong>Response:</strong></blockquote><pre>{<br>    &quot;message&quot;: &quot;Login successful!&quot;,<br>    &quot;token&quot;: &quot;1|AbCdEfGhIjKlMnOpQrStUvWxYz&quot;<br>}</pre><p><strong>Logout (POST /api/logout)</strong></p><pre>At Headers in postman set:<br>Authorization: Bearer 1|AbCdEfGhIjKlMnOpQrStUvWxYz</pre><blockquote><strong>Response</strong></blockquote><pre>{ &quot;message&quot;: &quot;Logged out successfully!&quot; }<br></pre><p>Feel Free to ask if you have any quiries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94f12d306a51" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>