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

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