Skip to content

Commit f7c3136

Browse files
committed
Login básico y autenticación con token jwt
1 parent 6cda228 commit f7c3136

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+203
-19827
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
},
2424
"homepage": "https://github.com/gdgcde/bootcamp01#readme",
2525
"dependencies": {
26+
"bcrypt": "^1.0.3",
2627
"body-parser": "^1.18.2",
2728
"bootstrap-social": "^5.1.1",
2829
"compression": "^1.7.2",
2930
"cors": "^2.8.4",
3031
"express": "^4.16.3",
32+
"jwt-simple": "^0.5.1",
3133
"method-override": "^2.3.10",
34+
"moment": "^2.22.0",
3235
"morgan": "^1.9.0",
3336
"nodemon": "^1.17.2",
37+
"passport": "^0.4.0",
38+
"passport-jwt": "^4.0.0",
3439
"pg": "^7.4.1",
3540
"pg-hstore": "^2.3.2",
3641
"sequelize": "^4.37.3",

public/app/dataService.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
this.updateEntity = updateEntity
1515
this.deleteEntity = deleteEntity
1616

17-
function getEntityAll(table) {
17+
function getEntityAll(table, token) {
1818
var endpoint = '';
1919
switch(table) {
2020
case 'eventos':
@@ -30,7 +30,11 @@
3030
default: ''
3131
}
3232

33-
return $http.get(endpoint)
33+
return $http.get(endpoint, {
34+
headers: {
35+
"Authorization": token
36+
}
37+
})
3438
.then(function(result) {
3539
return result.data
3640
})
@@ -39,7 +43,7 @@
3943
});
4044
}
4145

42-
function getEntityById(table, id) {
46+
function getEntityById(table, id, token) {
4347
var endpoint = '';
4448
switch(table) {
4549
case 'eventos':
@@ -55,7 +59,11 @@
5559
default: ''
5660
}
5761

58-
return $http.get(endpoint + '/' + id)
62+
return $http.get(endpoint + '/' + id, {
63+
headers: {
64+
"Authorization": token
65+
}
66+
})
5967
.then(function(result) {
6068
return result.data
6169
})
@@ -64,17 +72,29 @@
6472
});
6573
}
6674

67-
function saveEntity(table, data) {
68-
return $http.post(config.serviceUrl + table, data)
75+
function saveEntity(table, data, token) {
76+
return $http.post(config.serviceUrl + table, data, {
77+
headers: {
78+
"Authorization": token
79+
}
80+
})
6981
}
7082

71-
function updateEntity(table, data) {
72-
return $http.put(config.serviceUrl + table + '/' + data.id, data)
83+
function updateEntity(table, data, token) {
84+
return $http.put(config.serviceUrl + table + '/' + data.id, data, {
85+
headers: {
86+
"Authorization": token
87+
}
88+
})
7389

7490
}
7591

76-
function deleteEntity(table, id) {
77-
return $http.delete(config.serviceUrl + table + '/' + id)
92+
function deleteEntity(table, id, token) {
93+
return $http.delete(config.serviceUrl + table + '/' + id, {
94+
headers: {
95+
"Authorization": token
96+
}
97+
})
7898
}
7999
}
80100
})()

public/app/eventos/eventoController.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,38 @@
55
.module('gdgAdmin')
66
.controller('eventoController', eventoController);
77

8-
eventoController.$inject = ['$scope', '$state', '$stateParams', 'dataService'];
8+
eventoController.$inject = ['$scope', '$rootScope', '$state', '$stateParams', 'dataService'];
99

