Ionic Login and PHP/Mysql API

Asked

Viewed 663 times

1

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

Since I’m a beginner in Ionic, I have "sewn" mining codes... In this I already got the app to initially load the "login" page and, if the name and password are correct, redirect to the "start" page and write a token in 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 their lines)

$urlRouterProvider.otherwise('/login');

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

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

I need a north with codes :)

Thanks in advance.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.