Various fixes
This commit is contained in:
65
app/partials/login/authentication.service.js
Normal file
65
app/partials/login/authentication.service.js
Normal file
@@ -0,0 +1,65 @@
|
||||
(function(){
|
||||
'use strict';
|
||||
|
||||
angular
|
||||
.module('app.login')
|
||||
.service('authentication', authentication);
|
||||
|
||||
authentication.$inject = ['$http', '$window'];
|
||||
function authentication ($http, $window) {
|
||||
|
||||
var saveToken = function (token) {
|
||||
$window.localStorage['mean-token'] = token;
|
||||
};
|
||||
|
||||
var getToken = function () {
|
||||
return $window.localStorage['mean-token'];
|
||||
};
|
||||
|
||||
var logout = function() {
|
||||
$window.localStorage.removeItem('mean-token');
|
||||
};
|
||||
|
||||
var isLoggedIn = function() {
|
||||
var token = getToken();
|
||||
var payload;
|
||||
|
||||
if(token){
|
||||
payload = token.split('.')[1];
|
||||
payload = $window.atob(payload);
|
||||
payload = JSON.parse(payload);
|
||||
|
||||
return payload.exp > Date.now() / 1000;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
var currentUser = function() {
|
||||
if(isLoggedIn()){
|
||||
var token = getToken();
|
||||
var payload = token.split('.')[1];
|
||||
payload = $window.atob(payload);
|
||||
payload = JSON.parse(payload);
|
||||
return {
|
||||
_id: payload._id,
|
||||
role: payload.role
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var login = function(user) {
|
||||
return $http.post('http://'+$rootScope.domainUrl+'/login', user).success(function(data) {
|
||||
saveToken(data.token);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
saveToken : saveToken,
|
||||
getToken : getToken,
|
||||
logout : logout,
|
||||
isLoggedIn : isLoggedIn,
|
||||
currentUser : currentUser
|
||||
};
|
||||
}
|
||||
})();
|
||||
@@ -5,69 +5,49 @@
|
||||
.module('app.login')
|
||||
.controller('loginCtrl', loginCtrl)
|
||||
|
||||
loginCtrl.$inject = ['$rootScope', '$scope', '$state'];
|
||||
function loginCtrl($rootScope, $scope, $state){
|
||||
loginCtrl.$inject = ['$scope', '$state', 'login', 'authentication', '$modal'];
|
||||
function loginCtrl($scope, $state, login, authentication, $modal){
|
||||
var vm = this;
|
||||
vm.user;
|
||||
vm.credentials = {};
|
||||
|
||||
vm.submit = function() {
|
||||
if (vm.data_input_form.$valid){
|
||||
vm.pkg = {
|
||||
"Nickname": vm.user.nickname,
|
||||
"Password": vm.user.password
|
||||
var professor = {
|
||||
"id": vm.user.nickname,
|
||||
"password": vm.user.password
|
||||
};
|
||||
|
||||
/*$rootScope.mensaje = "";
|
||||
$rootScope.bcancel = false;
|
||||
$scope.modalInstance = $modal.open({
|
||||
animation: $rootScope.animationsEnabled,
|
||||
templateUrl: 'LoginModal.html',
|
||||
scope: $scope,
|
||||
size: 'sm',
|
||||
resolve: {
|
||||
items: function () {
|
||||
return $rootScope.items;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Login.save(vm.pkg,
|
||||
function(data){
|
||||
if(data.Data._value != null){
|
||||
$rootScope.actOk = true;
|
||||
$rootScope.urlLo = 'listarProfesor';
|
||||
$rootScope.bcancel = false;
|
||||
|
||||
}else{
|
||||
$rootScope.bcancel = true;
|
||||
$rootScope.mensaje = data.Data._error;
|
||||
}
|
||||
},
|
||||
function(data){
|
||||
verificar = data.Data;
|
||||
if(verificar){
|
||||
alert("no existe el usuario");
|
||||
}
|
||||
})
|
||||
GetRol.get({id:vm.user.Nickname},
|
||||
login.save(professor,
|
||||
function(data){
|
||||
$rootScope.role = data.Data;
|
||||
},
|
||||
function(){
|
||||
authentication.saveToken(data.token);
|
||||
var permission = authentication.currentUser();
|
||||
if(permission.role=='admin'){
|
||||
$state.go('ProfessorList');
|
||||
};
|
||||
if(permission.role=='professor'){
|
||||
$state.go('CourseList');
|
||||
};
|
||||
|
||||
});*/
|
||||
$state.go('CourseList');
|
||||
},
|
||||
function(data){
|
||||
vm.message = 'Usuario/Clave incorrecto. Por favor intente de nuevo.'
|
||||
vm.botonOk = true;
|
||||
$scope.modalInstance = $modal.open({
|
||||
templateUrl:
|
||||
'/partials/login/modal/login_modal.html',
|
||||
scope: $scope,
|
||||
size: 'sm',
|
||||
resolve: {
|
||||
items: function () {
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/*$rootScope.open = function($event) {
|
||||
$event.preventDefault();
|
||||
$event.stopPropagation();
|
||||
$rootScope.opened = true;
|
||||
};*/
|
||||
|
||||
$scope.ok = function (urlLo) {
|
||||
$location.url(urlLo);
|
||||
$scope.ok = function () {
|
||||
$state.reload();
|
||||
$scope.modalInstance.dismiss('cancel');
|
||||
};
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
</div>
|
||||
<div class="col-md-4 column">
|
||||
<label for="password">Contraseña</label>
|
||||
<input type="password" class="form-control" id="password" name="password" ng-model="vm.user.password" maxlength="8" required/>
|
||||
<input type="password" class="form-control" id="password" name="password" ng-model="vm.user.password" required/>
|
||||
<div ng-show=false> {{ vm.password = vm.user.Password }}</div>
|
||||
<div class="error" ng-show="vm.submitted && vm.data_input_form.password.$invalid">
|
||||
<small class="error" ng-show="vm.data_input_form.password.$error.required">
|
||||
|
||||
@@ -3,47 +3,12 @@
|
||||
|
||||
angular
|
||||
.module('app.login')
|
||||
.factory('Login', Login)
|
||||
.factory('GetRol', GetRol)
|
||||
.factory('hash', hash)
|
||||
.value('algoritmo','SHA-1')
|
||||
.factory('login', login)
|
||||
.value('user',{})
|
||||
.value('id',{})
|
||||
|
||||
|
||||
Login.$inject = ['$resource','$rootScope'];
|
||||
function Login($resource, $rootScope){
|
||||
return $resource('http://'+$rootScope.domainUrl+'/api/VerifyUser');
|
||||
login.$inject = ['$resource','$rootScope'];
|
||||
function login($resource, $rootScope){
|
||||
return $resource('http://'+$rootScope.domainUrl+'/login');
|
||||
};
|
||||
|
||||
GetRol.$inject = ['$resource','$rootScope'];
|
||||
function GetRol($resource, $rootScope){
|
||||
return $resource('http://'+$rootScope.domainUrl+'/api/User/:id');
|
||||
};
|
||||
|
||||
hash.$inject = ['algoritmo'];
|
||||
function hash(algoritmo){
|
||||
|
||||
var hashFunction;
|
||||
|
||||
if (algoritmo==="MD5") {
|
||||
hashFunction=CryptoJS.MD5;
|
||||
} else if (algoritmo==="SHA-1") {
|
||||
hashFunction=CryptoJS.SHA1;
|
||||
} else if (algoritmo==="SHA-2-256") {
|
||||
hashFunction=CryptoJS.SHA256;
|
||||
} else if (algoritmo==="SHA-2-512") {
|
||||
hashFunction=CryptoJS.SHA512;
|
||||
} else {
|
||||
throw Error("El tipo de algoritmo no es válido:"+algoritmo);
|
||||
}
|
||||
|
||||
var hash=function(message) {
|
||||
var objHashResult=hashFunction(message);
|
||||
var strHashResult=objHashResult.toString(CryptoJS.enc.Base64);
|
||||
|
||||
return strHashResult;
|
||||
}
|
||||
return hash;
|
||||
};
|
||||
})();
|
||||
})();
|
||||
10
app/partials/login/modal/login_modal.html
Normal file
10
app/partials/login/modal/login_modal.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<div class="modal-header">
|
||||
<h3 class="modal-title">Error</h3>
|
||||
</div>
|
||||
<div style="text-align: center" class="modal-body">
|
||||
{{ vm.message }}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-primary" ng-show="vm.botonOk" type="button"
|
||||
ng-click="ok()">Aceptar</button>
|
||||
</div>
|
||||
Reference in New Issue
Block a user