在 VerifyCsrfToken.php 第 68 行中的 Laravel 5.4 TokenMismatchException [英] 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?
这就是我执行登录和注销的方式(如果代码错误,请纠正我,因为我是 Laravel 新手).
This is how I perform login and logout(Please correct me if the code is wrong as I'm new in laravel).
我试过 laravel-caffeine 和 {{ csrf_token() }}.
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');
}
推荐答案
您的错误可能来自会话操作.当你这样做
You error may be coming from your session manipulation. When you do
Auth::attempt($data)
用户已在会话中设置.你不需要做你的 Session::put()
事情.要获取用户,请执行
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();
要退出,你做
Auth::logout(); //will clear the user from the session automatically
总而言之,删除代码中的所有会话操作.只玩Auth;
So to summarise, remove all the session manipulations you have in your code. Only play with Auth;
Session::flush(); //DELETE THIS LINE
使用
use Auth; //easier to work like that.
这篇关于在 VerifyCsrfToken.php 第 68 行中的 Laravel 5.4 TokenMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!