Working with JWTs in Laravel (Without the Magic) —…
    Neura MarketNeura Market/Midjourney
    ChatGPTChatGPTClaudeClaudeGeminiGeminiCursorCursorGrokGrokPerplexityPerplexityMidjourneyMidjourney
    DeepSeekDeepSeekCoPilotCoPilotStable DiffusionStable Diffusion
    View All Directories
    OverviewPromptsBlogVideosGuidesCoursesCommunityStylesTrending
    MidjourneyBlogWorking with JWTs in Laravel (Without the Magic)
    Back to Blog
    Working with JWTs in Laravel (Without the Magic)
    laravel

    Working with JWTs in Laravel (Without the Magic)

    Mark Townsend March 30, 2026
    0 views

    If you've worked with APIs, authentication, or third-party integrations, you've almost certainly run...

    If you've worked with APIs, authentication, or third-party integrations, you've almost certainly run into JWTs (JSON Web Tokens).

    JWT (pronounced: JOT) is a compact string used to pass data between systems. It has three base64url-encoded parts separated by dots: header, payload, and signature. The header defines metadata like the algorithm, the payload contains claims such as user id and expiration, and the signature ensures integrity. The header and payload can be decoded into JSON for quick inspection.

    Let's create a simple trait you can use on any class in your Laravel application to read the contents of a JWT:

    <?php
    
    namespace Quartzy\Illuminate\Support\Traits;
    
    use Illuminate\Support\Str;
    
    trait InspectsJwt
    {
        public function inspectJwt(string $jwt): array
        {
            if (!str_contains($jwt, '.')) {
                return [];
            }
    
            return collect(explode('.', $jwt))
                ->map(function (string $segment) {
                    $decoded = base64_decode(
                        Str::of($segment)
                            ->replace('_', '/')
                            ->replace('-', '+')
                            ->__toString()
                    );
    
                    return json_decode($decoded, true) ?? [];
                })
                ->filter() // removes empty payload elements from the array
                ->reduce(fn (array $carry, array $item) => array_merge($carry, $item), []);
        }
    }
    

    Now you can inspect the contents of JWT with ease! Let's look at an example where a user is making a request to our app and their JWT token is passed as a header in the request. This would be our example JWT:

    (You can copy the following JWT and examine it on JWT.io eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2Nzg5MCIsInV1aWQiOiI1NTBlODQwMC1lMjliLTQxZDQtYTcxNi00NDY2NTU0NDAwMDAiLCJuYW1lIjoiTWFyayBUb3duc2VuZCIsImVtYWlsIjoibWFyay50b3duc2VuZEBleGFtcGxlLmNvbSIsInJvbGUiOiJkZXZlbG9wZXIiLCJpYXQiOjE3MTAwMDAwMDAsImV4cCI6MTcxMDAwMzYwMH0.signatureplaceholder

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Http\Request;
    use Quartzy\Illuminate\Support\Traits\InspectsJwt;
    use App\Models\User;
    use Illuminate\Support\Facades\Log;
    
    class AuthenticateWithJwtUuid
    {
        use InspectsJwt;
    
        public function handle(Request $request, Closure $next)
        {
            $authorization = $request->header('Authorization', '');
    
            if (str_starts_with($authorization, 'Bearer ')) {
                $jwt = substr($authorization, 7);
                $payload = $this->inspectJwt($jwt);
    
                if (!empty($payload['uuid'])) {
                    // Pull user by UUID instead of numeric ID
                    $user = User::where('uuid', $payload['uuid'])->first();
    
                    if ($user) {
                        $request->attributes->set('user', $user);
                        Log::info("JWT authenticated user {$user->name} with role {$payload['role']}");
                    }
                }
            }
    
            return $next($request);
        }
    }
    

    That's all there is to it! Hopefully this helps demystify JWTs and simplifies working with them in your application!

    Tags

    laravelphpsecuritytutorial

    Comments

    More Blog

    View all
    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught megemma

    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught me

    I ported the whole Gemma-4 family — E2B, E4B, 12B, 31B, and the 26B-A4B MoE — to run on...

    X
    xbill
    Hey DEV, I'm Tobore. Let's actually connect.community

    Hey DEV, I'm Tobore. Let's actually connect.

    Hey DEV, I'm Tobore. Let's actually connect. I've been on here for a while now, mostly writing and...

    L
    Laurina Ayarah
    I burned through thousands of AI tokens. Then a friend did it for freeai

    I burned through thousands of AI tokens. Then a friend did it for free

    (yep, kinda clickbait, just for the funsies 😊) At the beginning of the year, I relaunched my...

    P
    Paulo Henrique
    Claude might be saturating your machineai

    Claude might be saturating your machine

    My laptop was sitting idle with the fan at full tilt. Nothing was running that I knew of. The culprit...

    S
    Sidhant Panda
    Automated GitHub Code Reviews Using Google Geminigithubactions

    Automated GitHub Code Reviews Using Google Gemini

    I Built a Thing! TL;DR — Google Gemini-based Pull Request reviews and Issue Triaging for...

    D
    Darren "Dazbo" Lester
    What is an "agentic harness," actually?ai

    What is an "agentic harness," actually?

    I've been hearing the word "harness" thrown around a lot lately. I assumed it just meant "the IDE" or...

    T
    Tilde A. Thurium

    Stay up to date

    Get the latest Midjourney prompts, rules, and resources delivered to your inbox weekly.

    Neura Market LogoNeura Market

    Discover the best AI prompts, plugins, and resources for Midjourney and more.

    Content Types

    • Rules
    • Prompts
    • MCPs
    • Agents
    • Guides

    Platforms

    • ChatGPT Directory
    • Claude Directory
    • Gemini Directory
    • Cursor Directory
    • Grok Directory
    • Perplexity Directory
    • DeepSeek Directory
    • CoPilot Directory
    • Stable Diffusion Directory
    • Midjourney Directory
    • All Directories

    Resources

    • Blog
    • Documentation
    • Help Center
    • Marketplace

    Legal

    • Privacy Policy
    • Terms of Service

    © 2026 Neura Market. All rights reserved.

    |

    Not affiliated with any AI platform vendors.

    Neura Market

    Custom AI Systems & Services

    Our team of experienced AI builders will help build custom AI systems, workflows, and solutions for your business.

    Request custom work

    Ready-made automations for this

    Workflows from the Neura Market marketplace related to this Midjourney resource

    • Securely Access Google Cloud Run APIs with JWT Authentication in n8nn8n · $9.99 · Related topic
    • Automate Graphic Wallpaper Creation with Midjourney and Canvas APIsn8n · $9.99 · Related topic
    • Customer Authentication for Chat Support with OpenAI and Redis Session Managementn8n · $14.99 · Related topic
    • Secure API Endpoint with Bearer Token Authentication and Field Validationn8n · $14.99 · Related topic
    Browse all workflows