2017-03-09 164 views
2

當我第一次登錄時,它完美地工作,但是當我從我的應用程序註銷並嘗試重新登錄時出現此錯誤。Laravel 5.4 TokenMismatchException在VerifyCsrfToken.php行68

我已經嘗試了幾乎所有可用的解決方案,但無法解決問題。任何解決方案來解決這個錯誤?

這就是我如何執行登錄和註銷(如果代碼錯誤,請更正我,因爲我是laravel中的新成員)。

我試過laravel-caffeine和{{csrf_token()}}。

我認爲這是一些會話相關的問題。

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'); 
} 
+0

和你怎麼註銷和登錄? – EddyTheDove

+0

列出您嘗試過的解決方案和結果,將有助於任何人回答不建議採取相同的行動。 – canadiancreed

+0

嘗試(您的項目基礎路徑)/ public/logout然後(基本路徑)/ public。它應該工作 – Nevermore

回答

2

您的錯誤可能來自您的會話操作。當你做

Auth::attempt($data) 

用戶已經設置在會話中。你不需要做你的事情。爲了獲得用戶,請

$user = Auth::user(); 

要註銷,你做

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

所以總結一下,去掉你在你的代碼有所有的會話操作。只能玩Auth;

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

添加驗證到控制器的頂部

use Auth; //easier to work like that. 
0

是您的按鈕在窗體中斷開?如果是這樣,您需要在您的POST類型表單中輸入{{csrf_field}}。

+0

已經添加了該字段。 –

1

也許形式沒有宣佈令牌的輸入標籤:

<input type="hidden" name="_token" value="{{ csrf_token() }}"> 
相關問題