Skip to content

Instantly share code, notes, and snippets.

@catrielmuller
Created November 13, 2015 23:59
Show Gist options
  • Save catrielmuller/db6973270c93e30efbb9 to your computer and use it in GitHub Desktop.
Save catrielmuller/db6973270c93e30efbb9 to your computer and use it in GitHub Desktop.
AngularJs Token Auth Demo - Metro 2015 Prog4
.controller('EntrarCtrl', function($rootScope, $scope, $stateParams, $http, $ionicPopup, $location ) {
$rootScope.userToken = '';
$scope.user={};
$scope.user.email='';
$scope.user.password ='';
$scope.doLogin = function() {
$http.post('http://api-prog4.herokuapp.com/login',$scope.user).then(function(resp) {
console.log(resp.data);
$rootScope.userToken = resp.data.token;
var alertPopup = $ionicPopup.alert({
title: 'Logeado con exito',
template: 'Ingresa ahora'
});
alertPopup.then(function(res) {
$location.path('/app/usuarios');
});
}, function(err) {
console.error('ERR', err);
var alertPopup = $ionicPopup.alert({
title: 'Error en el ingreso',
template: 'Email o contraseña invalido'
});
alertPopup.then(function(res) {
$location.path('/app/entrar');
});
// err.status will contain the status code
});
};
})
.controller('UsuarioslistsCtrl', function($rootScope, $scope, $http, $location) {
console.log($rootScope.userToken);
$scope.user = [];
$http.get('http://api-prog4.herokuapp.com/me', {headers: {'auth-token': $rootScope.userToken}}).then(function(resp) {
$scope.user = resp.data.data;
console.log('Succes', resp.data.data);
$location.path('/app/usuarios');
}, function(err) {
console.error('ERR', err);
$location.path('/app/entrar');
// err.status will contain the status code
}); /*var alertPopup = $ionicPopup.alert({
title: 'Tienes que logearte',
template: 'Se nuestro amigo'
});
alertPopup.then(function(res) {
$location.path('/app/entrar');
});*/
$scope.usuarios = [];
$scope.$on('$ionicView.beforeEnter', function() {
$http.get('http://api-geoalquiler.herokuapp.com/index.php/usuarios').then(function(resp) {
$scope.usuarios = resp.data.data;
console.log('Succes', resp.data.data);
}, function(err) {
console.error('ERR', err);
// err.status will contain the status code
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment