-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c0adc
commit 2b25a3d
Showing
25 changed files
with
1,839 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,6 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
*.swp | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- "node" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# ptz-user-app | ||
# ptz-user-app | ||
|
||
[](https://travis-ci.org/polutz/ptz-user-app) | ||
[](http://codecov.io/github/polutz/ptz-user-app) | ||
[](https://gemnasium.com/polutz/ptz-user-app) | ||
[](https://www.bithound.io/github/polutz/ptz-user-app) | ||
[](http://opensource.org/licenses/MIT) | ||
|
||
|
||
Core Domain: https://github.com/polutz/ptz-core-domain | ||
|
||
User Domain: https://github.com/polutz/ptz-user-app | ||
|
||
User Repository: https://github.com/polutz/ptz-user-repository | ||
|
||
User App: https://github.com/polutz/ptz-user-app | ||
|
||
Projeto que utiliza os modulos acima: https://github.com/angeloocana/freecomclub | ||
|
||
Tarefas: https://trello.com/b/w9BqiPdz/frecom-club | ||
|
||
Stack: react, redux, relay, graphql, nodejs e mongodb. | ||
|
||
Metodologias: TDD (Test Driven Development), DDD (Domain Driven Design). | ||
|
||
Tools: Docker, Typescript, babel, webpack, mocha, gulp. | ||
|
||
## How to use in your project | ||
|
||
``` | ||
npm install ptz-user-app --save | ||
``` | ||
|
||
To enable Typescript support, add to your typings/index.d.ts | ||
``` | ||
/// <reference path="../node_modules/ptz-user-app/src/typings/index.d.ts" /> | ||
``` | ||
|
||
|
||
## Prerequisites | ||
|
||
1. Latest version of Node to be installed. | ||
|
||
## NPM Global packages | ||
``` | ||
npm install ts-node -g | ||
npm install typescript-node -g | ||
npm install babel-cli -g | ||
``` | ||
|
||
## Typings Global Packages | ||
``` | ||
typings install dt~mocha --global --save | ||
``` | ||
|
||
## Setup | ||
``` | ||
npm install | ||
node gensalt.js | ||
``` | ||
|
||
## Test | ||
``` | ||
npm test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _ptzUserDomain = require('ptz-user-domain'); | ||
|
||
var _bcryptjs = require('bcryptjs'); | ||
|
||
var _jwtSimple = require('jwt-simple'); | ||
|
||
var __awaiter = undefined && undefined.__awaiter || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : new P(function (resolve) { | ||
resolve(result.value); | ||
}).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
|
||
function UserApp(userRepository) { | ||
var tokenSecret, passwordSalt; | ||
passwordSalt = tokenSecret = process.env.PASSWORD_SALT; | ||
function hashPassword(user) { | ||
return __awaiter(this, void 0, void 0, regeneratorRuntime.mark(function _callee() { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (user.password) { | ||
_context.next = 2; | ||
break; | ||
} | ||
|
||
return _context.abrupt('return', Promise.resolve(user)); | ||
|
||
case 2: | ||
if (passwordSalt) { | ||
_context.next = 4; | ||
break; | ||
} | ||
|
||
throw 'passwordSalt not added to process.env.'; | ||
|
||
case 4: | ||
_context.next = 6; | ||
return (0, _bcryptjs.hash)(user.password, passwordSalt); | ||
|
||
case 6: | ||
user.passwordHash = _context.sent; | ||
|
||
user.password = undefined; | ||
return _context.abrupt('return', Promise.resolve(user)); | ||
|
||
case 9: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
} | ||
function save(user) { | ||
return __awaiter(this, void 0, void 0, regeneratorRuntime.mark(function _callee2() { | ||
var isUpdate, otherUsers, users, userDb; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
isUpdate = !!user.id; | ||
|
||
user = new _ptzUserDomain.User(user); | ||
_context2.next = 4; | ||
return hashPassword(user); | ||
|
||
case 4: | ||
user = _context2.sent; | ||
|
||
if (user.isValid()) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
|
||
return _context2.abrupt('return', Promise.resolve(user)); | ||
|
||
case 7: | ||
_context2.next = 9; | ||
return userRepository.getOtherUsersWithSameUserNameOrEmail(user); | ||
|
||
case 9: | ||
otherUsers = _context2.sent; | ||
|
||
if (!user.otherUsersWithSameUserNameOrEmail(otherUsers)) { | ||
_context2.next = 12; | ||
break; | ||
} | ||
|
||
return _context2.abrupt('return', Promise.resolve(user)); | ||
|
||
case 12: | ||
if (!isUpdate) { | ||
_context2.next = 18; | ||
break; | ||
} | ||
|
||
_context2.next = 15; | ||
return userRepository.getByIds([user.id]); | ||
|
||
case 15: | ||
users = _context2.sent; | ||
userDb = new _ptzUserDomain.User(users[0]); | ||
|
||
user = userDb.update(user); | ||
|
||
case 18: | ||
_context2.next = 20; | ||
return userRepository.save(user); | ||
|
||
case 20: | ||
user = _context2.sent; | ||
return _context2.abrupt('return', Promise.resolve(user)); | ||
|
||
case 22: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
} | ||
function find(query, _ref) { | ||
var limit = _ref.limit; | ||
|
||
return userRepository.find(query, { limit: limit }); | ||
} | ||
function authenticateUser(userNameOrEmail, password) { | ||
return __awaiter(this, void 0, void 0, regeneratorRuntime.mark(function _callee3() { | ||
var user, userError, res; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.next = 2; | ||
return userRepository.getByUserNameOrEmail(userNameOrEmail); | ||
|
||
case 2: | ||
user = _context3.sent; | ||
userError = _ptzUserDomain.User.getUserAthenticationError(userNameOrEmail); | ||
|
||
if (user) { | ||
_context3.next = 6; | ||
break; | ||
} | ||
|
||
return _context3.abrupt('return', Promise.resolve(userError)); | ||
|
||
case 6: | ||
_context3.next = 8; | ||
return (0, _bcryptjs.compare)(password, user.passwordHash); | ||
|
||
case 8: | ||
res = _context3.sent; | ||
|
||
if (!res) { | ||
_context3.next = 13; | ||
break; | ||
} | ||
|
||
return _context3.abrupt('return', Promise.resolve(user)); | ||
|
||
case 13: | ||
return _context3.abrupt('return', Promise.resolve(userError)); | ||
|
||
case 14: | ||
case 'end': | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
} | ||
function getAuthToken(userNameOrEmail, password) { | ||
return __awaiter(this, void 0, void 0, regeneratorRuntime.mark(function _callee4() { | ||
var user; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.next = 2; | ||
return authenticateUser(userNameOrEmail, password); | ||
|
||
case 2: | ||
user = _context4.sent; | ||
|
||
if (user.isValid()) user.accessToken = (0, _jwtSimple.encode)(user, tokenSecret); | ||
return _context4.abrupt('return', Promise.resolve(user)); | ||
|
||
case 5: | ||
case 'end': | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
} | ||
function verifyAuthToken(token) { | ||
var user = (0, _jwtSimple.decode)(token, passwordSalt); | ||
return Promise.resolve(user); | ||
} | ||
return { | ||
save: save, | ||
find: find, | ||
getAuthToken: getAuthToken, | ||
verifyAuthToken: verifyAuthToken, | ||
hashPassword: hashPassword, | ||
authenticateUser: authenticateUser | ||
}; | ||
} | ||
exports.default = UserApp; |
Oops, something went wrong.