Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloocana committed Mar 8, 2017
1 parent 86c0adc commit 2b25a3d
Show file tree
Hide file tree
Showing 25 changed files with 1,839 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

*.swp
.env
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "node"
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
# ptz-user-app
# ptz-user-app

[![Build Status](https://travis-ci.org/polutz/ptz-user-app.svg)](https://travis-ci.org/polutz/ptz-user-app)
[![codecov.io](http://codecov.io/github/polutz/ptz-user-app/coverage.svg)](http://codecov.io/github/polutz/ptz-user-app)
[![Dependency Status](https://gemnasium.com/polutz/ptz-user-app.svg)](https://gemnasium.com/polutz/ptz-user-app)
[![bitHound Score](https://www.bithound.io/github/gotwarlost/istanbul/badges/score.svg)](https://www.bithound.io/github/polutz/ptz-user-app)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](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
```
236 changes: 236 additions & 0 deletions dist/userApp.js
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;
Loading

0 comments on commit 2b25a3d

Please sign in to comment.