Files
cheerstothe_season_2.0/app/Http/Middleware/CustomerJwtMiddleware.php

53 lines
1.6 KiB
PHP
Raw Normal View History

2024-05-24 13:32:51 +05:30
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Facades\JWTAuth;
use Exception;
use Illuminate\Support\Facades\Session;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
2024-07-16 18:48:00 +05:30
use Illuminate\Support\Facades\Log;
2024-05-24 13:32:51 +05:30
class CustomerJwtMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// Check if the custom access-token header is present
if (!$request->hasHeader('access-token')) {
return response()->json(['status' => 'error', 'status_code' => 401, 'message' => 'Access token not provided'], 401);
}
// Retrieve the token from the custom access-token header
$token = $request->header('access-token');
try {
// Attempt to authenticate the user based on the token
$user = JWTAuth::setToken($token)->authenticate();
// Check if authentication was successful and user type is correct
2024-07-16 13:22:12 +00:00
2024-07-16 18:55:01 +05:30
if (!$user || $user->principal_type_xid != 3) {
2024-07-16 18:48:00 +05:30
return response()->json(['status' => 'error', 'status_code' => 401, 'message' => 'Unauthorized access'], 401);
}
2024-07-22 15:35:03 +05:30
2024-07-16 18:58:17 +05:30
2024-07-17 13:22:38 +05:30
2024-05-24 13:32:51 +05:30
Session::flash('vendorToken', $token);
} catch (JWTException $e) {
return response()->json(['status' => 'error', 'status_code' => 401, 'message' => 'Invalid token'], 401);
}
return $next($request);
}
}