I added X-CSRF-TOKEN in the headers and _token as data just to be sure it would work. But I still get
GET http://localhost/App/ 401 (Unauthorized)
I am copying what is going on when a post is made in the original Blade template when you 'logout'.
Although it does log out the user it just throws this error in the console Uncaught (in promise) Error: Request failed with status code 401 Why is this happening?
Strange. It is a post request. I was looking at an older version of laravel and just did a artisan route:list where only Auth::routes() was defined. Edit: nevermind, this was a Laravel Spark project which alters things a bit.
@tykus
The routes are from Auth::routes(); and the controller:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
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
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct() {
$this->middleware('guest')->except('logout');
}
}
@tykus what really is strange is that the user IS logged out after I refresh the page. But of course I get this 401 before I refresh. So it's working but at the same time I'm getting this 401.
CSRF is not for authentication; it is to mitigate the threat of cross-site request forgery.
I know. I'm just duplicating the Form in the Blade template. I'm going to try one more thing. You mentioned that you don't see _token in the URI. I'll try that first and see what happens.
I think Axios takes data and parses that to JSON and that is part of the body of the request and not the URI.
The token I mentioned earlier would be a JWT or similar... nothing to do with CSRF.; you seem to be confusing the too different types of token from my responses.
I mean I could probably just hack this and force a refresh on the browser after the request is sent. Like I mentioned it logs the user out I just get a 401 error
So the logout action is actually working? I didn't understand that from earlier! What is the response of the action; the typical redirect to /? Check your browser's devtools.
You will need to handle the redirection differently (or not at all) depending on your app's needs because the 3xx response code will not be understood by your javascript. If you need to, you can redirect in javascript using:
window.location = '/'
But why not simply use the standard request/response cycle in that case?
So the logout action is actually working? I didn't understand that from earlier! What is the response of the action; the typical redirect to /? Check your browser's devtools.
Yeah... that was in my original post. It's working but instead of just a nice redirect I get that 401. And yes this the root /is Auth protected. Normally Laravel will boot you to /login if you try to access this.. which is does. But after logging out it just sends a 401 from a GET. So there is no response except {message:"Unauthenticated."}
In 5.5, you can override the logout method in the LoginController to set a different redirection after successfully logging out.
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/'); // redirect somewhere else that a guest can visit
}