Laravel 4.2 Login Auth via jQuery Ajax

My colleague design the login that pops up.

Here is my Javascript

// JavaScript Document Tabs 01
$(document).ready(function() {
	$('#formPopupLogin').submit(function(){
		 $.ajax({
	        type: "POST",
	        url: $(this).attr('action'),
	        dataType: "json",
	         data     : $(this).serializeArray(),
	         success: function(data) {
	            if (data.status) { 
// Success this should redirect or refresh the page. 
document.location.reload(true);
				} 
	            else {
				$('.errorWithLogin').html(data.errorMessage);
				$('.errorWithLogin').removeClass('hide');
	            }
	        },
	        error: function()
	        {
	            $('.errorWithLogin').html(server_error_message);
				$('.errorWithLogin').removeClass('hide');
	        }
	    });

		return false;
	});

});

and here is my controller function

public static function authLogin()
	{
		$input = Input::all();

		// Entry Record 
		$dataRecord = array('Username'=>$input['txtUsername'],
					'Password'=>$input['txtPassword']);

		// For my Future used
		$session_id  = Session::getId();

		// Validation
		$validator = Validator::make($dataRecord, User::$rulesLogin);

		// Intialize Arayy 
		$data = array();
		$data['status'] = 0; // Reload the page success 
		
		if ($validator->fails())
		{
			$htmlContent = "";
			$errors = $validator->errors()->getMessages();

			foreach ($dataRecord as $key => $value) {
				 $htmlContent .= "<p>".$errors[$key][0]."</p>"; /// I checked the result it is always zero // Then I assume that this display only zero // Lazy me. 
			}
			
			$data['errorMessage'] =  $htmlContent;
		}
		else 
		{	
			$rememberMe = false;	
			// Design doesn't capture remember me. 
			// So I remove it. 
			// if (isset($input['remember']))
			// {
			// 	$rememberMe = true;
			// }
			
			// validate the credentials 

			if (Auth::attempt(array('username' => $input['txtUsername'], 
									'password' => $input['txtPassword'], 
									'status'=>'1'), $rememberMe))
			{	
					
					if (Auth::check())
					{
						
					}

					$data['status'] = 1; // I assume that when status is success then I can reload the page / or do some redirection 
			}
			else 
			{	
					$data['errorMessage'] =  trans('messages.LOGIN_LOGIN_DOES_NOT_EXIST');
			}

		}

		response()->json($data);
	}

Leave a Reply

Your email address will not be published. Required fields are marked *