Skip to content

Commit dd3f2ee

Browse files
author
Alessandro Mancini
committed
fix
1 parent 294b9d9 commit dd3f2ee

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

models/user.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var mongoose = require('mongoose');
22
var moment = require('moment');
3+
var expired_time = 60;
34

45
module.exports = function(connection) {
56

@@ -17,9 +18,7 @@ module.exports = function(connection) {
1718
});
1819

1920
userSchema.methods.hasExpired = function() {
20-
console.log("Sono dentro hasExpired");
21-
console.log(moment().diff(this.token.createDate, 'minutes'));
22-
return (moment().diff(this.token.createDate, 'minutes')) > 3;
21+
return (moment().diff(this.token.createDate, 'minutes')) > expired_time;
2322

2423
};
2524

public/js/controllers.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
var mainAppControllers = angular.module('mainAppControllers', []);
55

6-
mainAppControllers.controller('NavCtrl', ['$scope', '$http','$window','$location','localStorageService','AuthenticationService',
7-
function ($scope, $http,$window,$location,localStorageService,AuthenticationService) {
6+
mainAppControllers.controller('NavCtrl', ['$scope', '$http','$location','localStorageService','AuthenticationService',
7+
function ($scope, $http,$location,localStorageService,AuthenticationService) {
88

99

1010
$scope.isAuthenticated = AuthenticationService.isLogged()
@@ -17,16 +17,17 @@ mainAppControllers.controller('NavCtrl', ['$scope', '$http','$window','$location
1717
}
1818
]);
1919

20-
mainAppControllers.controller('LoginCtrl', ['$scope', '$http','$window','$location', "cryptoJSService",'localStorageService',
21-
function ($scope, $http,$window,$location,cryptoJSService,localStorageService) {
22-
23-
console.log(cryptoJSService.cryptoJS);
20+
mainAppControllers.controller('LoginCtrl', ['$scope', '$http','$location', "cryptoJSService",'localStorageService',
21+
function ($scope, $http,$location,CryptoJS,localStorageService) {
2422

2523
$scope.failed_login = "";
2624

2725
$scope.submit = function()
2826
{
29-
var user = {"username": $scope.username, "password": $scope.password};
27+
var salt = $scope.username;
28+
var enc_password = CryptoJS.PBKDF2($scope.password, salt, { keySize: 256/32 });
29+
30+
var user = {"username": $scope.username, "password": enc_password.toString()};
3031

3132
if($scope.username!==undefined || $scope.password !==undefined){
3233
$http({method: 'POST', url: '/api/login', data:user}).
@@ -37,8 +38,11 @@ mainAppControllers.controller('LoginCtrl', ['$scope', '$http','$window','$locati
3738

3839
}).
3940
error(function(data, status, headers, config) {
40-
console.log(data);
41-
noty({text: data, timeout: 2000, type: 'error'});
41+
if(status===401){
42+
noty({text: 'Wrong username and/or password!', timeout: 2000, type: 'error'});
43+
}else{
44+
noty({text: data, timeout: 2000, type: 'error'});
45+
}
4246
});
4347
}else{
4448
noty({text: 'Username and password are mandatory!', timeout: 2000, type: 'error'});
@@ -50,26 +54,32 @@ mainAppControllers.controller('LoginCtrl', ['$scope', '$http','$window','$locati
5054
]);
5155

5256

53-
mainAppControllers.controller('RegistrationCtrl', ['$scope', '$http','$window','$location',
54-
function ($scope, $http) {
57+
mainAppControllers.controller('RegistrationCtrl', ['$scope', '$http','cryptoJSService',
58+
function ($scope, $http, CryptoJS) {
5559

5660
$scope.signup = function()
5761
{
58-
var user = {"username": $scope.username, "password": $scope.password, "check_password" : $scope.check_password};
62+
var salt = $scope.username;
63+
64+
var enc_password = CryptoJS.PBKDF2($scope.password, salt, { keySize: 256/32 });
65+
var enc_check_password = CryptoJS.PBKDF2($scope.check_password, salt, { keySize: 256/32 });
66+
67+
var user = {"username": $scope.username, "password": enc_password.toString(), "check_password" : enc_check_password.toString() };
5968

6069
if($scope.username!==undefined || $scope.password !==undefined || $scope.check_password !==undefined){
6170

6271
if($scope.password !== $scope.check_password){
6372
noty({text: 'password and check_password must be the same!', timeout: 2000, type: 'warning'});
6473
}else{
65-
$http({method: 'POST', url: '/signup', data:user}).
74+
$http({method: 'POST', url: '/api/signup', data:user}).
6675
success(function(data, status, headers, config) {
67-
console.log(data);
6876
noty({text: "Username is registered correctly!", timeout: 2000, type: 'success'});
77+
$scope.username = null;
78+
$scope.password = null;
79+
$scope.check_password = null;
6980
}).
7081
error(function(data, status, headers, config) {
71-
console.log(data);
72-
noty({text: data, timeout: 2000, type: 'error'});
82+
noty({text: data.message, timeout: 2000, type: 'error'});
7383
});
7484
}
7585

@@ -84,8 +94,8 @@ mainAppControllers.controller('RegistrationCtrl', ['$scope', '$http','$window','
8494

8595

8696

87-
mainAppControllers.controller('HomeCtrl', ['$scope', '$http','$window','$location','localStorageService','AuthenticationService',
88-
function ($scope, $http,$window,$location,localStorageService,AuthenticationService) {
97+
mainAppControllers.controller('HomeCtrl', ['$scope', '$http',
98+
function ($scope, $http) {
8999

90100
$http({method: 'GET', url: '/api/things'}).
91101
success(function(data, status, headers, config) {
@@ -191,9 +201,8 @@ mainAppControllers.controller('HomeCtrl', ['$scope', '$http','$window','$locatio
191201
]);
192202

193203

194-
mainAppControllers.controller('PersonCtrl', ['$scope', '$http','$window','$location','localStorageService','AuthenticationService',
195-
function ($scope, $http,$window,$location,localStorageService,AuthenticationService) {
196-
204+
mainAppControllers.controller('PersonCtrl', ['$scope', '$http',
205+
function ($scope, $http) {
197206

198207
$scope.person = null;
199208

@@ -218,9 +227,8 @@ mainAppControllers.controller('PersonCtrl', ['$scope', '$http','$window','$locat
218227

219228

220229

221-
mainAppControllers.controller('ThingCtrl', ['$scope', '$http','$window','$location','localStorageService','AuthenticationService',
222-
function ($scope, $http,$window,$location,localStorageService,AuthenticationService) {
223-
230+
mainAppControllers.controller('ThingCtrl', ['$scope', '$http',
231+
function ($scope, $http) {
224232

225233
$scope.thing = null;
226234

public/js/services.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ myAppServices.service('TokenInterceptor',
2222
},
2323
responseError : function (response) {
2424

25-
if(response.status===401){
25+
console.log(response);
26+
27+
if(response.config.url!=="/api/login" && response.status===401){
2628
localStorageService.clearAll();
2729
$location.path("/login");
2830
noty({text: "You have to perform signin to earned access to privileged resources!", timeout: 2000, type: 'error'});
@@ -36,7 +38,7 @@ myAppServices.service('TokenInterceptor',
3638

3739
myAppServices.service('cryptoJSService',function(){
3840
console.log(CryptoJS)
39-
this.cryptoJS = CryptoJS;
41+
return CryptoJS;
4042
})
4143

4244
myAppServices.service('AuthenticationService',function(localStorageService){

0 commit comments

Comments
 (0)