Ionic Framework Login Authentication from database

ionic


Angularjs + Cordova = Ionic previousle ionic framework previously discussed. Ionic Framework Basically first Login authentication from database. I choose front end ionic framework and backend core php  simple Login authentication access database.

Ionic Framework

Controller.js

.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
        });      
    };        
})


Services

Ionic Framework is work only Web services.


.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;
        }
    };
});


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"}}');
}
?>



Disclaimer: Not all these snippet are written by me. Some of them are collected from other sources on Internet.
Ionic Framework Login Authentication from database Dev2Tricks 5 of 5
Angularjs + Cordova = Ionic previousle ionic framework previously discussed. Ionic Framework Basically first Login authentication from ...

Share this

Related Posts

Previous
Next Post »