Laravel 5.1 Authentication - Token Mistmatch

- 1 answer

I have a fresh install of Laravel 5.1 but am getting a token mismatch error when I try to login a user. I'm not using a form, rather I am using an ajax call that logs in the user after google verification has been satisfied.

My error is: TokenMismatchException in VerifyCsrfToken.php line 53

My controller that is getting hit with the ajax call:

    namespace App\Http\Controllers;
    use App\Email;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Google_Client; 
    use Auth;
    use App\User;
    class verify extends Controller

    public function verifyIdToken(Request $request)
    $user = User::where('name', 'Molly')->first();
        if (Auth::check($user))
    return response()->json(['Logged In' => "Yes!"]);

I don't get a response, just a 500 internal server error with the above error. I do see a laravel session cookie being returned however, so I am very confused as to what is going on. What is this token and why is it throwing an error?

For more information, my routes.php file is:

    if (Auth::guest()) {
    Route::get('/', function () {
        return view('welcome');
} else {
    Route::get('/', ['middleware' => 'auth', function () {
        return view('mainview');

Edit: Basically I want the controller hit in the ajax call to verify the user was logged in, without any internal server error so that on a refresh, they are rerouted to the mainview not the welcome page.



Try the following method, From Docs.

<meta name="csrf-token" content="{{ csrf_token() }}" />

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')