Login authentication from database

Ok, here is some of my code. Basically I created a login service to use in my registration controller. My login form used the email and password to log in and retrieve the user data, then passed it back from the php as JSON . This is just the way I did it and may not be the prefered way, but you should be able to put it together from this and the tutorials.

Some of the PHP… login.php

<?php

$postdata = file_get_contents("php://input");
$loginData = json_decode($postdata);
$email = $loginData->email;
$password = $loginData->password;

$userData = array('userID' => '',
				'firstName' => '',
				'lastName' => '',
				'phone' => '',
				'email' => '',
				'active' => '');	

mysql_connect("IP address of DB", "DB user name", "DB password") or die('{"userData":'.json_encode($userData).', "error": {"code": "003", "message": "Login error! Code: 003"}}'); // Connect to database server(localhost) with username and password.
mysql_select_db("table name") or die('{"userData":'.json_encode($userData).', "error": {"code": "004", "message": "Login error! Code: 004"}}'); 

if(!empty($email) && !empty($password)){
	
	//echo($email.'  '.$password);
	
	$email = mysql_escape_string($email);
	$password = mysql_escape_string(md5($password));
	 
	$results = mysql_query("SELECT id, fname, lname, phone, email, password, active FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die('{"error":"Login error! Code: 003"}'); 
	$match  = mysql_num_rows($results);
	
	$res = mysql_fetch_assoc($results);
	
	if($match > 0 ){		
		if ($res['active'] = 1) {
			// login success
			$userData['userID'] = $res['id'];
			$userData['firstName'] = $res['fname'];
			$userData['lastName'] = $res['lname'];
			$userData['phone'] = $res['phone'];
			$userData['email'] = $res['email'];
			$userData['active'] = $res['active'];
			echo ('{"userData":'.json_encode($userData).',"error":{"code":"000","message":"Logged in as '.$userData['firstName'].' '.$userData['lastName'].'."}}');
		} else {				
			echo('{"userData":'.json_encode($userData).', "error":{"code":"001","message":"Your account has not been ativated. Please verify your account by clicking on the link in the activation email sent when you registered.\r\n If you did not receive an email, click on the Resend Activation Email link and make sure the email is not being blocked by your spam filter."}}');
		}
	}else{
		// login failed
		echo ('{"userData":'.json_encode($userData).', "error": {"code": "002","message": "The email or password you entered is incorrect."}}');			
	}
} else {
	// something failed with submitting data, should never get here!
	echo('{"userData":'.json_encode($userData).', "error": {"code":"005", "message": "Login error! Code: 005"}}');
}
?>

Here’s my controller…

.controller('RegistrationCtrl', function ($scope, LoginService) {

    $scope.login = function (userLogin) {
        LoginService.loginUser(userLogin)
        .then(function (data) {
            //log in successfull
        }, function (data) {
            //log in failed
        });
    };

    $scope.register = function (reg) {
       LoginService.registerUser(reg)
        .then(function (resp) {
            // registration success
        }, function (resp) {
            //registration error
        });      
    };        
})

And heres my service…

.service('LoginService', function ($q, $http) {
    return {
        loginUser: function (loginData) {
            var deferred = $q.defer(),
                promise = deferred.promise;

            $http({
                url: 'http://someurl/login.php',
                method: "POST",
                data: loginData,
                headers: {'Content-Type': 'application/json'}
            })
                .then(function (response) {
                    if (response.data.error.code === "000") {
                        console.log("User login successful: " + JSON.stringify(response.data));
                        deferred.resolve(response.data);
                    } else {
                        console.log("User login failed: " + JSON.stringify(response.data.error));
                        deferred.reject(response.data);
                    }
                }, function (error) {
                    console.log("Server Error on login: " + JSON.stringify(error));
                    deferred.reject(error);
                });

            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return promise;
        },
        
        registerUser: function (regData) {
            var deferred = $q.defer(),
                promise = deferred.promise;
            
            $http({
                url: 'http://someurl/register.php',
                method: "POST",
                data: regData,
                headers: {'Content-Type': 'application/json'}
            }).then(function (response) {
                if (response.data.error.code === "000") {
                    console.log("User login successful: " + JSON.stringify(response.data));
                    deferred.resolve(response.data);
                } else {
                    console.log("User login failed: " + JSON.stringify(response.data.error));
                    deferred.reject(response.data);
                }
            }, function (error) {
                console.log("Server Error on login: " + JSON.stringify(error));
                deferred.reject(error);
            });
            
            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            };
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            };
            return promise;
        }
    };
});