doushi1510 2016-01-22 18:13
浏览 46
已采纳

Laravel 5.1身份验证 - 令牌不匹配

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:

<?php
    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();
        Auth::login($user);
        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.

  • 写回答

1条回答 默认 最新

  • dongtan6543 2016-01-22 18:42
    关注

    Try the following method, From Docs.

    <meta name="csrf-token" content="{{ csrf_token() }}" />
    
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?