Skip to content

Commit

Permalink
update authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Tris Setiawan committed May 13, 2016
1 parent a59b81b commit ad90d8f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
27 changes: 22 additions & 5 deletions app/managers/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,32 @@ module.exports = class AccountManager extends Manager {

authenticate(username, password) {
return new Promise(function (resolve, reject) {
var query = { username: username, password: sha1(password) };
var query = { username: username, password: sha1(password || '') };
// var query = { username: username };

this.dbSingleOrDefault(map.identity.account, query)
.then(account => {
if (account)
resolve(true);
if (account) {
var loadProfile = this.dbSingle(map.identity.userProfile, { accountId: account._id });
var loadInfo = this.dbSingle(map.identity.userOrganizationInfo, { accountId: account._id });
Promise.all([loadProfile, loadInfo])
.then(results => {
var profile = results[0];
var info = results[1];
var data = {
id:account._id,
username: account.username,
name: profile.name,
nik: info.nik,
initial: info.initial,
department: info.department
};
resolve(data);
})
.catch(e => reject(e));
}
else
resolve(false);
reject("invalid username or password");
})
.catch(e => {
reject(e);
Expand Down Expand Up @@ -115,7 +132,7 @@ module.exports = class AccountManager extends Manager {
var query = { 'username': account.username };
if (account.password && account.password.length > 0)
account.password = sha1(account.password);

return new Promise(function (resolve, reject) {

this.dbUpdate(map.identity.account, query, account, true)
Expand Down
21 changes: 8 additions & 13 deletions app/services/authentication-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ module.exports = class AuthenticationService extends Service {
var accountManager = new AccountManager(request.db);
accountManager.authenticate(username, password)
.then(result => {
if (result === true) {
accountManager.get(username)
.then(user => {
var tokenOption = {};//{ expiresInMinutes: 1440 };
var token = jwt.sign(user, config.secret, tokenOption);
response.locals.data = { token: token }
next();
})
.catch(e => next(e));
}
else {
next("Authentication failed. Invalid username or password");

var tokenOption = {};//{ expiresInMinutes: 1440 };
var token = jwt.sign(result, config.secret, tokenOption);
response.locals.data = {
token: token,
user: result
}
next();
})
.catch(e => {
.catch(e => {
next("Authentication failed. Invalid username or password");
});
}
Expand Down

0 comments on commit ad90d8f

Please sign in to comment.