Login with Ionic and PHP / Mysql API

1

I'm developing an application with Ionic v1 whose API is in PHP / Mysql and now need to implement a login system. I already have the platform running from the web, it's a social network.

As I'm a beginner in Ionic, I have "stitched" the money codes ... In this I've already got the app to load the login page and, if the name and password entered are correct, redirect to the "initial" and writes a token to the localStorage. The issue is that this "name and password" is defined in the code, need to be consulted in the API, among several users already registered ...

Among other things, I have the following:

Controller

.controller('LoginCtrl', function($scope, $http, LoginService, $ionicPopup, $state) {
$scope.data = {};

$scope.login = function() {
    LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) {
            var alertPopup = $ionicPopup.alert({
                title: 'Bem-vindo!'
            });

        $scope.gctoken = "id1";
        localStorage.setItem("gctoken", $scope.gctoken); // guarda o token

        $state.go('tabsController.inicial'); // redireciona
    }).error(function(data) {
        var alertPopup = $ionicPopup.alert({
            title: 'Erro!',
            template: 'Verifique os dados digitados!'
        });
    });
}
})

Service

.service('LoginService', function($q) {
return {
    loginUser: function(name, pw) {
        var deferred = $q.defer();
        var promise = deferred.promise;
        if (name == '1' && pw == '2') {
            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;
    }
}
});

Routes (one of your lines)

$urlRouterProvider.otherwise('/login');

I even thought about putting an if there ... something like (logic only):

if(status == 'logado'){
$urlRouterProvider.otherwise('/inicial');
}else{
$urlRouterProvider.otherwise('/login');
}

I need a north with codes:)

Thank you in advance.

    
asked by anonymous 17.01.2017 / 01:40

0 answers