Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

csrf - Laravel 5.4 TokenMismatchException in VerifyCsrfToken.php line 68

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');
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...