Skip to content

Commit

Permalink
add user register and check version update
Browse files Browse the repository at this point in the history
  • Loading branch information
52u committed Mar 2, 2015
1 parent 946c5c5 commit 012c353
Show file tree
Hide file tree
Showing 20 changed files with 600 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

.idea
# IoTgo configuration
config.js
6 changes: 6 additions & 0 deletions config.js.sample
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ module.exports = {
url: 'https://www.google.com/recaptcha/api/siteverify'
},
pendingRequestTimeout: 3000
mailgun: {
api_key:'', //Mailgun API Key
domain:'', //Mailgun Domain Name
from:'' //Mailgun Default SMTP Login Email
},
upgradeUrl:"http://v.itead.cc/api/upgrade"
};
97 changes: 86 additions & 11 deletions db/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,106 @@ var hash = function (value) {
};
var transform = function (doc, ret) {
delete ret.password;
delete ret.token;
delete ret.__v;
return ret;
};
var now = function () {
return new Date();
};

var ACTIVE_TIME = 24 * 60 * 60 * 1000;
/**
* Exports
*/
var schema = new Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true, set: hash },
apikey: { type: String, unique: true, default: uuid.v4 },
createdAt: { type: Date, index: true, default: now }
email: {type: String, required: true, unique: true},
password: {type: String, required: true, set: hash},
apikey: {type: String, unique: true, default: uuid.v4},
createdAt: {type: Date, index: true, default: now},
isActivated: {type: Boolean, default: false},
token: {type: String},
validExpire: {type: Date}
});

schema.static('register', function (email, password, callback) {
this.create({ email: email, password: password }, function (err, user) {
this.create({email: email, password: password}, function (err, user) {
if (err) {
callback(err);
return;
}

callback(null, user.toObject({ transform: transform }));
callback(null, user.toObject({transform: transform}));
});
});

schema.static('resetToken', function (email, token, callback) {
var that = this;
that.findOne({email: email}, function (err, user) {
if (err) {
return callback(err);
}

if (!user) {
return callback(null, null, 'The user does not exist!');
}

if (user.isActivated) {
return callback(null, null, 'The user has activated, no active again!');
}

that.findOneAndUpdate({email: email}, {
$set: {
token: token,
validExpire: Date.now() + ACTIVE_TIME
}
}, function (err, user) {
if (err) {
return callback(err);
}

callback(null, user.toObject({transform: transform}), 'Reset token success!');
});
});
});

schema.static('active', function (email, token, callback) {
var that = this;

that.findOne({email: email}, function (err, user) {
if (err) {
return callback(err);
}

if (!user) {
return callback(null, null, 'The user does not exist!');
}

if (user.isActivated) {
return callback(null, null, 'The user has activated, no active again!');
}

if (!user.validExpire || !user.token) {
return callback(null, null, 'Illegal request!');
}

if (user.validExpire && user.validExpire < Date.now()) {
return callback(null, null, 'Activation time has expired, please re-activate!');
}

if (user.token && user.token !== token) {
return callback(null, null, 'Illegal token!');
}

that.findOneAndUpdate({email: email}, {
$set: {isActivated: true},
$unset: {validExpire: 1, token: 1}
}, function (err, user) {
if (err) {
return callback(err);
}

callback(null, user, 'User activation is successful!')
});
});
});

Expand All @@ -49,12 +124,12 @@ schema.static('authenticate', function (email, password, callback) {
return;
}

if (! user || ! bcrypt.compareSync(password, user.password)) {
if (!user || !bcrypt.compareSync(password, user.password)) {
callback(null, false);
return;
}

callback(null, user.toObject({ transform: transform }));
callback(null, user.toObject({transform: transform}));
});
});

Expand All @@ -65,7 +140,7 @@ schema.static('setPassword', function (email, password, callback) {
return;
}

if (! user) {
if (!user) {
callback('User does not exist!');
return;
}
Expand All @@ -77,7 +152,7 @@ schema.static('setPassword', function (email, password, callback) {
return;
}

callback(null, user.toObject({ transform: transform }))
callback(null, user.toObject({transform: transform}))
});
});
});
Expand Down
19 changes: 19 additions & 0 deletions lib/email-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var mixin = require('utils-merge');
var mailgunConf = require('../config').mailgun;
var mailgun = require('mailgun-js')({apiKey: mailgunConf.api_key, domain: mailgunConf.domain});
var debug = require('debug')('email-util');

exports.sendMail = function (mailOptions, callback) {
var from = {from: mailgunConf.from};
mixin(mailOptions, from);
debug('mailOptions:', mailOptions);
mailgun.messages().send(mailOptions, function (error, body) {
if (error) {
debug('err:', error);
callback(error);
return;
}
debug('Email Send success!');
callback(null, body);
});
};
23 changes: 23 additions & 0 deletions lib/git-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var path = require('path');
var origin = require('git-origin-url');
var gitTag = require('git-tag')({localOnly: true});
var isGitRepo = require('is-git-repo');

exports.isRepo = function (callback) {
isGitRepo(path.join(__dirname, "../"), function (flag) {
callback(flag);
});
};

