Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorJJF committed Aug 11, 2020
1 parent 6da3ba6 commit 123f3e8
Show file tree
Hide file tree
Showing 36 changed files with 9,346 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DB
MONGO_URI=
# TOKEN
SALTROUNDS=
EXPIRESIN=
JWT_SECRET=
JWT_EXPIRATION_IN_MINUTES=
# EMAILS
EMAIL_FROM_NAME=
EMAIL_FROM_ADDRESS=
EMAIL_SMTP_DOMAIN_MAILGUN=
EMAIL_SMTP_API_MAILGUN=
# FRONTEND
FRONTEND_URL=http://localhost:8080
# ENVIRONMENT
NODE_ENV=development
60 changes: 60 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"env": {
"commonjs": true,
"es2020": true,
"node": true
},
"extends": "airbnb",
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
//custom
"callback-return": [
"error",
["done", "proceed", "next", "onwards", "callback", "cb"]
],
// "no-extra-semi": ["warn"],
"max-nested-callbacks": ["off"],
"no-unused-vars": [
"warn",
{
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^unused($|[A-Z].*$)"
}
],
//end
"import/no-extraneous-dependencies": ["error", {
"devDependencies": true
}],
"no-async-promise-executor": ["off"],
"no-plusplus": [2, {
"allowForLoopAfterthoughts": true
}],
"implicit-arrow-linebreak": ["off"],
"no-param-reassign": ["off"],
"operator-linebreak": ["off"],
"no-underscore-dangle": ["off"],
"no-prototype-builtins": ["off"], //like hasownproperty
"no-use-before-define": ["off"],
"consistent-return": ["off"],
"new-cap": ["off"], //CHECK LATER New Model
"prefer-const": ["off"],
"camelcase": ["off"],
"no-iterator": ["off"],
"no-console": ["off"],
"global-require": ["off"],
"no-restricted-syntax": ["off"],
"import/no-dynamic-require": ["off"],
"no-await-in-loop": ["off"],
"comma-dangle": [2, "always-multiline"],
"quotes": [
"warn",
"single",
{
"avoidEscape": false,
"allowTemplateLiterals": true
}
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
client/node_modules
node_modules
.env
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"semi": true,
"singleQuote": true,
"printWidth": 80,
"bracketSpacing": true
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript"],
"eslint.workingDirectories": [{
"mode": "auto"
}],
"javascript.suggestionActions.enabled": false
}
46 changes: 46 additions & 0 deletions config/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const mongoose = require('mongoose');
mongoose.plugin(require('mongoose-beautiful-unique-validation')); // unique validator
mongoose.plugin(require('mongoose-paginate-v2')); // paginator
mongoose.plugin(require('mongoose-aggregate-paginate-v2'));
// aggregate paginator
const DB_URL = process.env.MONGO_URI;
const loadModels = require('../models');

module.exports = () => {
const connect = () => {
mongoose.Promise = global.Promise;

mongoose.connect(
DB_URL,
{
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
let dbStatus = '';
if (err) {
dbStatus = `* Error connecting to DB: ${err}\n****************************\n`;
}
dbStatus = '* DB Connection: OK\n****************************\n';
if (process.env.NODE_ENV !== 'test') {
// Prints initialization
console.log('****************************');
console.log('* Starting Server');
console.log(`* Port: ${process.env.PORT || 3000}`);
console.log(`* NODE_ENV: ${process.env.NODE_ENV}`);
console.log('* Database: MongoDB');
console.log(dbStatus);
}
},
);
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
};
connect();

mongoose.connection.on('error', console.log);
mongoose.connection.on('disconnected', connect);

loadModels();
};
45 changes: 45 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const User = require('../models/Users');
const auth = require('../helpers/auth');

/**
* Extracts token from: header, body or query
* @param {Object} req - request object
* @returns {string} token - decrypted token
*/
const jwtExtractor = (req) => {
let token = null;
if (req.headers.authorization) {
token = req.headers.authorization.replace('Bearer ', '').trim();
} else if (req.body.token) {
token = req.body.token.trim();
} else if (req.query.token) {
token = req.query.token.trim();
}
if (token) {
// Decrypts token
token = auth.decrypt(token);
}
return token;
};
/**
* Options object for jwt middlware
*/
const jwtOptions = {
jwtFromRequest: jwtExtractor,
secretOrKey: process.env.JWT_SECRET,
};
/**
* Login with JWT middleware
*/
const jwtLogin = new JwtStrategy(jwtOptions, (payload, done) => {
User.findById(payload.data._id, (err, user) => {
if (err) {
return done(err, false);
}
return !user ? done(null, false) : done(null, user);
});
});

passport.use(jwtLogin);
125 changes: 125 additions & 0 deletions controllers/auth.validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const { check } = require('express-validator');
const { validationResult } = require('../helpers/utils');

/**
* Validates register request
*/
exports.register = [
check('first_name')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY'),
check('last_name')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY'),
check('email')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isEmail()
.withMessage('EMAIL_IS_NOT_VALID'),
check('password')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isLength({
min: 5,
})
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
(req, res, next) => {
validationResult(req, res, next);
},
];

/**
* Validates login request
*/
exports.login = [
check('email')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isEmail()
.withMessage('EMAIL_IS_NOT_VALID'),
check('password')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isLength({
min: 5,
})
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
(req, res, next) => {
validationResult(req, res, next);
},
];

/**
* Validates verify request
*/
exports.verify = [
check('id')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY'),
(req, res, next) => {
validationResult(req, res, next);
},
];

/**
* Validates forgot password request
*/
exports.forgotPassword = [
check('email')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isEmail()
.withMessage('EMAIL_IS_NOT_VALID'),
(req, res, next) => {
validationResult(req, res, next);
},
];

/**
* Validates reset password request
*/
exports.resetPassword = [
check('id')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY'),
check('password')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isLength({
min: 5,
})
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
(req, res, next) => {
validationResult(req, res, next);
},
];
Loading

0 comments on commit 123f3e8

Please sign in to comment.