Laravel 500 Internal Error (AJAX jQuery Login)

getting a internal server error (500) with my laravel and jQuery/AJAX login system. I've already made sure the csrf token is implemented and verified on the server. I'm using Laravel 4.2 with PHP 7.

HTML for the login form:

<div class="col-md-6">
      <h1>Login</h1>
      <div id="login-error" class="alert alert-danger"></div>
      {{ Form::open(array('action' => '[email protected]', 'id' => 'loginForm')) }}
            <div class="form-group">
            <label for="exampleInputEmail1">Username</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="">
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Password</label>
            <input type="password" class="form-control" id="password" name="password" placeholder="">
          </div>
        <div class="checkbox">
        <label>
          <input id="stay_signed" name="stay_signed" type="checkbox">Remember Me
        </label>
      </div>
          <button type="submit" class="btn btn-default">Submit</button>
      {{ Form::close() }}
  </div>

On this image you can see Laravel has added the input field for the token: console

User Model

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */

    public $timestamps = false;

    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');


}

jQuery/AJAX Code

$("#loginForm").submit(function (e) {
    e.preventDefault();
    var form = $(this);
    var errors;
    var username = $("#loginForm #username").val();
    var password = $("#loginForm #password").val();
    if(username == "") { $("#loginForm #username").css("border-color", "red").fadeTo('slow', 1); errors++; } else { $("#username").css("border-color", "#ccc").fadeTo('slow', 1); errors--; }
    if(password == "") { $("#loginForm #password").css("border-color", "red").fadeTo('slow', 1); errors++; } else { $("#password").css("border-color", "#ccc").fadeTo('slow', 1); errors--; }
    if(errors > 0) { 
        $("#login-error").text("These fields are required!");
        $("#login-error").fadeIn('slow');
        return false;
    }
    $.ajax({
        url:  $( form ).prop( 'action' ),
        type: 'POST',
        async: false,
        data: form.serialize(),

        success:(function(result) {
            console.log(result);
        }),
       error:function(result){
            console.log(result);
       }
    });

});    

Controller Code

 <?php

class UserController extends BaseController {

      public function loginUser()
    {
        if ( Session::token() !== Input::get( '_token' ) ) {
            return Response::json( array(
                'msg' => 'Unauthorized attempt to create setting'
            ) );
            return false;
        }

           $credentials = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );

              if(Auth::attempt($credentials, true)) {
                $response = array(
                    'success' => true,
                    'msg' => 'Login success - forever',
                );
                return Response::json( $response );

    }
    public function logoutUser()
    {
        Auth::logout();
        return Redirect::to('/');
    }

}

Route:

Route::post('user/login', array('before' => 'csrf', 'as'=>'loginuser', 'uses'=>'[email protected]'));

Answers 1

Related Articles