Skip to content

Commit e3d761c

Browse files
committed
Formatting with Prettier
1 parent 01f01b4 commit e3d761c

32 files changed

+535
-524
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"after": true
7777
}],
7878
"semi-style": ["warn", "last"],
79-
"space-before-function-paren": ["off", 2]
79+
"space-before-function-paren": ["off", 2],
80+
"prettier/prettier": "error"
8081
},
8182
"extends": [
8283
"formidable/rules/eslint/best-practices/on",

.prettierrc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "none",
3+
"semi": false,
4+
"singleQuote": true,
5+
"printWidth": 80,
6+
"bracketSpacing": true
7+
}

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ It´s a good practice to do tests at your code, so a sample of how to do that in
106106
npm run test
107107
```
108108

109+
### Formatting code
110+
111+
Format your code with prettier by typing:
112+
113+
```bash
114+
npm run format
115+
```
116+
109117
## Usage
110118

111119
Once everything is set up to test API routes either use Postman or any other api testing application. Default username/password combination for login is `[email protected]/12345`.

app/controllers/auth.js

+24-26
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ const {
1414
sendResetPasswordEmailMessage
1515
} = require('./base')
1616
const uuid = require('uuid')
17-
const {
18-
addHours
19-
} = require('date-fns')
20-
const {
21-
matchedData
22-
} = require('express-validator/filter')
17+
const { addHours } = require('date-fns')
18+
const { matchedData } = require('express-validator/filter')
2319
const HOURS_TO_BLOCK = 2
2420
const LOGIN_ATTEMPTS = 5
2521

26-
2722
/*********************
2823
* Private functions *
2924
*********************/
@@ -58,7 +53,7 @@ const saveUserAccessAndReturnToken = async (req, user) => {
5853
country: getCountry(req)
5954
})
6055
return new Promise((resolve, reject) => {
61-
userAccess.save((err) => {
56+
userAccess.save(err => {
6257
if (err) {
6358
reject(buildErrObject(422, err.message))
6459
}
@@ -113,10 +108,8 @@ const checkPassword = async (password, user) => {
113108
})
114109
}
115110

116-
const blockIsExpired = ({
117-
loginAttempts,
118-
blockExpires,
119-
}) => loginAttempts > LOGIN_ATTEMPTS && blockExpires <= new Date()
111+
const blockIsExpired = ({ loginAttempts, blockExpires }) =>
112+
loginAttempts > LOGIN_ATTEMPTS && blockExpires <= new Date()
120113

121114
const checkLoginAttemptsAndBlockExpires = async user => {
122115
return new Promise((resolve, reject) => {
@@ -149,7 +142,8 @@ const userIsBlocked = async user => {
149142

150143
const findUser = async email => {
151144
return new Promise((resolve, reject) => {
152-
User.findOne({
145+
User.findOne(
146+
{
153147
email
154148
},
155149
'password loginAttempts blockExpires name email role verified',
@@ -206,7 +200,8 @@ const returnRegisterToken = (item, userInfo) => {
206200

207201
const verificationExists = async id => {
208202
return new Promise((resolve, reject) => {
209-
User.findOne({
203+
User.findOne(
204+
{
210205
verification: id,
211206
verified: false
212207
},
@@ -275,23 +270,27 @@ const updatePassword = async (password, user) => {
275270

276271
const findUserToResetPassword = async email => {
277272
return new Promise((resolve, reject) => {
278-
User.findOne({
279-
email
280-
}, (err, user) => {
281-
if (err) {
282-
reject(buildErrObject(422, err.message))
283-
}
284-
if (!user) {
285-
reject(buildErrObject(404, 'NOT_FOUND'))
273+
User.findOne(
274+
{
275+
email
276+
},
277+
(err, user) => {
278+
if (err) {
279+
reject(buildErrObject(422, err.message))
280+
}
281+
if (!user) {
282+
reject(buildErrObject(404, 'NOT_FOUND'))
283+
}
284+
resolve(user)
286285
}
287-
resolve(user)
288-
})
286+
)
289287
})
290288
}
291289

292290
const findForgotPassword = async id => {
293291
return new Promise((resolve, reject) => {
294-
ForgotPassword.findOne({
292+
ForgotPassword.findOne(
293+
{
295294
verification: id,
296295
used: false
297296
},
@@ -350,7 +349,6 @@ const checkPermissions = async (data, next) => {
350349
})
351350
}
352351

353-
354352
/********************
355353
* Public functions *
356354
********************/

app/controllers/auth.validate.js

+62-68
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
1-
const {
2-
handleError,
3-
buildErrObject
4-
} = require('./base')
5-
const {
6-
check,
7-
validationResult
8-
} = require('express-validator/check')
1+
const { handleError, buildErrObject } = require('./base')
2+
const { check, validationResult } = require('express-validator/check')
93

104
exports.register = [
115
check('name')
12-
.exists()
13-
.withMessage('MISSING')
14-
.not()
15-
.isEmpty()
16-
.withMessage('IS_EMPTY'),
6+
.exists()
7+
.withMessage('MISSING')
8+
.not()
9+
.isEmpty()
10+
.withMessage('IS_EMPTY'),
1711
check('email')
18-
.exists()
19-
.withMessage('MISSING')
20-
.not()
21-
.isEmpty()
22-
.withMessage('IS_EMPTY')
23-
.normalizeEmail(),
12+
.exists()
13+
.withMessage('MISSING')
14+
.not()
15+
.isEmpty()
16+
.withMessage('IS_EMPTY')
17+
.normalizeEmail(),
2418
check('password')
25-
.exists()
26-
.withMessage('MISSING')
27-
.not()
28-
.isEmpty()
29-
.withMessage('IS_EMPTY')
30-
.isLength({
31-
min: 5
32-
})
33-
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
19+
.exists()
20+
.withMessage('MISSING')
21+
.not()
22+
.isEmpty()
23+
.withMessage('IS_EMPTY')
24+
.isLength({
25+
min: 5
26+
})
27+
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
3428
(req, res, next) => {
3529
try {
3630
validationResult(req).throw()
@@ -43,20 +37,20 @@ exports.register = [
4337

4438
exports.login = [
4539
check('email')
46-
.exists()
47-
.withMessage('MISSING')
48-
.not()
49-
.isEmpty()
50-
.withMessage('IS_EMPTY')
51-
.isEmail()
52-
.withMessage('EMAIL_IS_NOT_VALID')
53-
.normalizeEmail(),
40+
.exists()
41+
.withMessage('MISSING')
42+
.not()
43+
.isEmpty()
44+
.withMessage('IS_EMPTY')
45+
.isEmail()
46+
.withMessage('EMAIL_IS_NOT_VALID')
47+
.normalizeEmail(),
5448
check('password')
55-
.exists()
56-
.withMessage('MISSING')
57-
.not()
58-
.isEmpty()
59-
.withMessage('IS_EMPTY'),
49+
.exists()
50+
.withMessage('MISSING')
51+
.not()
52+
.isEmpty()
53+
.withMessage('IS_EMPTY'),
6054
(req, res, next) => {
6155
try {
6256
validationResult(req).throw()
@@ -69,11 +63,11 @@ exports.login = [
6963

7064
exports.verify = [
7165
check('id')
72-
.exists()
73-
.withMessage('MISSING')
74-
.not()
75-
.isEmpty()
76-
.withMessage('IS_EMPTY'),
66+
.exists()
67+
.withMessage('MISSING')
68+
.not()
69+
.isEmpty()
70+
.withMessage('IS_EMPTY'),
7771
(req, res, next) => {
7872
try {
7973
validationResult(req).throw()
@@ -86,14 +80,14 @@ exports.verify = [
8680

8781
exports.forgotPassword = [
8882
check('email')
89-
.exists()
90-
.withMessage('MISSING')
91-
.not()
92-
.isEmpty()
93-
.withMessage('IS_EMPTY')
94-
.isEmail()
95-
.withMessage('EMAIL_IS_NOT_VALID')
96-
.normalizeEmail(),
83+
.exists()
84+
.withMessage('MISSING')
85+
.not()
86+
.isEmpty()
87+
.withMessage('IS_EMPTY')
88+
.isEmail()
89+
.withMessage('EMAIL_IS_NOT_VALID')
90+
.normalizeEmail(),
9791
(req, res, next) => {
9892
try {
9993
validationResult(req).throw()
@@ -106,21 +100,21 @@ exports.forgotPassword = [
106100

107101
exports.resetPassword = [
108102
check('id')
109-
.exists()
110-
.withMessage('MISSING')
111-
.not()
112-
.isEmpty()
113-
.withMessage('IS_EMPTY'),
103+
.exists()
104+
.withMessage('MISSING')
105+
.not()
106+
.isEmpty()
107+
.withMessage('IS_EMPTY'),
114108
check('password')
115-
.exists()
116-
.withMessage('MISSING')
117-
.not()
118-
.isEmpty()
119-
.withMessage('IS_EMPTY')
120-
.isLength({
121-
min: 5
122-
})
123-
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
109+
.exists()
110+
.withMessage('MISSING')
111+
.not()
112+
.isEmpty()
113+
.withMessage('IS_EMPTY')
114+
.isLength({
115+
min: 5
116+
})
117+
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
124118
(req, res, next) => {
125119
try {
126120
validationResult(req).throw()

app/controllers/base.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ exports.getCountry = req =>
2121

2222
exports.emailExists = async email => {
2323
return new Promise((resolve, reject) => {
24-
User.findOne({
24+
User.findOne(
25+
{
2526
email
2627
},
2728
(err, item) => {
@@ -39,7 +40,8 @@ exports.emailExists = async email => {
3940

4041
exports.emailExistsExcludingMyself = async (id, email) => {
4142
return new Promise((resolve, reject) => {
42-
User.findOne({
43+
User.findOne(
44+
{
4345
email,
4446
_id: {
4547
$ne: id
@@ -102,9 +104,9 @@ exports.sendRegistrationEmailMessage = async user => {
102104
this.sendEmail(
103105
data,
104106
messageSent =>
105-
messageSent ?
106-
console.log(`Email SENT to: ${user.email}`) :
107-
console.log(`Email FAILED to: ${user.email}`)
107+
messageSent
108+
? console.log(`Email SENT to: ${user.email}`)
109+
: console.log(`Email FAILED to: ${user.email}`)
108110
)
109111
} else if (process.env.NODE_ENV === 'development') {
110112
console.log(email)
@@ -132,9 +134,9 @@ exports.sendResetPasswordEmailMessage = async user => {
132134
this.sendEmail(
133135
data,
134136
messageSent =>
135-
messageSent ?
136-
console.log(`Email SENT to: ${user.email}`) :
137-
console.log(`Email FAILED to: ${user.email}`)
137+
messageSent
138+
? console.log(`Email SENT to: ${user.email}`)
139+
: console.log(`Email FAILED to: ${user.email}`)
138140
)
139141
} else if (process.env.NODE_ENV === 'development') {
140142
console.log(email)
@@ -188,18 +190,18 @@ exports.buildSuccObject = msg => {
188190
exports.isIDGood = async id => {
189191
return new Promise((resolve, reject) => {
190192
const goodID = String(id).match(/^[0-9a-fA-F]{24}$/)
191-
return goodID ?
192-
resolve(id) :
193-
reject(this.buildErrObject(422, 'ID_MALFORMED'))
193+
return goodID
194+
? resolve(id)
195+
: reject(this.buildErrObject(422, 'ID_MALFORMED'))
194196
})
195197
}
196198

197199
exports.checkQueryString = async query => {
198200
return new Promise((resolve, reject) => {
199201
try {
200-
return typeof query !== 'undefined' ?
201-
resolve(JSON.parse(query)) :
202-
resolve({})
202+
return typeof query !== 'undefined'
203+
? resolve(JSON.parse(query))
204+
: resolve({})
203205
} catch (err) {
204206
console.log(err.message)
205207
return reject(

0 commit comments

Comments
 (0)