-1

Thank you in advance

I have one middleware that is used to check the user is logged in or not. it is working absolutely fine for synchronous request,

For example, the user is logged out, after that they made one ajax request at that time middleware denied access but this is an AJAX request so no redirection will be made. I want to redirect for even AJAX and only wants redirections from middleware

My middleware looks like below

public function handle($request, Closure $next)
    {
        if (Helper::checkLogin() != 'success') {
            if ($request->ajax()) {
                // Here what i can do?
            }
            return redirect()->route('splash.screen');
        }
        return $next($request);
    }
Ronak Solanki
  • 341
  • 2
  • 5
  • 14

1 Answers1

0

Return response in json format and then redirect page from javascript

public function handle($request, Closure $next)
    {
        if (Helper::checkLogin() != 'success') {
            if ($request->ajax()) {
                return response()->json(["url"=>'/splash/screen', 'access'=>'deny']);
            }
            return redirect()->route('splash.screen');
        }
        return $next($request);
    }

Every Ajax Call you can check if access is equal to deny redirect you.

Shahrukh
  • 399
  • 2
  • 15
  • This will just return the response, I will have to manage this response from JS manually for all the AJAX request I made across the app – Ronak Solanki Sep 22 '20 at 08:02
  • What's the change, this will return only response, i want to handle redirection within middleware for AJAX request – Ronak Solanki Sep 22 '20 at 08:08
  • 1
    with Ajax request you can't redirect your page. in ajax success method you can check if request access is deny redirect it from javascript window.location.href – Shahrukh Sep 22 '20 at 08:16