Fix bugs from refactor!

This commit is contained in:
2020-11-11 20:58:18 -06:00
parent 7002e78aa0
commit 0193935d2d
11 changed files with 132 additions and 139 deletions

View File

@@ -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

View File

@@ -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));
}));
};

View File

@@ -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
});

View File

@@ -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;
};

View File

@@ -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"
}

View File

@@ -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}

View File

@@ -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

View File

@@ -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/*

View File

@@ -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"
]
}
}

View File

@@ -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}