我是Laravel的新手,我創建了一個登錄表單,它將通過Ajax進行驗證。現在的問題是,當我使用Auth :: login($ user)它似乎沒有登錄該用戶的電子郵件和密碼驗證後。在下面檢查我的代碼。Laravel Auth ::登錄不能在AJAX請求上工作
JS
$('#loginform').on('submit', function(event){
var token = $('#loginform input[name="_token"]').val();
var email = $.trim($('#loginform #email').val());
var password = $.trim($('#loginform #password').val());
$.post("/checklogin",
{
_token: token,
email: email,
password: password,
},
function(data){
if(data == "success"){
window.location.reload();
} else {
$('#loginform .error').text(data);
}
});
event.preventDefault();
});
CheckLoginController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User as User;
use Hash;
class CheckLoginController extends Controller
{
public function check(Request $request)
{
if($request->ajax()) :
$user = User::where('email', '=', $_POST['email'])->first();
if($user) :
//check password
if(Hash::check($_POST['password'], $user->password)) :
Auth::login($user);
echo "success";
else :
echo "Invalid username/password";
endif;
endif;
endif;
}
}
任何人,請幫助。
的輸出是什麼'的console.log(數據)'在你的Ajax代碼'功能(數據)' – xmhafiz
@ h44f33z控制檯輸出「成功」,但仍是用戶沒有登錄in。 –
也許可以嘗試使用'location.href'而不是'window.location.reload();' – xmhafiz