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')
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度