Laravel timeout on authorization

Laravel 5.3 - My goal is to send the login form via ajax to the login controller (AuthenticatesUsers property) and get a response (json will be fine), so I can set a timeout before redirecting to the dashboard section (authenticated), I need this timeout for some interfaces. So can this be done? If it were possible, a hint would be enough. Thanks in advance.

Javascript example:

$("#login-form").submit(function (e) { var url = "/login"; // the script where you handle the form input. $.ajax({ type: "POST", cache : false, url: url, data: $("#login-form").serialize(), // serializes the form elements. success: function () { //Do the timeout part, then redirect topbar.addClass('success'); form.addClass('goAway'); article.addClass('active'); tries = 0; }, error: function () { location.reload(); input.addClass('disabled'); topbar.addClass('error'); } });}); 

The login form is sent via a message, and I would like to do the redirection myself, and not through the controller, and my main problem is that the csrf token in the javascript redirect has changed.

edit: Another thing I discovered includes a marker in ajax setup:

 $.ajaxSetup({ headers: {'X-CSRF-TOKEN': _token} }); 

and prevent the default action for the form:

 $("#login-form").submit(function (e) { e.preventDefault(); 

Here is my login form (all js and css are included in the parent view):

 @extends('layouts.app') @section('content') <form class="form form-horizontal" id="login-form" role="form" method="POST" action="{{ url('/login') }}"> {{ csrf_field() }} <div class="forceColor"></div> <div id="logosm_wrapper"> <img id="logosm_login" src="img/ipism_100x50.png" alt="logo" > </div> <div class="forceColor"></div> @if (count($errors)) <div class="topbar error"> @else <div class="topbar"> @endif <div class="spanColor"></div> <input id="email" type="email" class="input form-control" name="email" placeholder="E-mail" value="{{ old('email') }}"> </div> @if (count($errors)) <div class="topbar error"> @else <div class="topbar"> @endif <div class="spanColor"></div> <input id="password" type="password" class="input form-control" name="password" placeholder="Password"/> </div> <button class="submit" id="submit">Login</button> <!--input class="submit" id="submit" type="submit" value="Login"--> </form> @endsection 

This works as intended by Laravel Auth, my intention was to clear the login fields from authorization via JS, and then redirect the user to the control panel ...

+5
source share
1 answer

I suggest you add this to your ajax:

 $.ajax({ .... async : false, .... 
0
source

Source: https://habr.com/ru/post/1257718/


All Articles