lundi 5 décembre 2016

Laravel AuthController not working when ajax request pass

I am adapting the out-of-the-box AuthController in Laravel 5.2 to suit my needs. I want to implement login form using AJAX. For that I write the code. But when I click on login button then show 302 error and redirect. I don't know what is my mistake.

My JS

 function do_login()
{
    frm_name = 'userlogin';
    username = $('#userlogin input[id=email]').val();
    password = $('#userlogin input[id=password]').val();
    _token = $('#userlogin input[id=_token]').val();

    if(username == '' || password == '') {
        $('#flashMessage').attr('class','alert alert-danger');
        $('#flashMessage').html('Please specify Username and Password');
    } else {
        var param = 'username='+ username+ '&password='+ password +'&_token='+ _token;
        $.ajax({
            type : "POST",
            datatype : "json",
            url: "auth/login",
            data : param,
            success : function(data) {
                data = JSON.parse(data);
                if (data.status == 0) {
                    $('#myModallogin').modal('hide');
                    window.location.href = data.redirect_url;
                }
                if (data.status == 1) {
                    $('#flashMessage').attr('class','alert alert-danger');
                    $('#flashMessage').html(data.message);
                } else {
                    onError(data.Error,'#'+ frm_name);
                }
            }
        });
    }
}

$(document).ready(function(){
    $('#member_login').click(function() {
        do_login();
    });
});

My route

Route::auth();
Route::post('auth/login', 'Auth\AuthController@userLogin');

My AuthController

 class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    //protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    public function userLogin() {
        $post_data = Request::all();
        pr($post_data);exit;
    }
}

My route is working properly because when I debug the routes for example Route::post('auth/login', function(){ echo 'aaaaaaaaaa'; }); so it display the aaaaaa. But when I called the function then show 302 error and redirect the page. I don't know what is my mistake. Please suggest me.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2h6f1NF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire