Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sajadsholi's avatar

how to handle RedirectIfAuthenticated middleware in api endpoints

how to handle the RedirectIfAuthenticated middleware in API requests

someone send a request to login endpoint with bearer token and after that user redirected to the dashboard

what is the appropriate response I should respond to?

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {

            if ($request->wantsJson()) {
                return response()->json(); // ?
            }


            return redirect('/');
        }

        return $next($request);
    }
0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

possibly return a 400 error ( bad request) or a redirection

1 like

Please or to participate in this conversation.