<?php

// app/Http/Middleware/OptionalSanctum.php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\PersonalAccessToken;
use Symfony\Component\HttpFoundation\Response;

class OptionalSanctum
{
    public function handle(Request $request, Closure $next): Response
    {
        // dd(Auth::id());
        $token = $request->bearerToken();
        // dd($token);
        if ($token) {
            $accessToken = PersonalAccessToken::findToken($token);
            if ($accessToken) {
                auth()->setUser($accessToken->tokenable);
            }
        }

        return $next($request);
    }
}

