0

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

  • I would suggest that you put an `error` function in there as well, and see what it comes up with. Turning off ajax and seeing that everything works without it doesn't tell you what's wrong when you're using ajax, after all. For some ideas on how to put together an error function, [this thread](https://stackoverflow.com/questions/3642348/jquery-ajax-error-callback) looks helpful. – BobRodes Dec 26 '18 at 18:56
  • No errors! If you would try the link you can see that no error will persist just the javascript isn't redirecting – Jarrod Estepp Dec 26 '18 at 19:04
  • I just removed all the success code and put only the redirect in there and it literally did nothing. the error message still pops up when im not even inserting it anymore – Jarrod Estepp Dec 26 '18 at 19:04
  • It redirects (or at lesat for me), there is a fatal error at `https://my.distincttrack.com/admin/`. Check your error logs. – user3783243 Dec 26 '18 at 19:04
  • I know there is no view or controller or whatever. Im focused on getting my computer to redirect – Jarrod Estepp Dec 26 '18 at 19:08
  • i feel like it has something to do with cache – Jarrod Estepp Dec 26 '18 at 19:08
  • Did you check your error logs? – BobRodes Dec 26 '18 at 19:11
  • 1
    Can you try to add return false after the window.location.href or try to use document.location.href = 'Your url',true; also try to change to: window.location.replace(url) – Sgedda Dec 26 '18 at 19:13
  • what sgedda said-use `window.location.replace` – mjw Dec 26 '18 at 19:46
  • What do you get if you `console.log(response, response==="success");` before the if statement in your success method? – Rwd Dec 26 '18 at 20:13
  • I think the redirection is working well. Could you check the Admin Dashboard route? Maybe you can dump somthing in the index controller. – Manuel Eduardo Romero Dec 26 '18 at 21:04

0 Answers0