When I log in using my Ajax login form, it shows an error instead of redirecting to the dashboard with:
window.location.href = "https://my.distincttrack.com/admin/";
If I refresh the page it will redirect me to the dashboard because of the auth guard in the laravel middleware. But the code I posted above is not at all redirecting me to that page when the response is a success.
I tried changing it to window.location but that did not help. I took out the double quotes and that did not work so I put them back. I tried turning off ajax and seeing what a normal form HTTP submit would respond and it echoed success as it should have.
My Jquery Ajax Code:
return {
init: function () {
l(), $("#m_login_signin_submit").click(function (e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var a = $(this), l = $(this).closest("form");
l.validate({
rules: {
email: {required: !0, email: !0},
password: {required: !0}
}
}), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0), l.ajaxSubmit({
url: "",
method: "post",
data: {
email:jQuery('#email').val(),
password:jQuery('#password').val()
},
success: function (response) {
if(response==="success") {
window.location.href="https://my.distincttrack.com/admin/";
} else {
setTimeout(function () {
a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1), i(l, "danger", "Incorrect username or password. Please try again.")
}, 2e3);
}
}
}))
}),
My controller where the user is logged and a response is made:
public function store(Request $request)
{
if (Auth::guard('system')->attempt(['email' => $request['email'], 'password' => $request['password'], 'status' => 1], $request['remember'])) {
return 'success';
}
return 'failed';
}
My HTML View in the blade:
<form class="m-login__form m-form" action="" method="post">
@csrf
<div class="form-group m-form__group">
<input class="form-control m-input" type="email" placeholder="Email" name="email" autocomplete="off" required>
</div>
<div class="form-group m-form__group">
<input class="form-control m-input m-login__form-input--last" type="password" placeholder="Password" name="password" required>
</div>
<div class="row m-login__form-sub">
<div class="col m--align-left m-login__form-left">
<label class="m-checkbox m-checkbox--light">
<input type="checkbox" name="remember"> Remember me
<span></span>
</label>
</div>
<div class="col m--align-right m-login__form-right">
<a href="javascript:;" id="m_login_forget_password" class="m-link">Forget Password ?</a>
</div>
</div>
<div class="m-login__form-action">
<button id="m_login_signin_submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn">Sign In</button>
</div>
</form>
And my meta tag with the CSRF token is correct.
When I log in and the ajax receives the response 'success' it's supposed to automatically redirect the user to the dashboard. When it fails it will flip the else statement and show an error ("Incorrect Credentials......") and so forth.
Temporarily you can see my issue until I cannot provide public access:
https://my.distincttrack.com/admin/login
email: jarrod@distincttrack.com pass: admin