Created
March 2, 2017 14:25
-
-
Save brunokrebs/789765cbd9c7362667512ee7e73bd5f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// imports node modules | |
const express = require('express'); | |
const mongojs = require('mongojs'); | |
const bodyParser = require('body-parser'); | |
const jwt = require('jsonwebtoken'); | |
// creates Express app with JSON body parser | |
const app = new express(); | |
app.use(bodyParser.json()); | |
// defines REST API (HTTP methods) | |
app.get('/', getTasks); | |
app.post('/', addTask); | |
app.delete('/', deleteTask); | |
// exports REST API | |
module.exports = app; | |
function addTask(req, res) { | |
let userCollection = loadUserCollection(req.webtaskContext); | |
// save new task to user collection | |
userCollection.save({ | |
createdAt: new Date(), | |
description: req.body.description | |
}, () => res.end()) | |
} | |
function getTasks(req, res) { | |
let userCollection = loadUserCollection(req.webtaskContext); | |
// retrieves all tasks sorting by descending creation date | |
userCollection.find().sort({ createdAt: -1 }, (err, data) => { | |
res.status(err ? 500 : 200).send(err || data); | |
}); | |
} | |
function deleteTask(req, res) { | |
let userCollection = loadUserCollection(req.webtaskContext); | |
// removes a task based on its id | |
userCollection.remove({ _id: mongojs.ObjectId(req.query.id) }, () => res.end()); | |
} | |
function loadUserCollection(webtaskContext) { | |
// this secrets are configured when creating the Webtask | |
const AUTH0_SECRET = webtaskContext.secrets.AUTH0_SECRET; | |
const MONGO_USER = webtaskContext.secrets.MONGO_USER; | |
const MONGO_PASSWORD = webtaskContext.secrets.MONGO_PASSWORD; | |
const MONGO_URL = webtaskContext.secrets.MONGO_URL; | |
// removes the 'Bearer ' prefix that comes in the authorization header, | |
let authorizationHeader = webtaskContext.headers.authorization; | |
authorizationHeader = authorizationHeader.replace('Bearer ', ''); | |
// verifies token authenticity | |
let token = jwt.verify(authorizationHeader, AUTH0_SECRET); | |
// connects to MongoDB and returns the user collection | |
let mongodb = mongojs(`${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_URL}`); | |
return mongodb.collection(token.sub); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment