50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
use Illuminate\Support\Facades\Log;
|
|
class RestaurantJwtMiddleware
|
|
{
|
|
/**
|
|
* Created By: Sayli Raut
|
|
* Created at: 07 Feb 2024
|
|
* Use: To handle Restaurant login authentication middleware
|
|
*
|
|
* 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 the user is of restaurant type
|
|
if (!$user ||$user->principal_type_xid != 4) {
|
|
return response()->json(['status' => 'error', 'status_code' => 401, 'message' => 'Unauthorized access'], 401);
|
|
}
|
|
|
|
Session::flash('RestToken', $token);
|
|
} catch (JWTException $e) {
|
|
return response()->json(['status' => 'error', 'status_code' => 401, 'message' => 'Invalid token'], 401);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|