angular.module('app.controllers', [])
.controller('AuthLoginCtrl', ['$scope', 'restApi', '$state', 'auth',
function ($scope, restApi, $state, auth) {
$scope.ingresar = function () {
restApi.call({
method: 'post',
url: 'auth/autenticar',
dataType: 'json',
data: {
Correo: $scope.Correo,
Password: $scope.Password
},
response: function (r) {
if (r.response) {
auth.setToken(r.result);
$state.go('pedidos');
} else {
alert(r.message);
}
},
error: function (r) {
},
validationError: function (r) {
}
});
}
}])
<?php
use App\Lib\Auth,
App\Lib\Response,
App\Validation\EmpleadoValidation,
App\Middleware\AuthMiddleware;
$app->group('/auth/', function () {
$this->post('autenticar', function ($req, $res, $args) {
$parametros = $req->getParsedBody();
return $res->withHeader('Content-type', 'application/json')
->write(
json_encode($this->model->auth->autenticar($parametros['Correo'], $parametros['Password']))
);
});
});
The error is apparently caused by the label ion-content
of ionic
, when I remove the label it is solved but I have to use it anyway.
angular.module('your_app_name.services', [])
.factory('auth', ['$state', function ($state) {
var auth = {
setToken: function (token) {
localStorage[API.token_name] = token;
},
getToken: function () {
return localStorage[API.token_name];
},
getUserData: function () {
try{
var token = localStorage[API.token_name];
if (token === '') return;
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return window.atob(base64).data;
} catch(err) {
$state.go('auth.login');
}
},
logout: function () {
localStorage[API.token_name] = '';
$state.go('auth.login');
},
hasToken: function () {
return (localStorage[API.token_name] !== '');
},
redirectIfNotExists: function () {
if (!auth.hasToken()) {
$state.go('auth.login');
}
}
};
return auth;
}])
.service('loader', function () {
this.show = function (show) {
document.querySelector("#loader").style.display = show ? 'block' : 'none';
};
})
.service('restApi', ['$http', 'loader', 'auth', function ($http, loader, auth) {
this.call = function (config) {
var headers = {};
headers[API.token_name] = auth.getToken();
//loader.show(true);
var http_config = {
method: config.method,
url: API.base_url + config.url,
data: typeof (config.data) === 'undefined' ? null : config.data,
headers: headers
};
$http(http_config).then(function successCallback(response) {
//loader.show(false);
config.response(response.data);
}, function errorCallback(response) {
//loader.show(false);
switch (response.status) {
case 401: // No autorizado
auth.logout();
break;
case 422: // Validación
config.validationError(response.data);
break;
default:
config.error(response);
console.log(response.statusText);
break;
}
});
};
}])