Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate authenticate-middleware + fix logic #1

Merged
merged 1 commit into from Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.exmaple
Expand Up @@ -3,4 +3,3 @@ DATABASE_HOSTNAME=
DATABASE_PORT=
DATABASE_COLLECTION_NAME=
CORS_ALLOWED_ORIGINS=
AUTHENTICATION_ENDPOINT=
28 changes: 0 additions & 28 deletions authenticate.js

This file was deleted.

5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -5,8 +5,9 @@ const compose = require('micro-compose')
const { handleErrors } = require('micro-errors')
const cors = require('micro-cors-multiple-allow-origin')
const UrlPattern = require('url-pattern')
const authenticateMiddleware = require('@remap/authenticate-middleware')
const { Authentication } = require('@remap/services')
const { connect } = require('./db')
const authenticate = require('./authenticate')
const create = require('./routes/create')
const getList = require('./routes/getList')
const getById = require('./routes/getById')
Expand All @@ -20,7 +21,7 @@ module.exports = compose(
allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
origin: process.env.CORS_ALLOWED_ORIGINS.split(','),
}),
authenticate
authenticateMiddleware({ authenticate: Authentication.authenticate.bind(Authentication) }),
)(
router(
post('/', create),
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -26,6 +26,9 @@
},
"homepage": "https://github.com/remap-app/stocks#readme",
"dependencies": {
"@remap/authenticate-middleware": "^1.1.0",
"@remap/services": "^1.1.0",
"cross-fetch": "^2.2.2",
"dotenv": "^6.0.0",
"lodash.isnan": "^3.0.2",
"lodash.tonumber": "^4.0.3",
Expand All @@ -35,7 +38,6 @@
"micro-errors": "^2.0.0",
"microrouter": "^3.1.3",
"mongoose": "^5.2.9",
"node-fetch": "^2.2.0",
"url-pattern": "^1.0.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion routes/create.js
Expand Up @@ -10,7 +10,7 @@ module.exports = async (req, res) => {
throw createError(400, STATUS_CODES[400], null, null, { detail: '`restaurant_id` must be required' })
}

const stock = new Stock({ restaurantId, userId: req.userId })
const stock = new Stock({ restaurantId, userId: req.auth.uid })
const result = await stock.save().catch(error => {
throw createError(500, STATUS_CODES[500], error, null, {
dbErrorName: error.name,
Expand Down
2 changes: 1 addition & 1 deletion routes/deleteById.js
Expand Up @@ -11,7 +11,7 @@ module.exports = async (req, res) => {
}

const result = await Stock
.findByIdAndDelete(id)
.findOneAndDelete({ _id: id, userId: req.auth.uid })
.exec()
.catch(error => {
if (error instanceof mongoose.Error.CastError) {
Expand Down
2 changes: 1 addition & 1 deletion routes/getById.js
Expand Up @@ -10,7 +10,7 @@ module.exports = async req => {
}

const result = await Stock
.findById(id)
.findOne({ _id: id, userId: req.auth.uid })
.select('restaurantId createdAt updatedAt')
.exec()
.catch(error => {
Expand Down
2 changes: 1 addition & 1 deletion routes/getList.js
Expand Up @@ -20,7 +20,7 @@ module.exports = async req => {
}

const results = await Stock
.find({ userId: req.userId })
.find({ userId: req.auth.uid })
.sort({ createdAt: -1 })
.limit(perPage)
.skip((page - 1) * perPage)
Expand Down
18 changes: 0 additions & 18 deletions services/authentication.js

This file was deleted.

28 changes: 25 additions & 3 deletions yarn.lock
Expand Up @@ -313,6 +313,17 @@
dependencies:
arrify "^1.0.1"

"@remap/authenticate-middleware@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@remap/authenticate-middleware/-/authenticate-middleware-1.1.0.tgz#76e94638e80e1f38f4bfda1e9a279b536fa1c2b0"
dependencies:
"@remap/services" "^1.1.0"
micro-errors "^2.0.0"

"@remap/services@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@remap/services/-/services-1.1.0.tgz#8b607075c8225964cb3875c7e77e10b3068de5bb"

abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
Expand Down Expand Up @@ -998,6 +1009,13 @@ create-error-class@^3.0.0:
dependencies:
capture-stack-trace "^1.0.0"

cross-fetch@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-2.2.2.tgz#a47ff4f7fc712daba8f6a695a11c948440d45723"
dependencies:
node-fetch "2.1.2"
whatwg-fetch "2.0.4"

cross-spawn@^5.0.1:
version "5.1.0"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
Expand Down Expand Up @@ -2609,9 +2627,9 @@ nice-try@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"

node-fetch@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
node-fetch@2.1.2:
version "2.1.2"
resolved "http://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"

node-pre-gyp@^0.10.0:
version "0.10.3"
Expand Down Expand Up @@ -3832,6 +3850,10 @@ well-known-symbols@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518"

whatwg-fetch@2.0.4:
version "2.0.4"
resolved "http://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"

which-module@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
Expand Down