2022年6月24日星期五

[Laravel] return json response after login/logout

 find all in files

protected function sendLoginResponse(Request $request)


you can see

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 204)
                    : redirect()->intended($this->redirectPath());
    }


change to it

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        return $request->wantsJson()
                    //? new JsonResponse([], 204)
                    ? response()->json(['logged_in'=>true])
                    : redirect()->intended($this->redirectPath());
    }





logout response
public function logout(Request $request)
{
$this->guard()->logout();

$request->session()->invalidate();

$request->session()->regenerateToken();

if ($response = $this->loggedOut($request)) {
return $response;
}

return $request->wantsJson()
//? new JsonResponse([], 204)
? response()->json( \Auth::user() )
: redirect('/');
}

沒有留言:

發佈留言

[Fixed] some windows chrome, scrolling has glitch position not accurate

   [Fixed] some windows chrome, scrolling has glitch position not accurate Smooth Scrolling Flag 📜 Chrome has an experimental "smooth ...