From 0193935d2d4a00f97e46e2d933e36ccbd5f4ecee Mon Sep 17 00:00:00 2001 From: benjaminramey Date: Wed, 11 Nov 2020 20:58:18 -0600 Subject: [PATCH] Fix bugs from refactor! --- .editorconfig | 4 +-- api/config/passport.js | 3 +- api/controllers/users.js | 4 +-- api/models/users.js | 12 ++----- api/package.json | 42 +++++++++++------------ api/serverless.yml | 28 +++++++-------- database/serverless.yml | 44 ++++++++++++------------ permissions/serverless.yml | 48 +++++++++++++------------- serverless.yml | 2 +- site/package.json | 70 +++++++++++++++++++------------------- site/serverless.yml | 14 ++++---- 11 files changed, 132 insertions(+), 139 deletions(-) diff --git a/.editorconfig b/.editorconfig index d5accb9..4c92222 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,5 +18,5 @@ indent_size = 4 # Matches the exact files either package.json or .travis.yml [{package.json,serverless.yml}] -indent_style = space -indent_size = 2 +indent_style = tab +indent_size = 4 diff --git a/api/config/passport.js b/api/config/passport.js index 1bf8a32..4c4b2fb 100644 --- a/api/config/passport.js +++ b/api/config/passport.js @@ -7,7 +7,6 @@ const ExtractJWT = require(`passport-jwt`).ExtractJwt; const { users } = require(`../models`); module.exports = (passport) => { - const options = {}; options.jwtFromRequest = ExtractJWT.fromAuthHeaderAsBearerToken(); options.secretOrKey = process.env.tokenSecret; @@ -24,6 +23,6 @@ module.exports = (passport) => { if (!user) { return done(null, false); } - return done(null, user); + return done(null, users.convertToPublicFormat(user)); })); }; diff --git a/api/controllers/users.js b/api/controllers/users.js index 7809ad8..309e737 100644 --- a/api/controllers/users.js +++ b/api/controllers/users.js @@ -27,7 +27,7 @@ const register = async (req, res, next) => { return next(error, null); } - const token = jwt.sign(user, process.env.tokenSecret, { + const token = jwt.sign(users.convertToPublicFormat(user), process.env.tokenSecret, { expiresIn: 604800 // 1 week }); @@ -61,7 +61,7 @@ const login = async (req, res, next) => { return res.status(401).send({ error: `Authentication failed. Wrong password.` }); } - const token = jwt.sign(user, process.env.tokenSecret, { + const token = jwt.sign(users.convertToPublicFormat(user), process.env.tokenSecret, { expiresIn: 604800 // 1 week }); diff --git a/api/models/users.js b/api/models/users.js index a3d83b6..d1a3c05 100644 --- a/api/models/users.js +++ b/api/models/users.js @@ -35,7 +35,7 @@ const register = async (user = {}) => { { hk: user.email, password: user.password, - }).promise(); + }); }; /** @@ -51,12 +51,9 @@ const getByEmail = async (email) => { throw new Error(`"${email}" is not a valid email address`); } - let user = await db.getByKey(email).promise(); + let user = await db.getByKey(email); user = user.Items && user.Items[0] ? user.Items[0] : null; - if (user) { - user = convertToPublicFormat(user); - } return user; }; @@ -70,12 +67,9 @@ const getById = async (id) => { throw new Error(`"id" is required`); } - let user = await db.getById(`user`, id).promise(); + let user = await db.getById(`user`, id); user = user.Items && user.Items[0] ? user.Items[0] : null; - if (user) { - user = convertToPublicFormat(user); - } return user; }; diff --git a/api/package.json b/api/package.json index 05fc0a9..dfb5f0d 100644 --- a/api/package.json +++ b/api/package.json @@ -1,23 +1,23 @@ { - "name": "forgetmenot-api", - "version": "1.0.0", - "description": "", - "main": "app.js", - "dependencies": { - "bcryptjs": "^2.4.3", - "express": "^4.17.1", - "jsonwebtoken": "^8.5.1", - "passport": "^0.4.1", - "passport-jwt": "^4.0.0", - "shortid": "^2.2.15" - }, - "devDependencies": { - "eslint": "^7.12.1" - }, - "scripts": { - "pretest": "eslint --ignore-path ../.gitignore .", - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC" + "name": "forgetmenot-api", + "version": "1.0.0", + "description": "", + "main": "app.js", + "dependencies": { + "bcryptjs": "^2.4.3", + "express": "^4.17.1", + "jsonwebtoken": "^8.5.1", + "passport": "^0.4.1", + "passport-jwt": "^4.0.0", + "shortid": "^2.2.15" + }, + "devDependencies": { + "eslint": "^7.12.1" + }, + "scripts": { + "pretest": "eslint --ignore-path ../.gitignore .", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" } diff --git a/api/serverless.yml b/api/serverless.yml index 72c1181..18e9d56 100644 --- a/api/serverless.yml +++ b/api/serverless.yml @@ -2,17 +2,17 @@ component: express name: api inputs: - # Express application source code. - src: ./ - # Permissions required for the AWS Lambda function to interact with other resources - roleName: ${output:permissions.name} - # Enable this when you want to set a custom domain. - # domain: api.${env:domain} - # Environment variables - env: - # AWS DynamoDB Table name. Needed for the code to access it. - db: ${output:database.name} - # AWS DynamoDB Table Index name. Needed for the code to access it. - dbIndex1: ${output:database.indexes.gsi1.name} - # A secret token to sign the JWT tokens with. - tokenSecret: ${env:tokenSecret} # Change to secret via environment variable: ${env:tokenSecret} + # Express application source code. + src: ./ + # Permissions required for the AWS Lambda function to interact with other resources + roleName: ${output:permissions.name} + # Enable this when you want to set a custom domain. + # domain: api.${env:domain} + # Environment variables + env: + # AWS DynamoDB Table name. Needed for the code to access it. + db: ${output:database.name} + # AWS DynamoDB Table Index name. Needed for the code to access it. + dbIndex1: ${output:database.indexes.gsi1.name} + # A secret token to sign the JWT tokens with. + tokenSecret: ${env:tokenSecret} # Change to secret via environment variable: ${env:tokenSecret} diff --git a/database/serverless.yml b/database/serverless.yml index f9081af..1313f81 100644 --- a/database/serverless.yml +++ b/database/serverless.yml @@ -2,29 +2,29 @@ component: aws-dynamodb name: database inputs: - name: ${name}-${stage} - region: us-east-1 - # Don't delete the Database Table if "serverless remove" is run - deletionPolicy: retain - # Simple, single-table design - attributeDefinitions: - - AttributeName: hk - AttributeType: S - - AttributeName: sk - AttributeType: S - - AttributeName: sk2 - AttributeType: S - keySchema: - - AttributeName: hk - KeyType: HASH - - AttributeName: sk - KeyType: RANGE - globalSecondaryIndexes: - - IndexName: gsi1 - KeySchema: + name: ${name}-${stage} + region: us-east-1 + # Don't delete the Database Table if "serverless remove" is run + deletionPolicy: retain + # Simple, single-table design + attributeDefinitions: + - AttributeName: hk + AttributeType: S + - AttributeName: sk + AttributeType: S - AttributeName: sk2 + AttributeType: S + keySchema: + - AttributeName: hk KeyType: HASH - AttributeName: sk KeyType: RANGE - Projection: - ProjectionType: ALL + globalSecondaryIndexes: + - IndexName: gsi1 + KeySchema: + - AttributeName: sk2 + KeyType: HASH + - AttributeName: sk + KeyType: RANGE + Projection: + ProjectionType: ALL diff --git a/permissions/serverless.yml b/permissions/serverless.yml index 5229904..19f7b75 100644 --- a/permissions/serverless.yml +++ b/permissions/serverless.yml @@ -2,27 +2,27 @@ component: aws-iam-role name: permissions inputs: - name: ${name}-${stage} - region: us-east-1 - service: lambda.amazonaws.com - policy: - # AWS Lambda function containing Express Logs and Assume Role access - - Effect: Allow - Action: - - sts:AssumeRole - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Resource: "*" - # AWS DynamoDB Table access - - Effect: Allow - Action: - - dynamodb:DescribeTable - - dynamodb:Query - - dynamodb:GetItem - - dynamodb:PutItem - - dynamodb:UpdateItem - - dynamodb:DeleteItem - Resource: - - ${output:database.arn} - - ${output:database.arn}/index/* + name: ${name}-${stage} + region: us-east-1 + service: lambda.amazonaws.com + policy: + # AWS Lambda function containing Express Logs and Assume Role access + - Effect: Allow + Action: + - sts:AssumeRole + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + # AWS DynamoDB Table access + - Effect: Allow + Action: + - dynamodb:DescribeTable + - dynamodb:Query + - dynamodb:GetItem + - dynamodb:PutItem + - dynamodb:UpdateItem + - dynamodb:DeleteItem + Resource: + - ${output:database.arn} + - ${output:database.arn}/index/* diff --git a/serverless.yml b/serverless.yml index f3e2e1e..867daae 100644 --- a/serverless.yml +++ b/serverless.yml @@ -1,2 +1,2 @@ app: forget-me-not -org: benjaminramey \ No newline at end of file +org: benjaminramey diff --git a/site/package.json b/site/package.json index 9525e65..f76d0e7 100644 --- a/site/package.json +++ b/site/package.json @@ -1,37 +1,37 @@ { - "name": "serverless-fullstack-app-website", - "version": "0.1.0", - "private": true, - "dependencies": { - "@testing-library/jest-dom": "^4.2.4", - "@testing-library/react": "^9.3.2", - "@testing-library/user-event": "^7.1.2", - "react": "^16.13.1", - "react-dom": "^16.13.1", - "react-scripts": "3.4.3", - "js-cookie": "^2.2.1", - "moment": "^2.24.0", - "react-router-dom": "^5.1.2" - }, - "scripts": { - "start": "react-scripts start", - "build": "react-scripts build", - "test": "react-scripts test", - "eject": "react-scripts eject" - }, - "eslintConfig": { - "extends": "react-app" - }, - "browserslist": { - "production": [ - ">0.2%", - "not dead", - "not op_mini all" - ], - "development": [ - "last 1 chrome version", - "last 1 firefox version", - "last 1 safari version" - ] - } + "name": "serverless-fullstack-app-website", + "version": "0.1.0", + "private": true, + "dependencies": { + "@testing-library/jest-dom": "^4.2.4", + "@testing-library/react": "^9.3.2", + "@testing-library/user-event": "^7.1.2", + "react": "^16.13.1", + "react-dom": "^16.13.1", + "react-scripts": "3.4.3", + "js-cookie": "^2.2.1", + "moment": "^2.24.0", + "react-router-dom": "^5.1.2" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": "react-app" + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } } diff --git a/site/serverless.yml b/site/serverless.yml index 845044b..3b075cc 100644 --- a/site/serverless.yml +++ b/site/serverless.yml @@ -2,10 +2,10 @@ component: website name: site inputs: - # React application. "hook" runs before deployment to build the source code. "dist" is the built artifact directory which is uploaded. - src: - src: ./ - hook: npm run build - dist: build - # Enable this when you want to set a custom domain. - # domain: ${env:domain} + # React application. "hook" runs before deployment to build the source code. "dist" is the built artifact directory which is uploaded. + src: + src: ./ + hook: npm run build + dist: build + # Enable this when you want to set a custom domain. + # domain: ${env:domain}