Laravel 5.4 TokenMismatchException in VerifyCsrfToken.php line 68

青春壹個敷衍的年華 提交于 2019-11-28 01:52:00

问题


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).

I've tried laravel-caffeine and {{ 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');
}

回答1:


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

$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;

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

Add Auth to the top of your controller with

use Auth; //easier to work like that. 



回答2:


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

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



回答3:


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




回答4:


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

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

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

SESSION_DOMAIN=null

Then refresh, should be fine.



来源:https://stackoverflow.com/questions/42687461/laravel-5-4-tokenmismatchexception-in-verifycsrftoken-php-line-68

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!