1

I am trying to make login for users, but when I try to login I have error status code 419. CSRF token mismatch.

I am using php artisan serve for running localhost on http://localhost.

I suppose that the problem is that I am using http instead of https, but I am not sure( as I know php artisan serve works only on http)

api.js

Route::group(['middleware' => ['web']], function () {
Route::post('register','Auth\RegisterController@register');  
Route::post('login','Auth\LoginController@login');  

});

LoginController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
use Illuminate\Http\Request;
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 = RouteServiceProvider::HOME;
    protected $redirectTo =  '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->middleware('guest')->except('logout');
    }
}

Login.js I call axios like this

onSubmit(e){
        e.preventDefault();
        const {email , password} = this.state ;
        axios.post('api/login', {
            email, 
            password
          })
          .then(response=> {
            this.setState({err: false,loggedIn:true,data:response.data.credentials.email},() => {
                localStorage.setItem('allProjects', this.state.data), localStorage.setItem('loginVar', JSON.stringify(true))});
             hashHistory.push("/") ; 

          })
          .catch(error=> {
            this.refs.email.value="";
            this.refs.password.value="";
            this.setState({err: true,loggedIn:false, errors: error.response.data,data:''});
            console.log(error.response.data);
            localStorage.setItem('allProjects', 'guest');
            localStorage.setItem('loginVar', false);


         });
     }

Does anyone have idea how to correct this issue?

Thank you

HardRock
  • 809
  • 2
  • 12
  • 37
  • 1
    [this](https://laracasts.com/discuss/channels/laravel/how-can-i-use-csrf-token-with-axios-post-method) can help you – Binar Web Jun 11 '20 at 10:37

0 Answers0