<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;
use App\Exceptions\JWTException;

class VerifyJWTToken
{
    public function handle($request, Closure $next)
    {
        try {
            $user = Auth::guard('api')->user();

            if (!$user) {
                // throw new JWTException('Invalid or expired token');
                return response()->json([
                    'status' => 'error',
                    'message' => "Invalid or expired login token"
                ], 401);
            }

            return $next($request);
        } catch (JWTException $e) {
            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage() // Customize error message
            ], 401);
        }
    }
}
