I am working on project using Laravel, inertiajs/inertia-laravel and vuejs. After login when dashboard is shown and when try to logout the logout button is not working, to make it work i have to refresh every time then it work fine same goes with login after logout when i try to login again face the same issue. I have to refresh again to login.
in my console this error is shown
Wed Jul 13 11:17:42 2022] 127.0.0.1:52434 Closing
[Wed Jul 13 11:17:43 2022] 127.0.0.1:52437 Accepted
[Wed Jul 13 11:17:43 2022] 127.0.0.1:52437 [200]: GET /site.webmanifest
[Wed Jul 13 11:17:43 2022] 127.0.0.1:52437 Closing
[Wed Jul 13 11:17:43 2022] 127.0.0.1:52438 Accepted
[Wed Jul 13 11:17:46 2022] 127.0.0.1:52438 Closed without sending a request; it was probably just an unused speculative preconnection
these are my routes
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login')->middleware('guest');
Route::post('login', 'Auth\LoginController@store')->name('login.attempt')->middleware('guest');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
this is my controller code for login and logout
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Requests\Auth\LoginRequest;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
public $redirectTo = RouteServiceProvider::HOME;
/**
* Show login form
*
* @return \Inertia\Response
*/
public function showLoginForm()
{
// return Inertia::render('Auth/Login');
return Inertia::render('Auth/Login', [
// ALWAYS included on first visit
// OPTIONALLY included on partial reloads
// ALWAYS evaluated
'users' => User::get(),
// ALWAYS included on first visit
// OPTIONALLY included on partial reloads
// ONLY evaluated when needed
'users' => fn () => User::get(),
// NEVER included on first visit
// OPTIONALLY included on partial reloads
// ONLY evaluated when needed
'users' => Inertia::lazy(fn () => User::get()),
]);
}
/**
* Handle an incoming authentication request.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect('/');
}
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function logout(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
return $this->loggedOut($request) ?: redirect('/');
// $request->session()->invalidate();
// $request->session()->regenerateToken();
// return redirect('/');
}
}