Add prettier to api
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "eslint:recommended",
|
||||
"extends": ["eslint:recommended", "prettier"],
|
||||
"env": {
|
||||
"node": true,
|
||||
"browser": false,
|
||||
@@ -9,31 +9,13 @@
|
||||
"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"
|
||||
]
|
||||
"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"]
|
||||
}
|
||||
}
|
||||
|
||||
2
api/.prettierignore
Normal file
2
api/.prettierignore
Normal file
@@ -0,0 +1,2 @@
|
||||
package.json
|
||||
package-lock.json
|
||||
20
api/app.js
20
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;
|
||||
|
||||
@@ -11,7 +11,8 @@ module.exports = (passport) => {
|
||||
options.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken();
|
||||
options.secretOrKey = process.env.tokenSecret;
|
||||
|
||||
passport.use(new StrategyJWT(options, async (jwtPayload, done) => {
|
||||
passport.use(
|
||||
new StrategyJWT(options, async (jwtPayload, done) => {
|
||||
let user;
|
||||
try {
|
||||
user = await users.getById(jwtPayload.id);
|
||||
@@ -24,5 +25,6 @@ module.exports = (passport) => {
|
||||
return done(null, false);
|
||||
}
|
||||
return done(null, users.convertToPublicFormat(user));
|
||||
}));
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -25,14 +25,14 @@ 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`,
|
||||
{
|
||||
await db.put(`user`, {
|
||||
hk: user.email,
|
||||
password: user.password,
|
||||
});
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -31,5 +31,5 @@ const comparePassword = (candidatePassword, trustedPassword) => {
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
comparePassword,
|
||||
validateEmailAddress
|
||||
validateEmailAddress,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user