在 VerifyCsrfToken.php 第 68 行中的 Laravel 5.4 TokenMismatchException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42687461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:31:04  来源:igfitidea点击:

Laravel 5.4 TokenMismatchException in VerifyCsrfToken.php line 68

laravelcsrflaravel-5.4

提问by ishan shah

When I login for the first time it works perfectly but when I log out from my app and try to re-login I get this error.

当我第一次登录时,它运行良好,但是当我从我的应用程序注销并尝试重新登录时,我收到此错误。

I've tried almost every available solutions but can't solve the issue. Any solution to fix this error?

我已经尝试了几乎所有可用的解决方案,但无法解决问题。任何解决此错误的解决方案?

This is how I perform login and logout(Please correct me if the code is wrong as I'm new in laravel).

这就是我执行登录和注销的方式(如果代码错误,请纠正我,因为我是 Laravel 新手)。

I've tried laravel-caffeine and {{ csrf_token() }}.

我试过 laravel-caffeine 和 {{ csrf_token() }}。

I think this is some session related issue.

我认为这是一些与会话相关的问题。

public function auth(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email|max:255',
        'password' => 'required|min:6',
    ]);

    $data = $request->only('email', 'password');

    if (\Auth::attempt($data)) {
        $email = $request->email;
         $users = DB::table('users')
            ->where('email', $email)
            ->select('users.*', 'user_name')
            ->get();

        Session::put('set', $users);

        if ($users[0]->is_admin == '1') {
            return redirect()->intended('adminDashboard');
        }else{
            return redirect()->intended('dashboard');
        }
    }else{
        return back()->withInput()->witherrors(['Email or password did not match!']);
    }
}

public function logout(){
    Session::flush();
    return view('login/login');
}

采纳答案by EddyTheDove

You error may be coming from your session manipulation. When you do

您的错误可能来自您的会话操作。当你做

Auth::attempt($data)

The user is already set in the session. You don't need to do your Session::put()thingy. To get the user, do

用户已在会话中设置。你不需要做你的事情Session::put()。要获取用户,请执行

$user = Auth::user();

To logout, you do

要注销,你做

Auth::logout(); //will clear the user from the session automatically

So to summarise, remove all the session manipulations you have in your code. Only play with Auth;

总而言之,删除代码中的所有会话操作。只玩Auth;

Session::flush(); //DELETE THIS LINE

Add Auth to the top of your controller with

将 Auth 添加到控制器的顶部

use Auth; //easier to work like that. 

回答by Sanji

Maybe the form has not declared the input tag of the token:

也许表单没有声明令牌的输入标签:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

回答by Emmanuel Iyen-Mediatrees

Go to config/session file and check if you have this line below:

转到配置/会话文件并检查下面是否有这一行:

'domain' => env('SESSION_DOMAIN', null)

If yes, then visit your .envfile and also set like this:

如果是,则访问您的.env文件并设置如下:

SESSION_DOMAIN=null

Then refresh, should be fine.

然后刷新,应该没问题。

回答by ettdro

Is your button to disconnect in a form? If so, you need to put this: {{ csrf_field }} in your form of type POST.

您的断开连接按钮是一种形式吗?如果是这样,你需要把这个:{{ csrf_field }} 放在你的 POST 形式中。