diff --git a/api/.eslintrc.json b/api/.eslintrc.json index fcd7bd0..b1d4df0 100644 --- a/api/.eslintrc.json +++ b/api/.eslintrc.json @@ -1,39 +1,21 @@ { - "extends": "eslint:recommended", - "env": { - "node": true, - "browser": false, - "es6": true - }, - "parserOptions": { - "ecmaVersion": 2018 - }, - "rules": { - "semi": [ - "error" - ], - "no-trailing-spaces": [ - "error" - ], - "object-curly-newline": [ - "error" - ], - "object-property-newline": [ - "error" - ], - "quotes": [ - "error", - "backtick" - ], - "indent": [ - "error", - "tab" - ], - "curly": [ - "error" - ], - "brace-style": [ - "error" - ] - } + "extends": ["eslint:recommended", "prettier"], + "env": { + "node": true, + "browser": false, + "es6": true + }, + "parserOptions": { + "ecmaVersion": 2018 + }, + "rules": { + "semi": ["error"], + "no-trailing-spaces": ["error"], + "object-curly-newline": ["error"], + "object-property-newline": ["error"], + "quotes": ["error", "backtick"], + "indent": ["error", "tab"], + "curly": ["error"], + "brace-style": ["error"] + } } diff --git a/api/.prettierignore b/api/.prettierignore new file mode 100644 index 0000000..cce0279 --- /dev/null +++ b/api/.prettierignore @@ -0,0 +1,2 @@ +package.json +package-lock.json diff --git a/api/app.js b/api/app.js index 31c9b17..f6fea9c 100644 --- a/api/app.js +++ b/api/app.js @@ -1,9 +1,7 @@ const express = require(`express`); const app = express(); const passport = require(`passport`); -const { - users -} = require(`./controllers`); +const { users } = require(`./controllers`); /** * Configure Passport @@ -37,10 +35,8 @@ app.use(express.json()); // Since Express doesn't support error handling of promises out of the box, // this handler enables that -const asyncHandler = fn => (req, res, next) => { - return Promise - .resolve(fn(req, res, next)) - .catch(next); +const asyncHandler = (fn) => (req, res, next) => { + return Promise.resolve(fn(req, res, next)).catch(next); }; /** @@ -63,7 +59,11 @@ app.get(`/test/`, (req, res) => { * Routes - Protected */ -app.post(`/user`, passport.authenticate(`jwt`, { session: false }), asyncHandler(users.get)); +app.post( + `/user`, + passport.authenticate(`jwt`, { session: false }), + asyncHandler(users.get) +); /** * Routes - Catch-All @@ -78,7 +78,9 @@ app.get(`/*`, (req, res) => { */ app.use(function (err, req, res) { console.error(err); - res.status(500).json({ error: `Internal Serverless Error - "${err.message}"` }); + res.status(500).json({ + error: `Internal Serverless Error - "${err.message}"`, + }); }); module.exports = app; diff --git a/api/config/passport.js b/api/config/passport.js index 4c4b2fb..2fd3ff3 100644 --- a/api/config/passport.js +++ b/api/config/passport.js @@ -11,18 +11,20 @@ module.exports = (passport) => { options.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken(); options.secretOrKey = process.env.tokenSecret; - passport.use(new StrategyJWT(options, async (jwtPayload, done) => { - let user; - try { - user = await users.getById(jwtPayload.id); - } catch (error) { - console.log(error); - return done(error, null); - } + passport.use( + new StrategyJWT(options, async (jwtPayload, done) => { + let user; + try { + user = await users.getById(jwtPayload.id); + } catch (error) { + console.log(error); + return done(error, null); + } - if (!user) { - return done(null, false); - } - return done(null, users.convertToPublicFormat(user)); - })); + if (!user) { + return done(null, false); + } + return done(null, users.convertToPublicFormat(user)); + }) + ); }; diff --git a/api/controllers/index.js b/api/controllers/index.js index 94c6e9b..ce3f1b6 100644 --- a/api/controllers/index.js +++ b/api/controllers/index.js @@ -2,4 +2,4 @@ const users = require(`./users`); module.exports = { users, -}; \ No newline at end of file +}; diff --git a/api/controllers/users.js b/api/controllers/users.js index 309e737..636e368 100644 --- a/api/controllers/users.js +++ b/api/controllers/users.js @@ -27,13 +27,17 @@ const register = async (req, res, next) => { return next(error, null); } - const token = jwt.sign(users.convertToPublicFormat(user), process.env.tokenSecret, { - expiresIn: 604800 // 1 week - }); + const token = jwt.sign( + users.convertToPublicFormat(user), + process.env.tokenSecret, + { + expiresIn: 604800, // 1 week + } + ); res.json({ message: `Authentication successful`, - token + token, }); }; @@ -53,21 +57,29 @@ const login = async (req, res, next) => { } if (!user) { - return res.status(404).send({ error: `Authentication failed. User not found.` }); + return res + .status(404) + .send({ error: `Authentication failed. User not found.` }); } const isCorrect = comparePassword(req.body.password, user.password); if (!isCorrect) { - return res.status(401).send({ error: `Authentication failed. Wrong password.` }); + return res + .status(401) + .send({ error: `Authentication failed. Wrong password.` }); } - const token = jwt.sign(users.convertToPublicFormat(user), process.env.tokenSecret, { - expiresIn: 604800 // 1 week - }); + const token = jwt.sign( + users.convertToPublicFormat(user), + process.env.tokenSecret, + { + expiresIn: 604800, // 1 week + } + ); res.json({ message: `Authentication successful`, - token + token, }); }; diff --git a/api/models/db.js b/api/models/db.js index a2ab596..fd380be 100644 --- a/api/models/db.js +++ b/api/models/db.js @@ -2,7 +2,7 @@ const AWS = require(`aws-sdk`); const shortid = require(`shortid`); const dynamodb = new AWS.DynamoDB.DocumentClient({ - region: process.env.AWS_REGION + region: process.env.AWS_REGION, }); const defaultParams = { @@ -18,7 +18,7 @@ const put = async (kind, item) => { sk2: shortid.generate(), createdAt: Date.now(), updatedAt: Date.now(), - } + }, }; await dynamodb.put(params).promise(); @@ -28,7 +28,7 @@ const getByKey = async (key) => { const params = { ...defaultParams, KeyConditionExpression: `hk = :hk`, - ExpressionAttributeValues: { ':hk': key } + ExpressionAttributeValues: { ":hk": key }, }; return await dynamodb.query(params).promise(); @@ -40,9 +40,9 @@ const getById = async (type, id) => { IndexName: process.env.dbIndex1, KeyConditionExpression: `sk2 = :sk2 and sk = :sk`, ExpressionAttributeValues: { - ':sk2': id, - ':sk': type, - } + ":sk2": id, + ":sk": type, + }, }; return await dynamodb.query(params).promise(); diff --git a/api/models/index.js b/api/models/index.js index 94c6e9b..ce3f1b6 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -2,4 +2,4 @@ const users = require(`./users`); module.exports = { users, -}; \ No newline at end of file +}; diff --git a/api/models/users.js b/api/models/users.js index d1a3c05..710a828 100644 --- a/api/models/users.js +++ b/api/models/users.js @@ -25,17 +25,17 @@ const register = async (user = {}) => { // Check if user is already registered const existingUser = await getByEmail(user.email); if (existingUser) { - throw new Error(`A user with email "${user.email}" is already registered`); + throw new Error( + `A user with email "${user.email}" is already registered` + ); } user.password = utils.hashPassword(user.password); - await db.put( - `user`, - { - hk: user.email, - password: user.password, - }); + await db.put(`user`, { + hk: user.email, + password: user.password, + }); }; /** diff --git a/api/package.json b/api/package.json index dfb5f0d..8acafc4 100644 --- a/api/package.json +++ b/api/package.json @@ -12,11 +12,14 @@ "shortid": "^2.2.15" }, "devDependencies": { - "eslint": "^7.12.1" + "eslint": "^7.12.1", + "eslint-config-prettier": "^6.15.0", + "prettier": "^2.1.2" }, "scripts": { "pretest": "eslint --ignore-path ../.gitignore .", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "format": "prettier --write \"**/*.{js,json,md,yml}\"" }, "author": "", "license": "ISC" diff --git a/api/utils/index.js b/api/utils/index.js index 15e9d18..0f36b9b 100644 --- a/api/utils/index.js +++ b/api/utils/index.js @@ -31,5 +31,5 @@ const comparePassword = (candidatePassword, trustedPassword) => { module.exports = { hashPassword, comparePassword, - validateEmailAddress + validateEmailAddress, };