10-
function eventoController($scope, $state, $stateParams, dataService) {
10+
function eventoController($scope, $rootScope, $state, $stateParams, dataService) {
1111
var vm = this;
1212
vm.evento = {};
1313
vm.table_name = 'eventos';
14+
var token = $rootScope.token;
1415
activate();
1516

1617
vm.submitEvento = function () {
1718
if ($state.current.name == 'form-evento-create') {
18-
return dataService.saveEntity(vm.table_name, vm.evento)
19+
return dataService.saveEntity(vm.table_name, vm.evento, token)
1920
.then(function (result) {
2021
$state.go('eventos');
2122
});
2223
} else {
23-
return dataService.updateEntity(vm.table_name, vm.evento)
24+
return dataService.updateEntity(vm.table_name, vm.evento, token)
2425
.then(function (result) {
2526
$state.go('eventos');
2627
});
2728
}
2829
}
2930

3031
function getEventoById () {
31-
return dataService.getEntityById(vm.table_name, $stateParams.id)
32+
return dataService.getEntityById(vm.table_name, $stateParams.id, token)
3233
.then(function (evento) {
3334
return evento;
3435
});
3536
}
3637

3738
vm.deleteEvento = function(id) {
38-
return dataService.deleteEntity(vm.table_name, id)
39+
return dataService.deleteEntity(vm.table_name, id, token)
3940
.then(function (result) {
4041
$state.go('eventos');
4142
});

public/app/eventos/eventosController.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55
.module('gdgAdmin')
66
.controller('eventosController', eventosController);
77

8-
eventosController.$inject = ['$scope', 'dataService'];
8+
eventosController.$inject = ['$scope', '$rootScope', 'dataService'];
99

10-
function eventosController($scope, dataService) {
10+
function eventosController($scope, $rootScope, dataService) {
1111
var vm = this;
1212
vm.eventos = [];
1313
vm.table_name = 'eventos';
14+
var token = $rootScope.token;
15+
1416
activate();
1517

1618
function getEventos() {
17-
return dataService.getEntityAll(vm.table_name)
19+
return dataService.getEntityAll(vm.table_name, token)
1820
.then(function (result) {
1921
return result;
2022
});
2123
}
2224

2325
vm.deleteEvento = function (id) {
24-
return dataService.deleteEntity(vm.table_name, id)
26+
return dataService.deleteEntity(vm.table_name, id, token)
2527
.then(function (result) {
2628
activate();
2729
});

public/app/home/home.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>Dashboard</h1>
1+
<h1>Dashboard {{identity.nombre}}</h1>

public/app/home/homeController.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
activate();
1414

15-
function activate() { }
15+
function activate() {
16+
17+
}
1618
}
1719
})();

public/app/login/login.html

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<form id="form-login" role="form">
2-
<div class="col-md-4 col-sm-4">
2+
<div>
3+
<div class="row">
4+
<div class="col-md-6">
5+
<h2>Login de Usuarios</h2>
6+
</div>
7+
</div>
8+
<br/>
9+
<div class="col-md-4 col-sm-4">
310
<div class="form-group">
411
<label for="email" class="">Email</label>
512
<input ng-model="usuario.email" type="text" class="form-control" id=email placeholder="Introduzca el email" required>
@@ -11,19 +18,6 @@
1118
<div class="form-group">
1219
<button type="submit" class="btn btn-lg btn-primary btn-block btn-signin" ng-click="loginUsuario(usuario)">Acceder</button>
1320
</div>
14-
</div>
15-
<div class="col-md-4 col-sm-4">
16-
<div class="form-group">
17-
<a class="btn btn-block btn-social btn-facebook" >
18-
<span class="fa fa-facebook"></span> Entrar con Facebook
19-
</a>
20-
<a class="btn btn-block btn-social btn-google">
21-
<span class="fa fa-google"></span> Entrar con Google
22-
</a>
23-
<a class="btn btn-block btn-social btn-github" ng-click="loginConGitHub(usuario)">
24-
<span class="fa fa-github"></span> Entrar con GitHub
25-
</a>
2621
</div>
2722
</div>
28-
</div>
2923
</form>

public/app/login/loginController.js

+16-49
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99

1010
function loginController($scope, $rootScope, $state, loginService) {
1111

12-
$rootScope.titulo1 = 'Login';
13-
$rootScope.titulo2 = 'Por favor acceda al sistema mediante una de las opciones';
14-
15-
$scope.loginUsuario = function(usuario) {
12+
$scope.loginUsuario = function(usuario_login) {
13+
var usuario = {
14+
email: usuario_login.email,
15+
clave: usuario_login.clave,
16+
gethash: true
17+
};
1618
loginService.loginUsuario(usuario)
17-
.then(function(usuario) {
18-
19-
if (!usuario) {
20-
alert("No coinciden los datos con ningún usuario registrado");
19+
.then(function(res) {
20+
var identity = res.usuario;
21+
var token = res.token;
22+
if (token.length <= 0) {
23+
alert("El token no se ha generado correctamente!");
2124
} else {
22-
$rootScope.usuario = usuario;
23-
$rootScope.titulo1 = null;
24-
$rootScope.titulo2 = null;
25+
// Crear elementos en el localStorage
26+
localStorage.setItem('identity', JSON.stringify(identity));
27+
localStorage.setItem('token', token);
28+
$rootScope.identity = identity;
29+
$rootScope.token = token;
2530
$state.go('home');
2631
}
2732
})
@@ -31,43 +36,5 @@
3136

3237
// $scope.usuario = null;
3338
};
34-
$scope.loginConGitHub = function(usuario) {
35-
loginService.loginConGitHub(usuario)
36-
.then(function(usuario) {
37-
$rootScope.usuario = usuario;
38-
$rootScope.titulo1 = null;
39-
$rootScope.titulo2 = null;
40-
//$state.go('home');
41-
})
42-
.catch(function(err) {
43-
console.log(err);
44-
});
45-
46-
// $scope.usuario = null;
47-
};
48-
/*$scope.uploadFiles = function(file, errFiles) {
49-
console.log(file);
50-
51-
$scope.f = file;
52-
$scope.errFile = errFiles && errFiles[0];
53-
if (file) {
54-
file.upload = Upload.upload({
55-
url: '/',
56-
data: {file: file}
57-
});
58-
59-
file.upload.then(function (response) {
60-
$timeout(function () {
61-
file.result = response.data;
62-
});
63-
}, function (response) {
64-
if (response.status > 0)
65-
$scope.errorMsg = response.status + ': ' + response.data;
66-
}, function (evt) {
67-
file.progress = Math.min(100, parseInt(100.0 *
68-
evt.loaded / evt.total));
69-
});
70-
}
71-
};*/
7239
}
7340
})();

public/app/login/loginService.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,12 @@
99

1010
function loginService($http, config) {
1111
this.loginUsuario = loginUsuario;
12-
// this.logoutUsuario = logoutUsuario;
13-
this.loginConGitHub = loginConGitHub;
1412

1513
function loginUsuario(usuario) {
14+
1615
return $http.post(config.serviceUrl + 'login', usuario)
1716
.then(function(result) {
18-
return result.data;
19-
})
20-
.catch(function(err) {
21-
console.log(err);
22-
});
23-
}
24-
25-
function loginConGitHub() {
26-
return $http.get(config.serviceUrl + 'usuario/github/callback')
27-
.then(function(result) {
28-
console.log("res: "+result.user);
29-
17+
//console.log(result);
3018
return result.data;
3119
})
3220
.catch(function(err) {

public/app/logout/logout.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Sesión cerrada</h1>

public/app/logout/logoutController.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function() {
2+
'use strict';
3+
4+
angular
5+
.module('gdgAdmin')
6+
.controller('logoutController', logoutController)
7+
8+
logoutController.$inject = ['$scope', '$rootScope', '$state'];
9+
10+
function logoutController($scope, $rootScope, $state) {
11+
activate();
12+
13+
function activate() {
14+
localStorage.removeItem('identity');
15+
localStorage.removeItem('token');
16+
localStorage.clear();
17+
$rootScope.identity = null;
18+
$rootScope.token = null;
19+
$state.go('home');
20+
}
21+
}
22+
})();

public/app/route.js

+10
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,15 @@
7777
label: 'Home'
7878
}
7979
})
80+
// Logout state
81+
.state('logout', {
82+
url: '/logout',
83+
templateUrl: 'app/logout/logout.html',
84+
controller: 'logoutController',
85+
controllerAs: 'vm',
86+
ncyBreadcrumb: {
87+
label: 'Home'
88+
}
89+
})
8090
}
8191
}())

public/css/bootstrap-social/.editorconfig

-11
This file was deleted.

public/css/bootstrap-social/.npmignore

-3
This file was deleted.

0 commit comments

Comments
 (0)