简体   繁体   English

Laravel auth登录超时

[英]Laravel timeout on auth login

Laravel 5.3 - My goal is to send login form via ajax to login controller (AuthenticatesUsers trait), and to get a response (json would be ok), so i could set timeout before redirect to "dashboard" section (authenticated). Laravel 5.3-我的目标是通过ajax将登录表单发送到登录控制器(AuthenticatesUsers特质),并获得响应(json可以),因此我可以设置超时,然后重定向到“仪表板”部分(已认证)。 I need that timeout for some frontend stuff. 我需要一些前端的超时时间。 So could it be done? 这样可以吗? If it could, hint would suffice. 如果可以的话,提示就足够了。 Thanks in advance. 提前致谢。

Javascript example: JavaScript示例:

$("#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's 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');
    }

});});

Login form is sent via post, and i would like to do a redirect by myself, not via controller, and my main concern is the csrf token on javascript redirect would change. 登录表单是通过帖子发送的,我想由我自己而不是通过控制器进行重定向,而我主要关心的是javascript重定向上的csrf令牌会更改。

edit: Another thing that I discovered is including token inside ajax setup: 编辑:我发现的另一件事是ajax安装程序中包括令牌:

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

and prevent form default action: 并防止表单默认操作:

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

Here's my login form (all js and css is included in parent view): 这是我的登录表单(所有js和CSS都包含在父视图中):

@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 is working as intended by Laravel Auth, my intention was to green out input fields on authorisation via JS, and then redirect user to dashboard... 这按Laravel Auth的预期进行,我的意图是通过JS清除授权上的输入字段,然后将用户重定向到仪表板...

I suggest u add this in your ajax: 我建议你在你的ajax中添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM