Ionic user authentication using PHP, MySQL (XAMPP)

2019-09-04 12:01发布

问题:

I'm new to Cordova and ionic. For the moment I need to authenticate a login using MySQL (XAMPP server).

This is my login.html

   <ion-view view-title="Login" name="login-view">
     <ion-content class="padding">
        <div class="app-icon"></div>      
        <div class="list">
            <label class="item item-input item-floating-label">
                 <span class="input-label">Username</span>
                 <input type="text" placeholder="Username" ng-model="data.username">
            </label>
            <label class="item item-input item-floating-label">
                 <span class="input-label">Password</span>
                 <input type="password" placeholder="Password" ng-model="data.password">
            </label>
        </div>
        <div class="list">
            <ion-md-input placeholder="Username" highlight-color="balanced" type="text"></ion-md-input>
            <ion-md-input placeholder="Password" highlight-color="energized" type="password"></ion-md-input>
        </div>
        <div class="padding">
            <button class="button button-block button-calm" ng-click="login()">Login</button>
        </div>

      </ion-content>
   </ion-view>

This is my Services.js

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

            if (name == 'user' && pw == 'secret') {
                deferred.resolve('Welcome ' + name + '!');
            } else {
                deferred.reject('Wrong credentials.');
            }
            promise.success = function(fn) {
                promise.then(fn);
                return promise;
            }
            promise.error = function(fn) {
                promise.then(null, fn);
                return promise;
            }
            return promise;
         }
     }
 })

This is my php file.

  <?php
  include_once './database_conn.php';
    // array for JSON response
  $response = array();
// check for required fields

  if(isset($_POST['username']) && isset($_POST['password'])){

    $username = $_POST['username'];
    $pass = $_POST['password'];


    // connecting to db
    $db = new DbConnection();
    // mysql checking the login
    $result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '$username' AND tu_pass = '$pass'");



    // check if row inserted or not
    if (mysql_num_rows($result) > 0) {
        // successfully retrieve data into database
        $row = mysql_fetch_assoc($result);
        $response["success"] = 1;
        $response["message"] = mysql_error();

        echo json_encode($response);
    } else {
        // failed to retrieve row
        $response["success"] = 0;
        $response["message"] = "Login failed";
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    // echoing JSON response
    echo json_encode($response);

}   
   ?>

My Question is how am I suppose to authenticate my user instead of using

    if (name == 'user' && pw == 'secret') {

in Services.js

回答1:

loginUser: function(name, pw) {
            var deferred = $q.defer();
            var promise = deferred.promise;

            var link = 'http://localhost/xampp/ioninc-php-login/login.php';

            $http.post(link, { username: name, password: pw }).then(function (res) {

               // alert("hi ",res.data);
                if (res.data == "Suuccessful") {
                    deferred.resolve('Welcome ' + name + '!');
                } else {
                    deferred.reject('Wrong credentials.');
                }
            });