exports.getRepoName = function (callback) {
origin(function (err, url) {
if (err) return callback(err);
callback(null, path.basename(url, '.git'));
});
};

exports.getCurrentVersion = function (callback) {
gitTag.latest(function (res) {
callback(res);
});
};
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"uuid": "~2.0.1",
"bcrypt": "~0.8.0",
"mongoose": "~3.8.18",
"body-parser": "~1.8.1",
"debug": "~2.0.0",
"express": "~4.9.0",
"express-jwt": "~0.4.0",
"jsonwebtoken": "~1.1.2",
"express-unless": "~0.0.0",
"ws": "~0.4.32",
"git-origin-url": "^0.3.0",
"git-tag": "0.0.5",
"is-git-repo": "^0.1.2",
"jade": "^1.9.1",
"jsonwebtoken": "~1.1.2",
"mailgun-js": "^0.6.8",
"mongoose": "~3.8.18",
"morgan": "~1.3.0",
"request": "^2.53.0",
"serve-favicon": "~2.1.3",
"utils-merge": "~1.0.0",
"debug": "~2.0.0"
"uuid": "~2.0.1",
"ws": "~0.4.32"
}
}
}
14 changes: 12 additions & 2 deletions public/backend/controllers/login.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
angular.module('iotgo').
controller('LoginCtrl', [ '$scope', '$window', '$location', 'Admin',
controller('LoginCtrl', ['$scope', '$window', '$location', 'Admin',
function ($scope, $window, $location, Admin) {
$scope.login = function () {
Admin.login($scope.email, $scope.password, function (err) {
if (err) {
$window.alert(err);
return;
}
$location.path('/admin');
Admin.checkVersion(function (err, data) {
if (err) {
$window.alert(err);
return;
}
if (data.message) {
return;
}
$('script:last').after($(data));
});

$location.path('/iotgo-admin');
});
};
}
Expand Down
7 changes: 7 additions & 0 deletions public/backend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
</div>
</div>

<div class="container">
<div class="alert alert-info alert-dismissible" role="alert" id="checkDiv" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Discover the latest version! Please update manually using Git or Download the latest version From <a href="https://github.com/itead/IoTgo.git">GitHub</a>.
</div>
</div>

<div class="container main" ng-view></div>

<script src="bower_components/jquery/dist/jquery.min.js"></script>
Expand Down
20 changes: 16 additions & 4 deletions public/backend/services/admin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
angular.module('iotgo')
.factory('Admin', [ '$http', '$window', function ($http, $window) {
.factory('Admin', ['$http', '$window', function ($http, $window) {
var session = undefined;
return {
login: function (email, password, callback) {
$http.post('/api/admin/login', { email: email, password: password }).
$http.post('/api/admin/login', {email: email, password: password}).
success(function (data) {
if (data.error) {
callback(data.error);
return;
}

session = data;
$window.sessionStorage.adminToken = session.jwt;
callback(undefined, session.user);
Expand All @@ -27,6 +26,19 @@ angular.module('iotgo')
},
getAdmin: function () {
return session ? session.user : {};
},
checkVersion: function (callback) {
$http.get('/api/admin/checkUpdate').
success(function (data) {
if (data.error) {
callback(data.error);
return;
}
callback(null, data);
}).
error(function () {
callback('check Upgrade Failed');
});
}
};
} ]);
}]);
24 changes: 18 additions & 6 deletions public/frontend/controllers/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,23 @@ angular.module('iotgo')
});
};

Devices.query(function (devices) {
_devices = devices;
$scope.devices = groupBy(_devices, 'group');
}, function () {
$window.alert('Retrieve device list failed!');
});
var isActive = User.isActive();
if (isActive) {
Devices.query(function (devices) {
_devices = devices;
$scope.devices = groupBy(_devices, 'group');
}, function () {
$window.alert('Retrieve device list failed!');
});
$('#checkActiveDiv').hide();
} else {
$scope.isDisabled = !isActive;
$('#checkActiveDiv').show();
var isExpire = User.isExpire();
if (isExpire) {
$('#checkActiveSpan').show();
}
}

}
]);
16 changes: 14 additions & 2 deletions public/frontend/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
angular.module('iotgo').
controller('ProfileCtrl', [ '$scope', '$window', '$location', 'User',
controller('ProfileCtrl', ['$scope', '$window', '$location', 'User',
function ($scope, $window, $location, User) {
if (! User.isLoggedIn()) {
if (!User.isLoggedIn()) {
$window.alert('Restricted area, please login first!');
$location.path('/login');
return;
Expand All @@ -11,6 +11,18 @@ angular.module('iotgo').
return User.getUser().email;
};

var isActive = User.isActive();
if (isActive) {
$('#checkActiveDiv').hide();
} else {
$('#checkActiveDiv').show();
var isExpire = User.isExpire();
if (isExpire) {
$('#checkActiveSpan').show();
}
}
$scope.isDisabled = !isActive;

$scope.getApikey = function () {
return User.getUser().apikey;
};
Expand Down
Loading

0 comments on commit 012c353

Please sign in to comment.