After I log out and want to log back in I get the following error:
message: "CSRF token mismatch.", exception: "Symfony\Component\HttpKernel\Exception\HttpException"
I customized some parts of the LoginController
so it might have something to do with that. I tried searching in the unmodified LoginController
but I can't find anything. My login function from my vuex
store:
login({ commit }, user) {
return new Promise((resolve, reject) => {
axios({ url: '/login', data: user, method: 'POST' })
.then(response => {
const user = response.data.user
commit('auth_success', user)
resolve(response)
})
.catch(error => {
commit('auth_error')
reject(error)
})
})
},
My logout function, also from my vuex
store:
logout({ commit }) {
return new Promise((resolve, reject) => {
axios({ url: '/logout', method: 'POST' })
.then(() => {
commit('logout')
localStorage.removeItem('user');
resolve()
})
.catch(error => {
reject(error)
})
})
},
And this is my LoginController:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
if ($this->attemptLogin($request)) {
$request->session()->regenerate();
return $this->authenticated($request, $this->guard()->user());
}
}
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(['email' => $request->json('email'), 'password' => $request->json('password')]);
}
protected function authenticated(Request $request, $user)
{
if ($user) {
return response()->json([
'user' => $user
], 200);
} else {
return response('', 400);
}
}
public function logout(Request $request)
{
$this->guard()->logout();
if ($request->session()->invalidate()) {
return response('', 200);
}
}
}
Am I missing something?
Update:
If I do a page refresh, I don't get the error anymore. This has probably something to do with that I have a Single Page Application using vue-router
. So I guess I need a page refresh to avoid the 419 error. But this is not an ideal situation. Does someone has experience with this?