From ad87c883e1fe91a5cfcfbf233a935516756021ca Mon Sep 17 00:00:00 2001 From: Rofandi Date: Mon, 2 Mar 2020 15:45:27 +0700 Subject: [PATCH 01/15] Rofandi Gunawan --- .gitignore | 2 + config/config.json | 9 + controllers/ToDoController.js | 44 ++ index.js | 16 + migrations/20200302081007-create-to-do.js | 36 ++ models/index.js | 37 ++ models/todo.js | 13 + package-lock.json | 650 ++++++++++++++++++++++ package.json | 26 + routers/index.js | 8 + routers/taskRouter.js | 11 + 11 files changed, 852 insertions(+) create mode 100644 .gitignore create mode 100644 config/config.json create mode 100644 controllers/ToDoController.js create mode 100644 index.js create mode 100644 migrations/20200302081007-create-to-do.js create mode 100644 models/index.js create mode 100644 models/todo.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 routers/index.js create mode 100644 routers/taskRouter.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8ee8d201 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +/node_modules \ No newline at end of file diff --git a/config/config.json b/config/config.json new file mode 100644 index 00000000..c780a3df --- /dev/null +++ b/config/config.json @@ -0,0 +1,9 @@ +{ + "development": { + "username": "postgres", + "password": "postgres", + "database": "FancyToDo", + "host": "127.0.0.1", + "dialect": "postgres" + } +} diff --git a/controllers/ToDoController.js b/controllers/ToDoController.js new file mode 100644 index 00000000..29d8cf6a --- /dev/null +++ b/controllers/ToDoController.js @@ -0,0 +1,44 @@ +const models = require('../models'); +const ToDo = models.ToDo; + +class ToDoController{ + static show(req, res){ + ToDo.findAll() + .then(data => res.status(200).json(data)) + .catch(err => res.status(500)); + } + static add(req, res){ + const title = req.body.title; + const description = req.body.description; + const status = req.body.status; + const due_date = req.body.due_date; + ToDo.create({ + title: title, + description: description, + status: status, + due_date: due_date + }) + .then(data => res.status(201).json(data)) + .catch(err => res.status(404).json(err)) + } + static edit(req, res){ + const id = req.params.id; + let option = { + where: {id:id} + } + ToDo.update(req.body, option) + .then(data => res.status(201).json(data)) + .catch(err => res.status(404).json(err)) + } + static delete(req, res){ + const id = req.params.id; + let option = { + where: {id:id} + } + ToDo.destroy(option) + .then(data => res.status(201).json(data)) + .catch(err => res.status(500)) + } +} + +module.exports = ToDoController; \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 00000000..ccecc136 --- /dev/null +++ b/index.js @@ -0,0 +1,16 @@ +"use strict" + +const express = require('express') +const app = express() +const port = process.env.port || 3001 +// const morgan = require('morgan'); +const router = require('./routers') + +app.use(express.urlencoded({extended:true})) +app.use(express.json()); +app.use(router); +// app.get('/', (req, res) => res.send('Hello World!')) + +app.listen(port, () => console.log(`Example app listening on port ${port}!`)) + +module.exports = app \ No newline at end of file diff --git a/migrations/20200302081007-create-to-do.js b/migrations/20200302081007-create-to-do.js new file mode 100644 index 00000000..fe23e3bd --- /dev/null +++ b/migrations/20200302081007-create-to-do.js @@ -0,0 +1,36 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('ToDos', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + title: { + type: Sequelize.STRING + }, + description: { + type: Sequelize.STRING + }, + status: { + type: Sequelize.STRING + }, + due_date: { + type: Sequelize.DATE + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('ToDos'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 00000000..c1a3d6d5 --- /dev/null +++ b/models/index.js @@ -0,0 +1,37 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Sequelize = require('sequelize'); +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = require(__dirname + '/../config/config.json')[env]; +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs + .readdirSync(__dirname) + .filter(file => { + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); + }) + .forEach(file => { + const model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/models/todo.js b/models/todo.js new file mode 100644 index 00000000..ad1ebdc8 --- /dev/null +++ b/models/todo.js @@ -0,0 +1,13 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const ToDo = sequelize.define('ToDo', { + title: DataTypes.STRING, + description: DataTypes.STRING, + status: DataTypes.STRING, + due_date: DataTypes.DATE + }, {}); + ToDo.associate = function(models) { + // associations can be defined here + }; + return ToDo; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..cb390121 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,650 @@ +{ + "name": "fancy-todo", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/node": { + "version": "13.7.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.7.tgz", + "integrity": "sha512-Uo4chgKbnPNlxQwoFmYIwctkQVkMMmsAoGGU4JKwLuvBefF0pCq4FybNSnfkfRCpC7ZW7kttcC/TrRtAJsvGtg==" + }, + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, + "any-promise": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", + "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" + }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + } + }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "bytes": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" + }, + "cls-bluebird": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/cls-bluebird/-/cls-bluebird-2.1.0.tgz", + "integrity": "sha1-N+8eCAqP+1XC9BZPU28ZGeeWiu4=", + "requires": { + "is-bluebird": "^1.0.2", + "shimmer": "^1.1.0" + } + }, + "content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "dottie": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.2.tgz", + "integrity": "sha512-fmrwR04lsniq/uSr8yikThDTrM7epXHBAAjH9TbeH3rEA8tdCO7mRzB9hdmdGyJCxF8KERo9CITcm3kGuoyMhg==" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "requires": { + "accepts": "~1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "content-type": "~1.0.4", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.5", + "qs": "6.7.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.1.2", + "send": "0.17.1", + "serve-static": "1.14.1", + "setprototypeof": "1.1.1", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inflection": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz", + "integrity": "sha1-ogCTVlbW9fa8TcdQLhrstwMihBY=" + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "is-bluebird": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-bluebird/-/is-bluebird-1.0.2.tgz", + "integrity": "sha1-CWQ5Bg9KpBGr7hkUOoTWpVNG1uI=" + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.43.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz", + "integrity": "sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ==" + }, + "mime-types": { + "version": "2.1.26", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz", + "integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==", + "requires": { + "mime-db": "1.43.0" + } + }, + "moment": { + "version": "2.24.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", + "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" + }, + "moment-timezone": { + "version": "0.5.28", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.28.tgz", + "integrity": "sha512-TDJkZvAyKIVWg5EtVqRzU97w0Rb0YVbfpqyjgu6GwXCAohVRqwZjf4fOzDE6p1Ch98Sro/8hQQi65WDXW5STPw==", + "requires": { + "moment": ">= 2.9.0" + } + }, + "morgan": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", + "integrity": "sha512-HQStPIV4y3afTiCYVxirakhlCfGkI161c76kKFca7Fk1JusM//Qeo1ej2XaMniiNeaZklMVrh3vTtIzpzwbpmA==", + "requires": { + "basic-auth": "~2.0.0", + "debug": "2.6.9", + "depd": "~1.1.2", + "on-finished": "~2.3.0", + "on-headers": "~1.0.1" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "pg": { + "version": "7.18.2", + "resolved": "https://registry.npmjs.org/pg/-/pg-7.18.2.tgz", + "integrity": "sha512-Mvt0dGYMwvEADNKy5PMQGlzPudKcKKzJds/VbOeZJpb6f/pI3mmoXX0JksPgI3l3JPP/2Apq7F36O63J7mgveA==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "0.1.3", + "pg-packet-stream": "^1.1.0", + "pg-pool": "^2.0.10", + "pg-types": "^2.1.0", + "pgpass": "1.x", + "semver": "4.3.2" + } + }, + "pg-connection-string": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", + "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-packet-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pg-packet-stream/-/pg-packet-stream-1.1.0.tgz", + "integrity": "sha512-kRBH0tDIW/8lfnnOyTwKD23ygJ/kexQVXZs7gEyBljw4FYqimZFxnMMx50ndZ8In77QgfGuItS5LLclC2TtjYg==" + }, + "pg-pool": { + "version": "2.0.10", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.10.tgz", + "integrity": "sha512-qdwzY92bHf3nwzIUcj+zJ0Qo5lpG/YxchahxIN8+ZVmXqkahKXsnl2aiJPHLYN9o5mB/leG+Xh6XKxtP7e0sjg==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz", + "integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=", + "requires": { + "split": "^1.0.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.4.tgz", + "integrity": "sha512-bESRvKVuTrjoBluEcpv2346+6kgB7UlnqWZsnbnCccTNq/pqfj1j6oBaN5+b/NrDXepYUT/HKadqv3iS9lJuVA==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "proxy-addr": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", + "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "retry-as-promised": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/retry-as-promised/-/retry-as-promised-3.2.0.tgz", + "integrity": "sha512-CybGs60B7oYU/qSQ6kuaFmRd9sTZ6oXSc0toqePvV74Ac6/IFZSI1ReFQmtCN+uvW1Mtqdwpvt/LGOiCBAY2Mg==", + "requires": { + "any-promise": "^1.3.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "semver": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" + }, + "send": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", + "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.7.2", + "mime": "1.6.0", + "ms": "2.1.1", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, + "sequelize": { + "version": "5.21.5", + "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-5.21.5.tgz", + "integrity": "sha512-n9hR5K4uQGmBGK/Y/iqewCeSFmKVsd0TRnh0tfoLoAkmXbKC4tpeK96RhKs7d+TTMtrJlgt2TNLVBaAxEwC4iw==", + "requires": { + "bluebird": "^3.5.0", + "cls-bluebird": "^2.1.0", + "debug": "^4.1.1", + "dottie": "^2.0.0", + "inflection": "1.12.0", + "lodash": "^4.17.15", + "moment": "^2.24.0", + "moment-timezone": "^0.5.21", + "retry-as-promised": "^3.2.0", + "semver": "^6.3.0", + "sequelize-pool": "^2.3.0", + "toposort-class": "^1.0.1", + "uuid": "^3.3.3", + "validator": "^10.11.0", + "wkx": "^0.4.8" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "semver": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" + } + } + }, + "sequelize-pool": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/sequelize-pool/-/sequelize-pool-2.3.0.tgz", + "integrity": "sha512-Ibz08vnXvkZ8LJTiUOxRcj1Ckdn7qafNZ2t59jYHMX1VIebTAOYefWdRYFt6z6+hy52WGthAHAoLc9hvk3onqA==" + }, + "serve-static": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", + "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.1" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "shimmer": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.2.1.tgz", + "integrity": "sha512-sQTKC1Re/rM6XyFM6fIAGHRPVGvyXfgzIDvzoq608vM+jeyVD0Tu1E6Np0Kc2zAIFWIj963V2800iF/9LPieQw==" + }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "requires": { + "through": "2" + } + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, + "toposort-class": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz", + "integrity": "sha1-f/0feMi+KMO6Rc1OGj9e4ZO9mYg=" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "uuid": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", + "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" + }, + "validator": { + "version": "10.11.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-10.11.0.tgz", + "integrity": "sha512-X/p3UZerAIsbBfN/IwahhYaBbY68EN/UQBWHtsbXGT5bfrH/p4NQzUCG1kF/rtKaNpnJ7jAu6NGTdSNtyNIXMw==" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, + "wkx": { + "version": "0.4.8", + "resolved": "https://registry.npmjs.org/wkx/-/wkx-0.4.8.tgz", + "integrity": "sha512-ikPXMM9IR/gy/LwiOSqWlSL3X/J5uk9EO2hHNRXS41eTLXaUFEVw9fn/593jW/tE5tedNg8YjT5HkCa4FqQZyQ==", + "requires": { + "@types/node": "*" + } + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..ed5d9729 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "fancy-todo", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Rofandi/fancy-todo.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Rofandi/fancy-todo/issues" + }, + "homepage": "https://github.com/Rofandi/fancy-todo#readme", + "dependencies": { + "express": "^4.17.1", + "morgan": "^1.9.1", + "pg": "^7.18.2", + "sequelize": "^5.21.5" + } +} diff --git a/routers/index.js b/routers/index.js new file mode 100644 index 00000000..709b5fba --- /dev/null +++ b/routers/index.js @@ -0,0 +1,8 @@ +"use strict" + +const router = require('express').Router(); +const taskRouter = require('./taskRouter.js'); + +router.use('/todos', taskRouter); + +module.exports = router; \ No newline at end of file diff --git a/routers/taskRouter.js b/routers/taskRouter.js new file mode 100644 index 00000000..547981e0 --- /dev/null +++ b/routers/taskRouter.js @@ -0,0 +1,11 @@ +"use strict" + +const router = require('express').Router(); +const ToDoController = require('../controllers/ToDoController'); + +router.get('/', ToDoController.show); +router.post('/', ToDoController.add); +router.put('/:id', ToDoController.edit); +router.delete('/:id', ToDoController.delete); + +module.exports = router; \ No newline at end of file From cf142dbd070d573cda247023c5fe851237e8bfc6 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Tue, 3 Mar 2020 00:11:53 +0700 Subject: [PATCH 02/15] hari 1 belum selesai --- controllers/ToDoController.js | 76 +++++++- index.js | 19 +- middleware/ErrorHandler.js | 11 ++ models/todo.js | 44 ++++- morganChalk.js | 25 +++ package-lock.json | 324 +++++++++++++++++++++++++++++++++- package.json | 6 +- routers/taskRouter.js | 9 +- swaggerOptions.js | 25 +++ 9 files changed, 517 insertions(+), 22 deletions(-) create mode 100644 middleware/ErrorHandler.js create mode 100644 morganChalk.js create mode 100644 swaggerOptions.js diff --git a/controllers/ToDoController.js b/controllers/ToDoController.js index 29d8cf6a..2444a760 100644 --- a/controllers/ToDoController.js +++ b/controllers/ToDoController.js @@ -1,13 +1,53 @@ const models = require('../models'); const ToDo = models.ToDo; +const createError = require("http-errors"); +/** + * @swagger + * tags: + * name: ToDo + * description: ToDo management + */ class ToDoController{ - static show(req, res){ + /** + * @swagger + * path: + * /todos/: + * get: + * summary: Get all todos + * tags: [ToDo] + * responses: + * "200": + * description: Array of ToDo + * content: + * application/json: + * schema: + * type: array + * items: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + */ + static getTodos(req, res, next){ ToDo.findAll() .then(data => res.status(200).json(data)) - .catch(err => res.status(500)); + .catch(err => next(err)); } - static add(req, res){ + static getTodo(req, res, next){ + const id = req.params.id + ToDo.findByPk(id) + .then(data=> { + if (data !== null) { + res.status(200).json(data) + } else { + next({ name: "NotFound" }); + } + }) + .catch(err => { + next(err); + }); + } + static addTodo(req, res, next){ const title = req.body.title; const description = req.body.description; const status = req.body.status; @@ -19,24 +59,44 @@ class ToDoController{ due_date: due_date }) .then(data => res.status(201).json(data)) - .catch(err => res.status(404).json(err)) + .catch(err => { + //res.send(err); + next(err); + }) } - static edit(req, res){ + static updateTodo(req, res){ const id = req.params.id; + const newData = { + title: req.body.title, + description: req.body.description, + status: req.body.status, + due_date: req.body.due_date + } let option = { where: {id:id} } - ToDo.update(req.body, option) + ToDo.update(newData, option) .then(data => res.status(201).json(data)) .catch(err => res.status(404).json(err)) + + // ToDo.findByPk(id) + // .then(data => { + // data.title = req.body.title; + // data.description = req.body.description; + // data.status = req.body.status; + // data.due_date = req.body.due_date; + // return data.save(); + // }) + // .then(data => res.status(201).json(data)) + // .catch(err => res.status(404).json(err)); } - static delete(req, res){ + static deleteTodo(req, res){ const id = req.params.id; let option = { where: {id:id} } ToDo.destroy(option) - .then(data => res.status(201).json(data)) + .then(data => res.status(202).json(data)) .catch(err => res.status(500)) } } diff --git a/index.js b/index.js index ccecc136..6c1786f6 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,25 @@ const express = require('express') const app = express() -const port = process.env.port || 3001 -// const morgan = require('morgan'); -const router = require('./routers') +const port = process.env.port || 3000 +const morganChalk = require('./morganChalk'); +const router = require('./routers'); +const ErrorHandler = require('./middleware/ErrorHandler'); +app.use(morganChalk); app.use(express.urlencoded({extended:true})) app.use(express.json()); app.use(router); -// app.get('/', (req, res) => res.send('Hello World!')) +app.use(ErrorHandler); + +const swaggerJsdoc = require("swagger-jsdoc"); +const swaggerUi = require("swagger-ui-express"); +const swaggerOptions = require("./swaggerOptions"); +const specs = swaggerJsdoc(swaggerOptions); +router.use("/docs", swaggerUi.serve); +router.get("/docs", swaggerUi.setup(specs, { + explorer: true +})); app.listen(port, () => console.log(`Example app listening on port ${port}!`)) diff --git a/middleware/ErrorHandler.js b/middleware/ErrorHandler.js new file mode 100644 index 00000000..31bdfd98 --- /dev/null +++ b/middleware/ErrorHandler.js @@ -0,0 +1,11 @@ +module.exports = (err, req, res, next) => { + if (res.headersSent) { + res.json(err); + } else if (err.name === "SequelizeValidationError") { + res.status(400).json(err); + } else if (err.name === "NotFound") { + res.status(404).json(err); + } else { + res.status(500).json(err); + } +}; \ No newline at end of file diff --git a/models/todo.js b/models/todo.js index ad1ebdc8..e603bbf7 100644 --- a/models/todo.js +++ b/models/todo.js @@ -1,7 +1,49 @@ 'use strict'; +/** + * @swagger + * components: + * schemas: + * ToDo: + * type: object + * required: + * - title + * - description + * - status + * - due_date + * properties: + * title: + * type: string + * description: Title of the todo task + * description: + * type: string + * status: + * type: string + * description: Uncomplete/Complete + * due_date: + * type: Date + * createdAt: + * type: Date + * updatedAt: + * type: Date + * example: + * title: Do Laundry + * description: Use Washing Machine + * status: Uncomplete + * due_date: 2020-10-10T00:00:00.000Z + * createdAt: 2020-10-10T00:00:00.000Z + * updatedAt: 2020-10-10T00:00:00.000Z + */ module.exports = (sequelize, DataTypes) => { const ToDo = sequelize.define('ToDo', { - title: DataTypes.STRING, + title: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notNull: { + msg: "please enter ToDo title" + } + } + }, description: DataTypes.STRING, status: DataTypes.STRING, due_date: DataTypes.DATE diff --git a/morganChalk.js b/morganChalk.js new file mode 100644 index 00000000..2388ed4a --- /dev/null +++ b/morganChalk.js @@ -0,0 +1,25 @@ +const morgan = require ('morgan'); +const chalk = require ('chalk'); + +const methodColor = (method) => { + switch (method) { + case "GET": return chalk.green; + case "POST": return chalk.yellow; + case "DELETE": return chalk.red; + case "PUT": return chalk.blueBright; + case "PATCH": return chalk.purple; + default: return chalk.green; + } +} + +const morganChalk = morgan(function (tokens, req, res) { + const method = tokens.method(req, res); + return [ + methodColor(method).bold(method), + res.statusCode >= 400 ? chalk.red.bold(tokens.status(req, res)) : chalk.green.bold(tokens.status(req, res)), + chalk.white(tokens.url(req, res)), + chalk.yellow(tokens['response-time'](req, res) + ' ms'), + ].join(' '); +}); + +module.exports = morganChalk; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cb390121..32f0271a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/color-name": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", + "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" + }, "@types/node": { "version": "13.7.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.7.tgz", @@ -18,16 +23,38 @@ "negotiator": "0.6.2" } }, + "ansi-styles": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", + "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "requires": { + "@types/color-name": "^1.1.1", + "color-convert": "^2.0.1" + } + }, "any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "requires": { + "sprintf-js": "~1.0.2" + } + }, "array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, "basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -56,6 +83,29 @@ "qs": "6.7.0", "raw-body": "2.4.0", "type-is": "~1.6.17" + }, + "dependencies": { + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + } + } + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" } }, "buffer-writer": { @@ -68,6 +118,20 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, + "call-me-maybe": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/call-me-maybe/-/call-me-maybe-1.0.1.tgz", + "integrity": "sha1-JtII6onje1y95gJQoV8DHBak1ms=" + }, + "chalk": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-3.0.0.tgz", + "integrity": "sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, "cls-bluebird": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/cls-bluebird/-/cls-bluebird-2.1.0.tgz", @@ -77,6 +141,29 @@ "shimmer": "^1.1.0" } }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "commander": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.0.1.tgz", + "integrity": "sha512-IPF4ouhCP+qdlcmCedhxX4xiGBPyigb8v5NeUp+0LyhwLgxMqyp3S0vl7TAPfS/hiP7FC3caI/PB9lTmP8r1NA==" + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, "content-disposition": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", @@ -118,6 +205,14 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "doctrine": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", + "integrity": "sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==", + "requires": { + "esutils": "^2.0.2" + } + }, "dottie": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.2.tgz", @@ -138,6 +233,16 @@ "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" }, + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" + }, + "esutils": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==" + }, "etag": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", @@ -204,16 +309,46 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, "http-errors": { - "version": "1.7.2", - "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", - "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.3.tgz", + "integrity": "sha512-ZTTX0MWrsQ2ZAhA1cejAwDLycFsd7I7nVtnkT3Ol0aqodaKW+0CTZDQ1uBv5whptCnc8e8HeRRJxRs0kmm/Qfw==", "requires": { "depd": "~1.1.2", - "inherits": "2.0.3", + "inherits": "2.0.4", "setprototypeof": "1.1.1", "statuses": ">= 1.5.0 < 2", "toidentifier": "1.0.0" + }, + "dependencies": { + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + } } }, "iconv-lite": { @@ -229,6 +364,15 @@ "resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz", "integrity": "sha1-ogCTVlbW9fa8TcdQLhrstwMihBY=" }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", @@ -244,11 +388,47 @@ "resolved": "https://registry.npmjs.org/is-bluebird/-/is-bluebird-1.0.2.tgz", "integrity": "sha1-CWQ5Bg9KpBGr7hkUOoTWpVNG1uI=" }, + "js-yaml": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, + "json-schema-ref-parser": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/json-schema-ref-parser/-/json-schema-ref-parser-7.1.3.tgz", + "integrity": "sha512-/Lmyl0PW27dOmCO03PI339+1gs4Z2PlqIyUgzIOtoRp08zkkMCB30TRbdppbPO7WWzZX0uT98HqkDiZSujkmbA==", + "requires": { + "call-me-maybe": "^1.0.1", + "js-yaml": "^3.13.1", + "ono": "^6.0.0" + }, + "dependencies": { + "ono": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/ono/-/ono-6.0.1.tgz", + "integrity": "sha512-5rdYW/106kHqLeG22GE2MHKq+FlsxMERZev9DCzQX1zwkxnFwBivSn5i17a5O/rDmOJOdf4Wyt80UZljzx9+DA==" + } + } + }, "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + }, + "lodash.isequal": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", + "integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA=" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -282,6 +462,14 @@ "mime-db": "1.43.0" } }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "^1.1.7" + } + }, "moment": { "version": "2.24.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", @@ -330,6 +518,29 @@ "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1" + } + }, + "ono": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/ono/-/ono-5.1.0.tgz", + "integrity": "sha512-GgqRIUWErLX4l9Up0khRtbrlH8Fyj59A0nKv8V6pWEto38aUgnOGOOF7UmgFFLzFnDSc8REzaTXOc0hqEe7yIw==" + }, + "openapi-schemas": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/openapi-schemas/-/openapi-schemas-1.0.3.tgz", + "integrity": "sha512-KtMWcK2VtOS+nD8RKSIyScJsj8JrmVWcIX7Kjx4xEHijFYuvMTDON8WfeKOgeSb4uNG6UsqLj5Na7nKbSav9RQ==" + }, + "openapi-types": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-1.3.5.tgz", + "integrity": "sha512-11oi4zYorsgvg5yBarZplAqbpev5HkuVNPlZaPTknPDzAynq+lnJdXAmruGWP0s+dNYZS7bjM+xrTpJw7184Fg==" + }, "packet-reader": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", @@ -340,6 +551,11 @@ "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, "path-to-regexp": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", @@ -451,6 +667,20 @@ "http-errors": "1.7.2", "iconv-lite": "0.4.24", "unpipe": "1.0.0" + }, + "dependencies": { + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + } } }, "retry-as-promised": { @@ -579,11 +809,68 @@ "through": "2" } }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + }, "statuses": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, + "supports-color": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", + "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", + "requires": { + "has-flag": "^4.0.0" + } + }, + "swagger-jsdoc": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/swagger-jsdoc/-/swagger-jsdoc-3.5.0.tgz", + "integrity": "sha512-TSmCgPodvVGGIXEU+zDp03Gau4pZxgVXiZMIjOgBQHEsRzPLoo2XvF9oQ8Nf1Wf29qsXebLb6qqlo6CIwCQ0Cw==", + "requires": { + "commander": "4.0.1", + "doctrine": "3.0.0", + "glob": "7.1.6", + "js-yaml": "3.13.1", + "swagger-parser": "8.0.3" + } + }, + "swagger-methods": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/swagger-methods/-/swagger-methods-2.0.2.tgz", + "integrity": "sha512-/RNqvBZkH8+3S/FqBPejHxJxZenaYq3MrpeXnzi06aDIS39Mqf5YCUNb/ZBjsvFFt8h9FxfKs8EXPtcYdfLiRg==" + }, + "swagger-parser": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/swagger-parser/-/swagger-parser-8.0.3.tgz", + "integrity": "sha512-y2gw+rTjn7Z9J+J1qwbBm0UL93k/VREDCveKBK6iGjf7KXC6QGshbnpEmeHL0ZkCgmIghsXzpNzPSbBH91BAEQ==", + "requires": { + "call-me-maybe": "^1.0.1", + "json-schema-ref-parser": "^7.1.1", + "ono": "^5.1.0", + "openapi-schemas": "^1.0.2", + "openapi-types": "^1.3.5", + "swagger-methods": "^2.0.1", + "z-schema": "^4.1.1" + } + }, + "swagger-ui-dist": { + "version": "3.25.0", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.25.0.tgz", + "integrity": "sha512-vwvJPPbdooTvDwLGzjIXinOXizDJJ6U1hxnJL3y6U3aL1d2MSXDmKg2139XaLBhsVZdnQJV2bOkX4reB+RXamg==" + }, + "swagger-ui-express": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.3.tgz", + "integrity": "sha512-f8SEn4YWkKh/HGK0ZjuA2VqA78i1aY6OIa5cqYNgOkBobfHV6Mz4dphQW/us8HYhEFfbENq329PyfIonWfzFrw==", + "requires": { + "swagger-ui-dist": "^3.18.1" + } + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", @@ -641,10 +928,39 @@ "@types/node": "*" } }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + }, + "z-schema": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/z-schema/-/z-schema-4.2.2.tgz", + "integrity": "sha512-7bGR7LohxSdlK1EOdvA/OHksvKGE4jTLSjd8dBj9YKT0S43N9pdMZ0Z7GZt9mHrBFhbNTRh3Ky6Eu2MHsPJe8g==", + "requires": { + "commander": "^2.7.1", + "lodash.get": "^4.4.2", + "lodash.isequal": "^4.5.0", + "validator": "^11.0.0" + }, + "dependencies": { + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "optional": true + }, + "validator": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-11.1.0.tgz", + "integrity": "sha512-qiQ5ktdO7CD6C/5/mYV4jku/7qnqzjrxb3C/Q5wR3vGGinHTgJZN/TdFT3ZX4vXhX2R1PXx42fB1cn5W+uJ4lg==" + } + } } } } diff --git a/package.json b/package.json index ed5d9729..fbe149ae 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,13 @@ }, "homepage": "https://github.com/Rofandi/fancy-todo#readme", "dependencies": { + "chalk": "^3.0.0", "express": "^4.17.1", + "http-errors": "^1.7.3", "morgan": "^1.9.1", "pg": "^7.18.2", - "sequelize": "^5.21.5" + "sequelize": "^5.21.5", + "swagger-jsdoc": "^3.5.0", + "swagger-ui-express": "^4.1.3" } } diff --git a/routers/taskRouter.js b/routers/taskRouter.js index 547981e0..4f839534 100644 --- a/routers/taskRouter.js +++ b/routers/taskRouter.js @@ -3,9 +3,10 @@ const router = require('express').Router(); const ToDoController = require('../controllers/ToDoController'); -router.get('/', ToDoController.show); -router.post('/', ToDoController.add); -router.put('/:id', ToDoController.edit); -router.delete('/:id', ToDoController.delete); +router.get('/', ToDoController.getTodos); +router.post('/', ToDoController.addTodo); +router.get('/:id', ToDoController.getTodo); +router.put('/:id', ToDoController.updateTodo); +router.delete('/:id', ToDoController.deleteTodo); module.exports = router; \ No newline at end of file diff --git a/swaggerOptions.js b/swaggerOptions.js new file mode 100644 index 00000000..464cd6f2 --- /dev/null +++ b/swaggerOptions.js @@ -0,0 +1,25 @@ +module.exports = { + swaggerDefinition: { + openapi: "3.0.0", + info: { + title: "Fancy Todo API Documentation", + version: "1.0.0", + description: "JSON API for Fancy Todo app", + license: { + name: "MIT" + }, + contact: { + name: "Rofandi" + } + }, + servers: [ + { + url: "http://localhost:3000/" + } + ] + }, + apis: [ + "./models/todo.js", + "./controllers/ToDoController.js" + ] +}; \ No newline at end of file From 9fce4f5a0c6163983d52acfcb303d2b7ea6d46a5 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Tue, 3 Mar 2020 16:30:51 +0700 Subject: [PATCH 03/15] register login --- .gitignore | 3 +- config/config.js | 11 + config/config.json | 9 - controllers/ToDoController.js | 41 +- controllers/UserController.js | 38 ++ index.js | 2 + middleware/Authentication.js | 9 + middleware/Authorization.js | 0 migrations/20200303074905-create-user.js | 30 ++ models/index.js | 2 +- models/user.js | 15 + package-lock.json | 502 +++++++++++++++++++++++ package.json | 3 + 13 files changed, 635 insertions(+), 30 deletions(-) create mode 100644 config/config.js delete mode 100644 config/config.json create mode 100644 controllers/UserController.js create mode 100644 middleware/Authentication.js create mode 100644 middleware/Authorization.js create mode 100644 migrations/20200303074905-create-user.js create mode 100644 models/user.js diff --git a/.gitignore b/.gitignore index 8ee8d201..69c566b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -/node_modules \ No newline at end of file +/node_modules +.env \ No newline at end of file diff --git a/config/config.js b/config/config.js new file mode 100644 index 00000000..f2a9de8c --- /dev/null +++ b/config/config.js @@ -0,0 +1,11 @@ +require('dotenv').config(); + +module.exports = { + "development": { + "username": process.env.DB_USER, + "password": process.env.DB_PASS, + "database": "FancyToDo", + "host": "127.0.0.1", + "dialect": "postgres" + } +} diff --git a/config/config.json b/config/config.json deleted file mode 100644 index c780a3df..00000000 --- a/config/config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "development": { - "username": "postgres", - "password": "postgres", - "database": "FancyToDo", - "host": "127.0.0.1", - "dialect": "postgres" - } -} diff --git a/controllers/ToDoController.js b/controllers/ToDoController.js index 2444a760..72f07ef9 100644 --- a/controllers/ToDoController.js +++ b/controllers/ToDoController.js @@ -34,18 +34,16 @@ class ToDoController{ .catch(err => next(err)); } static getTodo(req, res, next){ - const id = req.params.id + const id = req.params.id; ToDo.findByPk(id) .then(data=> { if (data !== null) { - res.status(200).json(data) + res.status(200).json(data); } else { next({ name: "NotFound" }); } }) - .catch(err => { - next(err); - }); + .catch(err => next(err)); } static addTodo(req, res, next){ const title = req.body.title; @@ -59,10 +57,7 @@ class ToDoController{ due_date: due_date }) .then(data => res.status(201).json(data)) - .catch(err => { - //res.send(err); - next(err); - }) + .catch(err => next(err)); } static updateTodo(req, res){ const id = req.params.id; @@ -72,12 +67,16 @@ class ToDoController{ status: req.body.status, due_date: req.body.due_date } - let option = { - where: {id:id} - } + let option = {where: {id:id}}; ToDo.update(newData, option) - .then(data => res.status(201).json(data)) - .catch(err => res.status(404).json(err)) + .then(data => { + if (data !== null && data!=false) { + res.status(200).json(data); + } else { + next({ name: "NotFound" }); + } + }) + .catch(err => next(err)); // ToDo.findByPk(id) // .then(data => { @@ -92,12 +91,16 @@ class ToDoController{ } static deleteTodo(req, res){ const id = req.params.id; - let option = { - where: {id:id} - } + let option = { where: {id:id}}; ToDo.destroy(option) - .then(data => res.status(202).json(data)) - .catch(err => res.status(500)) + .then(data => { + if (data !== null) { + res.status(200).json(data); + } else { + next({ name: "NotFound" }); + } + }) + .catch(err => next(err)); } } diff --git a/controllers/UserController.js b/controllers/UserController.js new file mode 100644 index 00000000..33985093 --- /dev/null +++ b/controllers/UserController.js @@ -0,0 +1,38 @@ +const model = require('../models'); +const User = model.User; +const bcrypt = require('bcrypt'); + +class UserController{ + static register(req, res, next){ + const email = req.body.email; + const password = req.body.password; + User.create({ + email: email, + password: password + }) + .then(data => res.status(301).json(data)) + .catch(error => next(error)); + } + static login(req, res, next){ + const email = req.body.email; + const password = req.body.password; + const salt = bcrypt.genSaltSync(10); + const hash = bcrypt.hashSync(password, salt); + User.findOne({ where: {email}}) + .then(data=>{ + if(data){ + if(bcrypt.compareSync(data.password, hash)){ + let token = jwt.sign({email: User.email}, process.env.JWT_SECRET); + res.status(200).json({token}) + }else{ + next(err); + } + } + else{ + next(err); + } + }) + } +} + +module.exports = UserController; \ No newline at end of file diff --git a/index.js b/index.js index 6c1786f6..109aac69 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ const port = process.env.port || 3000 const morganChalk = require('./morganChalk'); const router = require('./routers'); const ErrorHandler = require('./middleware/ErrorHandler'); +const dotenv = require('dotenv'); +dotenv.config(); app.use(morganChalk); app.use(express.urlencoded({extended:true})) diff --git a/middleware/Authentication.js b/middleware/Authentication.js new file mode 100644 index 00000000..961c29e9 --- /dev/null +++ b/middleware/Authentication.js @@ -0,0 +1,9 @@ +const JWT = require('jsonwebtoken') + +function authenticationUser(req, res, next){ + const token = req.headers + try{ + let decoded = JWT.verify(token, process.env.JWT_SECRET) + next(); + } +} \ No newline at end of file diff --git a/middleware/Authorization.js b/middleware/Authorization.js new file mode 100644 index 00000000..e69de29b diff --git a/migrations/20200303074905-create-user.js b/migrations/20200303074905-create-user.js new file mode 100644 index 00000000..249fa5a0 --- /dev/null +++ b/migrations/20200303074905-create-user.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('Users', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + email: { + type: Sequelize.STRING + }, + password: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('Users'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js index c1a3d6d5..c7cc49e7 100644 --- a/models/index.js +++ b/models/index.js @@ -5,7 +5,7 @@ const path = require('path'); const Sequelize = require('sequelize'); const basename = path.basename(__filename); const env = process.env.NODE_ENV || 'development'; -const config = require(__dirname + '/../config/config.json')[env]; +const config = require(__dirname + '/../config/config.js')[env]; const db = {}; let sequelize; diff --git a/models/user.js b/models/user.js new file mode 100644 index 00000000..23d883ca --- /dev/null +++ b/models/user.js @@ -0,0 +1,15 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const User = sequelize.define('User', { + email: DataTypes.STRING, + password: DataTypes.STRING + }, {}); + User.addHook('beforeCreate', (user, options)=>{ + const salt = bcrypt.genSaltSync(10); + const hash = bcrypt.hashSync(user.password, salt); + user.password = hash; + }) + User.associate = function(models) { + }; + return User; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 32f0271a..5399d604 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,11 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.7.tgz", "integrity": "sha512-Uo4chgKbnPNlxQwoFmYIwctkQVkMMmsAoGGU4JKwLuvBefF0pCq4FybNSnfkfRCpC7ZW7kttcC/TrRtAJsvGtg==" }, + "abbrev": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", + "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" + }, "accepts": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", @@ -23,6 +28,11 @@ "negotiator": "0.6.2" } }, + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, "ansi-styles": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", @@ -37,6 +47,20 @@ "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "aproba": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/aproba/-/aproba-1.2.0.tgz", + "integrity": "sha512-Y9J6ZjXtoYh8RnXVCMOU/ttDmk1aBjunq9vO0ta5x85WDQiQfUF9sIPBITdbiiIVcBo03Hi3jMxigBtsddlXRw==" + }, + "are-we-there-yet": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-1.1.5.tgz", + "integrity": "sha512-5hYdAkZlcG8tOLujVDTgCT+uPX0VnpAH28gWsLfzpXYm7wP6mp5Q/gYyR7YQ0cKVJcXJnl3j2kpBan13PtQf6w==", + "requires": { + "delegates": "^1.0.0", + "readable-stream": "^2.0.6" + } + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", @@ -63,6 +87,15 @@ "safe-buffer": "5.1.2" } }, + "bcrypt": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-4.0.1.tgz", + "integrity": "sha512-hSIZHkUxIDS5zA2o00Kf2O5RfVbQ888n54xQoF/eIaquU4uaLxK8vhhBdktd0B3n2MjkcAWzv4mnhogykBKOUQ==", + "requires": { + "node-addon-api": "^2.0.0", + "node-pre-gyp": "0.14.0" + } + }, "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -108,6 +141,11 @@ "concat-map": "0.0.1" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "buffer-writer": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", @@ -132,6 +170,11 @@ "supports-color": "^7.1.0" } }, + "chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" + }, "cls-bluebird": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/cls-bluebird/-/cls-bluebird-2.1.0.tgz", @@ -141,6 +184,11 @@ "shimmer": "^1.1.0" } }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" + }, "color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -164,6 +212,11 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, + "console-control-strings": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", + "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=" + }, "content-disposition": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", @@ -187,6 +240,11 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -195,6 +253,16 @@ "ms": "2.0.0" } }, + "deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==" + }, + "delegates": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", + "integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -205,6 +273,11 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "detect-libc": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", + "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=" + }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -213,11 +286,24 @@ "esutils": "^2.0.2" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "dottie": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.2.tgz", "integrity": "sha512-fmrwR04lsniq/uSr8yikThDTrM7epXHBAAjH9TbeH3rEA8tdCO7mRzB9hdmdGyJCxF8KERo9CITcm3kGuoyMhg==" }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -309,11 +395,34 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "fs-minipass": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", + "integrity": "sha512-GWSSJGFy4e9GUeCcbIkED+bgAoFyj7XF1mV8rma3QW4NIqX9Kyx79N/PF61H5udOV3aY1IaMLs6pGbH71nlCTA==", + "requires": { + "minipass": "^2.6.0" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "gauge": { + "version": "2.7.4", + "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", + "integrity": "sha1-LANAXHU4w51+s3sxcCLjJfsBi/c=", + "requires": { + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" + } + }, "glob": { "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", @@ -332,6 +441,11 @@ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" }, + "has-unicode": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", + "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" + }, "http-errors": { "version": "1.7.3", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.3.tgz", @@ -359,6 +473,14 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "ignore-walk": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/ignore-walk/-/ignore-walk-3.0.3.tgz", + "integrity": "sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==", + "requires": { + "minimatch": "^3.0.4" + } + }, "inflection": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz", @@ -378,6 +500,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, + "ini": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" + }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -388,6 +515,19 @@ "resolved": "https://registry.npmjs.org/is-bluebird/-/is-bluebird-1.0.2.tgz", "integrity": "sha1-CWQ5Bg9KpBGr7hkUOoTWpVNG1uI=" }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, "js-yaml": { "version": "3.13.1", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", @@ -414,6 +554,54 @@ } } }, + "jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^5.6.0" + }, + "dependencies": { + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + } + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", @@ -424,11 +612,46 @@ "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=" + }, "lodash.isequal": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", "integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA=" }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -470,6 +693,36 @@ "brace-expansion": "^1.1.7" } }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "minipass": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.9.0.tgz", + "integrity": "sha512-wxfUjg9WebH+CUDX/CdbRlh5SmfZiy/hpkxaRI16Y9W56Pa75sWgd/rvFilSgrauD9NyFymP/+JFV3KwzIsJeg==", + "requires": { + "safe-buffer": "^5.1.2", + "yallist": "^3.0.0" + } + }, + "minizlib": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.3.3.tgz", + "integrity": "sha512-6ZYMOEnmVsdCeTJVE0W9ZD+pVnE8h9Hma/iOwwRDsdQoePpoX56/8B6z3P9VNwppJuBKNRuFDRNRqRWexT9G9Q==", + "requires": { + "minipass": "^2.9.0" + } + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, "moment": { "version": "2.24.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", @@ -500,11 +753,118 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "needle": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/needle/-/needle-2.3.3.tgz", + "integrity": "sha512-EkY0GeSq87rWp1hoq/sH/wnTWgFVhYlnIkbJ0YJFfRgEFlz2RraCjBpFQ+vrEgEdp0ThfyHADmkChEhcb7PKyw==", + "requires": { + "debug": "^3.2.6", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" + }, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "node-addon-api": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz", + "integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==" + }, + "node-pre-gyp": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz", + "integrity": "sha512-+CvDC7ZttU/sSt9rFjix/P05iS43qHCOOGzcr3Ry99bXG7VX953+vFyEuph/tfqoYu8dttBkE86JSKBO2OzcxA==", + "requires": { + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.1", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.2.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4.4.2" + }, + "dependencies": { + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + } + } + }, + "nopt": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.1.tgz", + "integrity": "sha1-0NRoWv1UFRk8jHUFYC0NF81kR00=", + "requires": { + "abbrev": "1", + "osenv": "^0.1.4" + } + }, + "npm-bundled": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/npm-bundled/-/npm-bundled-1.1.1.tgz", + "integrity": "sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA==", + "requires": { + "npm-normalize-package-bin": "^1.0.1" + } + }, + "npm-normalize-package-bin": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz", + "integrity": "sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA==" + }, + "npm-packlist": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.4.8.tgz", + "integrity": "sha512-5+AZgwru5IevF5ZdnFglB5wNlHG1AOOuw28WhUq8/8emhBmLv6jX5by4WJCh7lW0uSYZYS6DXqIsyZVIXRZU9A==", + "requires": { + "ignore-walk": "^3.0.1", + "npm-bundled": "^1.0.1", + "npm-normalize-package-bin": "^1.0.1" + } + }, + "npmlog": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-4.1.2.tgz", + "integrity": "sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==", + "requires": { + "are-we-there-yet": "~1.1.2", + "console-control-strings": "~1.1.0", + "gauge": "~2.7.3", + "set-blocking": "~2.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -541,6 +901,25 @@ "resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-1.3.5.tgz", "integrity": "sha512-11oi4zYorsgvg5yBarZplAqbpev5HkuVNPlZaPTknPDzAynq+lnJdXAmruGWP0s+dNYZS7bjM+xrTpJw7184Fg==" }, + "os-homedir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=" + }, + "os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" + }, + "osenv": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/osenv/-/osenv-0.1.5.tgz", + "integrity": "sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==", + "requires": { + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" + } + }, "packet-reader": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", @@ -639,6 +1018,11 @@ "xtend": "^4.0.0" } }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", @@ -683,6 +1067,38 @@ } } }, + "rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "requires": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + } + } + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, "retry-as-promised": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/retry-as-promised/-/retry-as-promised-3.2.0.tgz", @@ -691,6 +1107,14 @@ "any-promise": "^1.3.0" } }, + "rimraf": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", + "requires": { + "glob": "^7.1.3" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -701,6 +1125,11 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, "semver": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", @@ -791,6 +1220,11 @@ "send": "0.17.1" } }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" + }, "setprototypeof": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", @@ -801,6 +1235,11 @@ "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.2.1.tgz", "integrity": "sha512-sQTKC1Re/rM6XyFM6fIAGHRPVGvyXfgzIDvzoq608vM+jeyVD0Tu1E6Np0Kc2zAIFWIj963V2800iF/9LPieQw==" }, + "signal-exit": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", + "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" + }, "split": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", @@ -819,6 +1258,37 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" + }, "supports-color": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", @@ -871,6 +1341,20 @@ "swagger-ui-dist": "^3.18.1" } }, + "tar": { + "version": "4.4.13", + "resolved": "https://registry.npmjs.org/tar/-/tar-4.4.13.tgz", + "integrity": "sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==", + "requires": { + "chownr": "^1.1.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.8.6", + "minizlib": "^1.2.1", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.2", + "yallist": "^3.0.3" + } + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", @@ -900,6 +1384,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -920,6 +1409,14 @@ "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "requires": { + "string-width": "^1.0.2 || 2" + } + }, "wkx": { "version": "0.4.8", "resolved": "https://registry.npmjs.org/wkx/-/wkx-0.4.8.tgz", @@ -938,6 +1435,11 @@ "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" }, + "yallist": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", + "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" + }, "z-schema": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/z-schema/-/z-schema-4.2.2.tgz", diff --git a/package.json b/package.json index fbe149ae..112fb0b2 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,12 @@ }, "homepage": "https://github.com/Rofandi/fancy-todo#readme", "dependencies": { + "bcrypt": "^4.0.1", "chalk": "^3.0.0", + "dotenv": "^8.2.0", "express": "^4.17.1", "http-errors": "^1.7.3", + "jsonwebtoken": "^8.5.1", "morgan": "^1.9.1", "pg": "^7.18.2", "sequelize": "^5.21.5", From 790e54bbcf88feb706052e8b0a4cca8a602ca3fc Mon Sep 17 00:00:00 2001 From: Rofandi Date: Tue, 3 Mar 2020 22:05:01 +0700 Subject: [PATCH 04/15] bcrypt fix --- bcrypttest.js | 7 +++++++ controllers/UserController.js | 10 ++++------ helpers/bcrypt.js | 12 ++++++++++++ models/user.js | 8 ++++---- 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 bcrypttest.js create mode 100644 helpers/bcrypt.js diff --git a/bcrypttest.js b/bcrypttest.js new file mode 100644 index 00000000..42804ac6 --- /dev/null +++ b/bcrypttest.js @@ -0,0 +1,7 @@ +const Bcrypt = require("./helpers/bcrypt"); + +const password = "marsupilami"; + +const hashed = Bcrypt.hash(password); + +console.log(hashed, Bcrypt.compare(password, hashed)); \ No newline at end of file diff --git a/controllers/UserController.js b/controllers/UserController.js index 33985093..dc8c8d26 100644 --- a/controllers/UserController.js +++ b/controllers/UserController.js @@ -1,6 +1,6 @@ const model = require('../models'); const User = model.User; -const bcrypt = require('bcrypt'); +const Bcrypt = require('../helpers/bcrypt.js'); class UserController{ static register(req, res, next){ @@ -10,18 +10,16 @@ class UserController{ email: email, password: password }) - .then(data => res.status(301).json(data)) - .catch(error => next(error)); + .then(data => res.status(201).json(data)) + .catch(next); } static login(req, res, next){ const email = req.body.email; const password = req.body.password; - const salt = bcrypt.genSaltSync(10); - const hash = bcrypt.hashSync(password, salt); User.findOne({ where: {email}}) .then(data=>{ if(data){ - if(bcrypt.compareSync(data.password, hash)){ + if(Bcrypt.compareSync(password, data.password)){ let token = jwt.sign({email: User.email}, process.env.JWT_SECRET); res.status(200).json({token}) }else{ diff --git a/helpers/bcrypt.js b/helpers/bcrypt.js new file mode 100644 index 00000000..e675b1bc --- /dev/null +++ b/helpers/bcrypt.js @@ -0,0 +1,12 @@ +const bcrypt = require('bcrypt'); +class Bcrypt{ + static hash(data){ + const salt = bcrypt.genSaltSync(10); + return bcrypt.hashSync(data, salt); + } + static compare(plain, hashed){ + return bcrypt.compareSync(plain, hashed); + } +} + +module.exports = Bcrypt; \ No newline at end of file diff --git a/models/user.js b/models/user.js index 23d883ca..ae321ac2 100644 --- a/models/user.js +++ b/models/user.js @@ -1,13 +1,13 @@ 'use strict'; +const Bcrypt = require('../helpers/bcrypt.js') + module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { email: DataTypes.STRING, password: DataTypes.STRING }, {}); - User.addHook('beforeCreate', (user, options)=>{ - const salt = bcrypt.genSaltSync(10); - const hash = bcrypt.hashSync(user.password, salt); - user.password = hash; + User.addHook('beforeCreate', (user)=>{ + user.password = Bcrypt.hash(user.password); }) User.associate = function(models) { }; From 4c213bb36d316eb3006b9bf75328fbc74274b0fe Mon Sep 17 00:00:00 2001 From: Rofandi Date: Wed, 4 Mar 2020 00:18:32 +0700 Subject: [PATCH 05/15] authentication authorization --- controllers/ToDoController.js | 23 +++++++++++++++---- controllers/UserController.js | 6 ++++- middleware/Authentication.js | 19 +++++++++------ middleware/Authorization.js | 19 +++++++++++++++ ...o-do.js => 20200303081007-create-to-do.js} | 9 ++++++++ models/todo.js | 1 + models/user.js | 1 + routers/index.js | 6 ++++- routers/userRouter.js | 9 ++++++++ 9 files changed, 79 insertions(+), 14 deletions(-) rename migrations/{20200302081007-create-to-do.js => 20200303081007-create-to-do.js} (80%) create mode 100644 routers/userRouter.js diff --git a/controllers/ToDoController.js b/controllers/ToDoController.js index 72f07ef9..7ab787cb 100644 --- a/controllers/ToDoController.js +++ b/controllers/ToDoController.js @@ -29,13 +29,22 @@ class ToDoController{ * description: Internal Server Error */ static getTodos(req, res, next){ - ToDo.findAll() + const user = req.user; + ToDo.findAll({ + where: { UserId: user.id } + }) .then(data => res.status(200).json(data)) .catch(err => next(err)); } static getTodo(req, res, next){ const id = req.params.id; - ToDo.findByPk(id) + const user = req.user; + ToDo.findOne({ + where: { + id: id, + UserId: user.id + } + }) .then(data=> { if (data !== null) { res.status(200).json(data); @@ -50,24 +59,27 @@ class ToDoController{ const description = req.body.description; const status = req.body.status; const due_date = req.body.due_date; + const user = req.user; ToDo.create({ title: title, description: description, status: status, - due_date: due_date + due_date: due_date, + UserId: user.id }) .then(data => res.status(201).json(data)) .catch(err => next(err)); } static updateTodo(req, res){ const id = req.params.id; + const user = req.user; const newData = { title: req.body.title, description: req.body.description, status: req.body.status, due_date: req.body.due_date } - let option = {where: {id:id}}; + let option = {where: {id:id, UserId: user.id }}; ToDo.update(newData, option) .then(data => { if (data !== null && data!=false) { @@ -91,7 +103,8 @@ class ToDoController{ } static deleteTodo(req, res){ const id = req.params.id; - let option = { where: {id:id}}; + const user = req.user; + let option = { where: {id:id, UserId: user.id}}; ToDo.destroy(option) .then(data => { if (data !== null) { diff --git a/controllers/UserController.js b/controllers/UserController.js index dc8c8d26..06986fc9 100644 --- a/controllers/UserController.js +++ b/controllers/UserController.js @@ -1,6 +1,7 @@ const model = require('../models'); const User = model.User; const Bcrypt = require('../helpers/bcrypt.js'); +const jwt = require("jsonwebtoken"); class UserController{ static register(req, res, next){ @@ -10,7 +11,10 @@ class UserController{ email: email, password: password }) - .then(data => res.status(201).json(data)) + .then(data => { + let token = jwt.sign({email: data.email}, process.env.JWT_SECRET); + res.status(200).json({token}) + }) .catch(next); } static login(req, res, next){ diff --git a/middleware/Authentication.js b/middleware/Authentication.js index 961c29e9..31ba43eb 100644 --- a/middleware/Authentication.js +++ b/middleware/Authentication.js @@ -1,9 +1,14 @@ -const JWT = require('jsonwebtoken') +const jwt = require('jsonwebtoken'); -function authenticationUser(req, res, next){ - const token = req.headers - try{ - let decoded = JWT.verify(token, process.env.JWT_SECRET) +module.exports = (req, res, next) => { + try { + const token = req.headers.authorization; + const decodedToken = jwt.verify(token, process.env.JWT_SECRET); + req.jwt = decodedToken; next(); - } -} \ No newline at end of file + } catch { + res.status(401).json({ + error: new Error('Invalid request!') + }); + } +}; \ No newline at end of file diff --git a/middleware/Authorization.js b/middleware/Authorization.js index e69de29b..f3c03d3e 100644 --- a/middleware/Authorization.js +++ b/middleware/Authorization.js @@ -0,0 +1,19 @@ +const models = require('../models'); +const User = models.User; + +module.exports = (req, res, next) => { + const email = req.jwt.email; + User.findAll({ + where: { email: email } + }).then(users => { + if (users.length === 0) { + res.status(403).end(); + } else { + const user = users[0]; + req.user = user; + next(); + } + }).catch(err => { + res.status(500).end(); + }); +}; \ No newline at end of file diff --git a/migrations/20200302081007-create-to-do.js b/migrations/20200303081007-create-to-do.js similarity index 80% rename from migrations/20200302081007-create-to-do.js rename to migrations/20200303081007-create-to-do.js index fe23e3bd..2f10a8d5 100644 --- a/migrations/20200302081007-create-to-do.js +++ b/migrations/20200303081007-create-to-do.js @@ -20,6 +20,15 @@ module.exports = { due_date: { type: Sequelize.DATE }, + UserId: { + type: Sequelize.INTEGER, + allowNull: false, + foreignKey: true, + references: { + model: "Users", + key: "id" + } + }, createdAt: { allowNull: false, type: Sequelize.DATE diff --git a/models/todo.js b/models/todo.js index e603bbf7..fdcc617f 100644 --- a/models/todo.js +++ b/models/todo.js @@ -50,6 +50,7 @@ module.exports = (sequelize, DataTypes) => { }, {}); ToDo.associate = function(models) { // associations can be defined here + ToDo.belongsTo(models.User); }; return ToDo; }; \ No newline at end of file diff --git a/models/user.js b/models/user.js index ae321ac2..175f97e3 100644 --- a/models/user.js +++ b/models/user.js @@ -10,6 +10,7 @@ module.exports = (sequelize, DataTypes) => { user.password = Bcrypt.hash(user.password); }) User.associate = function(models) { + User.hasMany(models.ToDo); }; return User; }; \ No newline at end of file diff --git a/routers/index.js b/routers/index.js index 709b5fba..5827f994 100644 --- a/routers/index.js +++ b/routers/index.js @@ -2,7 +2,11 @@ const router = require('express').Router(); const taskRouter = require('./taskRouter.js'); +const userRouter = require('./userRouter.js'); +const Authentication = require("../middleware/Authentication.js"); +const Authorization = require("../middleware/Authorization.js"); -router.use('/todos', taskRouter); +router.use('/todos', Authentication, Authorization, taskRouter); +router.use("/user", userRouter); module.exports = router; \ No newline at end of file diff --git a/routers/userRouter.js b/routers/userRouter.js new file mode 100644 index 00000000..eaba2b1c --- /dev/null +++ b/routers/userRouter.js @@ -0,0 +1,9 @@ +"use strict" + +const router = require('express').Router(); +const ToDoController = require('../controllers/UserController'); + +router.post('/register', ToDoController.register); +router.post('/login', ToDoController.login); + +module.exports = router; \ No newline at end of file From 837e2672f0fab0d0902fbca6864143e0a6990649 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Wed, 4 Mar 2020 16:22:14 +0700 Subject: [PATCH 06/15] belum ada progress --- controllers/ToDoController.js | 1 + controllers/UserController.js | 32 +++++++++++++++++++++++--- helpers/jwt.js | 11 +++++++++ index.js | 2 ++ middleware/Authentication.js | 23 ++++++++++++++----- middleware/Authorization.js | 42 +++++++++++++++++++++++------------ models/user.js | 24 ++++++++++++++++++++ package-lock.json | 3 ++- package.json | 7 ++++-- public/index.html | 13 +++++++++++ routers/index.js | 4 +--- routers/taskRouter.js | 8 +++---- 12 files changed, 137 insertions(+), 33 deletions(-) create mode 100644 helpers/jwt.js create mode 100644 public/index.html diff --git a/controllers/ToDoController.js b/controllers/ToDoController.js index 7ab787cb..c32be9bd 100644 --- a/controllers/ToDoController.js +++ b/controllers/ToDoController.js @@ -27,6 +27,7 @@ class ToDoController{ * $ref: "#/components/schemas/ToDo" * "500": * description: Internal Server Error + * */ static getTodos(req, res, next){ const user = req.user; diff --git a/controllers/UserController.js b/controllers/UserController.js index 06986fc9..ed6ec447 100644 --- a/controllers/UserController.js +++ b/controllers/UserController.js @@ -1,9 +1,35 @@ const model = require('../models'); const User = model.User; const Bcrypt = require('../helpers/bcrypt.js'); -const jwt = require("jsonwebtoken"); +const jwt = require("../helpers/jwt.js"); +/** + * @swagger + * tags: + * name: ToDo + * description: ToDo management + */ class UserController{ + /** + * @swagger + * path: + * /users/register: + * get: + * summary: Get all todos + * tags: [ToDo] + * responses: + * "200": + * description: Array of ToDo + * content: + * application/json: + * schema: + * type: array + * items: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + * + */ static register(req, res, next){ const email = req.body.email; const password = req.body.password; @@ -12,7 +38,7 @@ class UserController{ password: password }) .then(data => { - let token = jwt.sign({email: data.email}, process.env.JWT_SECRET); + let token = jwt.sign({email: data.email}); res.status(200).json({token}) }) .catch(next); @@ -24,7 +50,7 @@ class UserController{ .then(data=>{ if(data){ if(Bcrypt.compareSync(password, data.password)){ - let token = jwt.sign({email: User.email}, process.env.JWT_SECRET); + let token = jwt.sign({email: User.email}); res.status(200).json({token}) }else{ next(err); diff --git a/helpers/jwt.js b/helpers/jwt.js new file mode 100644 index 00000000..410474ea --- /dev/null +++ b/helpers/jwt.js @@ -0,0 +1,11 @@ +const jwt = require("jsonwebtoken"); +class Jwt{ + static sign(email){ + return jwt.sign(email, process.env.JWT_SECRET); + } + static verify(token){ + return jwt.verify(token, process.env.JWT_SECRET); + } +} + +module.exports = Jwt; \ No newline at end of file diff --git a/index.js b/index.js index 109aac69..1de6e739 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,10 @@ const morganChalk = require('./morganChalk'); const router = require('./routers'); const ErrorHandler = require('./middleware/ErrorHandler'); const dotenv = require('dotenv'); +const path = require('path'); dotenv.config(); +app.use('/static', express.static(path.join(__dirname, 'public'))) app.use(morganChalk); app.use(express.urlencoded({extended:true})) app.use(express.json()); diff --git a/middleware/Authentication.js b/middleware/Authentication.js index 31ba43eb..7e2d7509 100644 --- a/middleware/Authentication.js +++ b/middleware/Authentication.js @@ -1,14 +1,25 @@ -const jwt = require('jsonwebtoken'); +const jwt = require('../helpers/jwt.js'); +const User = require('../models/user.js'); module.exports = (req, res, next) => { try { const token = req.headers.authorization; - const decodedToken = jwt.verify(token, process.env.JWT_SECRET); + const decodedToken = jwt.verify(token); req.jwt = decodedToken; - next(); + User.findOne({ + where: { + id: req.jwt.id + } + }) + .then(user=>{ + if(user){next()} + else{ + throw new Error(res.status) + } + }) + .catch(next); + } catch { - res.status(401).json({ - error: new Error('Invalid request!') - }); + next(err); } }; \ No newline at end of file diff --git a/middleware/Authorization.js b/middleware/Authorization.js index f3c03d3e..7254ca5c 100644 --- a/middleware/Authorization.js +++ b/middleware/Authorization.js @@ -1,19 +1,33 @@ const models = require('../models'); +const ToDo = models.ToDo; const User = models.User; module.exports = (req, res, next) => { - const email = req.jwt.email; - User.findAll({ - where: { email: email } - }).then(users => { - if (users.length === 0) { - res.status(403).end(); - } else { - const user = users[0]; - req.user = user; - next(); - } - }).catch(err => { - res.status(500).end(); - }); + let UserId = req.jwt.UserId; + let TodoId = req.params.id; + + ToDo.findByPk(TodoId) + .then(result=>{ + if(result.UserId === UserId){ + next(); + } + else{ + throw new Error(res.status) + } + }) + .catch(next); + // const email = req.jwt.email; + // User.findAll({ + // where: { email: email } + // }).then(users => { + // if (users.length === 0) { + // res.status(403).end(); + // } else { + // const user = users[0]; + // req.user = user; + // next(); + // } + // }).catch(err => { + // res.status(500).end(); + // }); }; \ No newline at end of file diff --git a/models/user.js b/models/user.js index 175f97e3..560ec9fe 100644 --- a/models/user.js +++ b/models/user.js @@ -1,4 +1,28 @@ 'use strict'; +/** + * @swagger + * components: + * schemas: + * User: + * type: object + * required: + * - email + * - password + * properties: + * email: + * type: string + * password: + * type: string + * createdAt: + * type: Date + * updatedAt: + * type: Date + * example: + * email: rofandi.gunawan@yahoo.com + * password: rofandi.gunawan + * createdAt: 2020-10-10T00:00:00.000Z + * updatedAt: 2020-10-10T00:00:00.000Z + */ const Bcrypt = require('../helpers/bcrypt.js') module.exports = (sequelize, DataTypes) => { diff --git a/package-lock.json b/package-lock.json index 5399d604..d09556c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -289,7 +289,8 @@ "dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", - "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==", + "dev": true }, "dottie": { "version": "2.0.2", diff --git a/package.json b/package.json index 112fb0b2..8493b4ba 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "NODE_ENV=development nodemon app.js" }, "repository": { "type": "git", @@ -20,7 +21,6 @@ "dependencies": { "bcrypt": "^4.0.1", "chalk": "^3.0.0", - "dotenv": "^8.2.0", "express": "^4.17.1", "http-errors": "^1.7.3", "jsonwebtoken": "^8.5.1", @@ -29,5 +29,8 @@ "sequelize": "^5.21.5", "swagger-jsdoc": "^3.5.0", "swagger-ui-express": "^4.1.3" + }, + "devDependencies": { + "dotenv": "^8.2.0" } } diff --git a/public/index.html b/public/index.html new file mode 100644 index 00000000..8d5b4da8 --- /dev/null +++ b/public/index.html @@ -0,0 +1,13 @@ + + + +
+
+
+
+

+ +
+ + + diff --git a/routers/index.js b/routers/index.js index 5827f994..c2af8863 100644 --- a/routers/index.js +++ b/routers/index.js @@ -1,12 +1,10 @@ "use strict" - const router = require('express').Router(); const taskRouter = require('./taskRouter.js'); const userRouter = require('./userRouter.js'); const Authentication = require("../middleware/Authentication.js"); -const Authorization = require("../middleware/Authorization.js"); -router.use('/todos', Authentication, Authorization, taskRouter); +router.use('/todos', Authentication, taskRouter); router.use("/user", userRouter); module.exports = router; \ No newline at end of file diff --git a/routers/taskRouter.js b/routers/taskRouter.js index 4f839534..d8b0892d 100644 --- a/routers/taskRouter.js +++ b/routers/taskRouter.js @@ -1,12 +1,12 @@ "use strict" - const router = require('express').Router(); const ToDoController = require('../controllers/ToDoController'); +const Authorization = require("../middleware/Authorization.js"); router.get('/', ToDoController.getTodos); router.post('/', ToDoController.addTodo); -router.get('/:id', ToDoController.getTodo); -router.put('/:id', ToDoController.updateTodo); -router.delete('/:id', ToDoController.deleteTodo); +router.get('/:id', Authorization, ToDoController.getTodo); +router.put('/:id', Authorization, ToDoController.updateTodo); +router.delete('/:id', Authorization, ToDoController.deleteTodo); module.exports = router; \ No newline at end of file From 4a477673f81449d75fe52bcb80ee88c5281740a1 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Thu, 5 Mar 2020 00:23:02 +0700 Subject: [PATCH 07/15] server client --- .vscode/launch.json | 18 +++ Client/index.html | 81 +++++++++++++ Client/js/dataView.js | 2 + Client/js/login.js | 17 +++ Client/js/page.js | 60 ++++++++++ Client/js/request.js | 0 Client/js/todos.js | 15 +++ README.md => Server/README.md | 0 bcrypttest.js => Server/bcrypttest.js | 0 {config => Server/config}/config.js | 0 .../controllers}/ToDoController.js | 106 ++++++++++-------- .../controllers}/UserController.js | 9 +- {helpers => Server/helpers}/bcrypt.js | 0 {helpers => Server/helpers}/jwt.js | 1 + index.js => Server/index.js | 8 +- Server/middleware/Authentication.js | 38 +++++++ .../middleware}/Authorization.js | 10 +- .../middleware}/ErrorHandler.js | 0 .../migrations}/20200303074905-create-user.js | 0 .../20200303081007-create-to-do.js | 0 {models => Server/models}/index.js | 0 {models => Server/models}/todo.js | 0 {models => Server/models}/user.js | 0 morganChalk.js => Server/morganChalk.js | 0 package-lock.json => Server/package-lock.json | 9 ++ package.json => Server/package.json | 4 +- {routers => Server/routers}/index.js | 2 +- {routers => Server/routers}/taskRouter.js | 0 {routers => Server/routers}/userRouter.js | 0 swaggerOptions.js => Server/swaggerOptions.js | 0 middleware/Authentication.js | 25 ----- public/index.html | 13 --- 32 files changed, 324 insertions(+), 94 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 Client/index.html create mode 100644 Client/js/dataView.js create mode 100644 Client/js/login.js create mode 100644 Client/js/page.js create mode 100644 Client/js/request.js create mode 100644 Client/js/todos.js rename README.md => Server/README.md (100%) rename bcrypttest.js => Server/bcrypttest.js (100%) rename {config => Server/config}/config.js (100%) rename {controllers => Server/controllers}/ToDoController.js (50%) rename {controllers => Server/controllers}/UserController.js (85%) rename {helpers => Server/helpers}/bcrypt.js (100%) rename {helpers => Server/helpers}/jwt.js (89%) rename index.js => Server/index.js (77%) create mode 100644 Server/middleware/Authentication.js rename {middleware => Server/middleware}/Authorization.js (76%) rename {middleware => Server/middleware}/ErrorHandler.js (100%) rename {migrations => Server/migrations}/20200303074905-create-user.js (100%) rename {migrations => Server/migrations}/20200303081007-create-to-do.js (100%) rename {models => Server/models}/index.js (100%) rename {models => Server/models}/todo.js (100%) rename {models => Server/models}/user.js (100%) rename morganChalk.js => Server/morganChalk.js (100%) rename package-lock.json => Server/package-lock.json (99%) rename package.json => Server/package.json (87%) rename {routers => Server/routers}/index.js (89%) rename {routers => Server/routers}/taskRouter.js (100%) rename {routers => Server/routers}/userRouter.js (100%) rename swaggerOptions.js => Server/swaggerOptions.js (100%) delete mode 100644 middleware/Authentication.js delete mode 100644 public/index.html diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..47362e96 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/Server/index.js", + "cwd": "${workspaceFolder}/Server" + } + ] +} \ No newline at end of file diff --git a/Client/index.html b/Client/index.html new file mode 100644 index 00000000..61a5982f --- /dev/null +++ b/Client/index.html @@ -0,0 +1,81 @@ + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + \ No newline at end of file diff --git a/Client/js/dataView.js b/Client/js/dataView.js new file mode 100644 index 00000000..8968406a --- /dev/null +++ b/Client/js/dataView.js @@ -0,0 +1,2 @@ +let currentPage = "login"; +let jwt = null; \ No newline at end of file diff --git a/Client/js/login.js b/Client/js/login.js new file mode 100644 index 00000000..336bd553 --- /dev/null +++ b/Client/js/login.js @@ -0,0 +1,17 @@ +$("#login-submit").click(function() { + const email = $("#login-email").val(); + const password = $("#login-password").val(); + $.ajax("http://localhost:3000/user/login", { + type: "POST", + data: { + email, + password + } + }) + .done(result => { + jwt = result.token; + switchToLoggedIn(); + showPage("todos"); + }) + .fail(message => console.log(message)); +}); \ No newline at end of file diff --git a/Client/js/page.js b/Client/js/page.js new file mode 100644 index 00000000..23a88c13 --- /dev/null +++ b/Client/js/page.js @@ -0,0 +1,60 @@ +function hideAllPages() { + $("#register-page").hide(); + $("#login-page").hide(); + $("#todos-page").hide(); +} + +function showPage(page) { + currentPage = page; + hideAllPages(); + $(".nav-item").removeClass("active"); + switch (page) { + case "login": + $("#login-page").show(); + $("#login-link").parent().addClass("active"); + break; + case "register": + $("#register-page").show(); + $("#register-link").parent().addClass("active"); + break; + case "todos": + $("#todos-page").show(); + $("#todos-link").parent().addClass("active"); + loadTodos(); + break; + } +} + +function switchToLoggedIn() { + $("#login-link").parent().hide(); + $("#register-link").parent().hide(); + $("#todos-link").parent().show(); + $("#logout-link").parent().show(); +} + +function switchToLoggedOut() { + $("#login-link").parent().show(); + $("#register-link").parent().show(); + $("#todos-link").parent().hide(); + $("#logout-link").parent().hide(); +} + +$("#login-link").click(function () { + showPage("login"); +}); + +$("#register-link").click(function () { + showPage("register"); +}); + +$("#todos-link").click(function () { + showPage("todos"); +}); + +$("#logout-link").click(function () { + jwt = null; + switchToLoggedOut(); + showPage("login"); +}); + +showPage(currentPage); \ No newline at end of file diff --git a/Client/js/request.js b/Client/js/request.js new file mode 100644 index 00000000..e69de29b diff --git a/Client/js/todos.js b/Client/js/todos.js new file mode 100644 index 00000000..7d8baab0 --- /dev/null +++ b/Client/js/todos.js @@ -0,0 +1,15 @@ +function loadTodos() { + $.ajax("http://localhost:3000/todos", { + type: "GET", + headers: { + "Authorization": jwt + } + }) + .done(todos => { + $("#todos-list").html(""); + for (const todo of todos) { + $("#todos-list").append(`
  • ${todo.title}
    ${todo.description}
  • `); + } + }) + .fail(message => console.log(message)); +} \ No newline at end of file diff --git a/README.md b/Server/README.md similarity index 100% rename from README.md rename to Server/README.md diff --git a/bcrypttest.js b/Server/bcrypttest.js similarity index 100% rename from bcrypttest.js rename to Server/bcrypttest.js diff --git a/config/config.js b/Server/config/config.js similarity index 100% rename from config/config.js rename to Server/config/config.js diff --git a/controllers/ToDoController.js b/Server/controllers/ToDoController.js similarity index 50% rename from controllers/ToDoController.js rename to Server/controllers/ToDoController.js index c32be9bd..05438274 100644 --- a/controllers/ToDoController.js +++ b/Server/controllers/ToDoController.js @@ -38,22 +38,22 @@ class ToDoController{ .catch(err => next(err)); } static getTodo(req, res, next){ - const id = req.params.id; - const user = req.user; - ToDo.findOne({ - where: { - id: id, - UserId: user.id - } - }) - .then(data=> { - if (data !== null) { - res.status(200).json(data); - } else { - next({ name: "NotFound" }); - } - }) - .catch(err => next(err)); + const todo = req.todo; + res.status(200).json(todo); + // const id = req.params.id; + // ToDo.findOne({ + // where: { + // id: id + // } + // }) + // .then(data=> { + // if (data !== null) { + // res.status(200).json(data); + // } else { + // next({ name: "NotFound" }); + // } + // }) + // .catch(err => next(err)); } static addTodo(req, res, next){ const title = req.body.title; @@ -72,24 +72,33 @@ class ToDoController{ .catch(err => next(err)); } static updateTodo(req, res){ - const id = req.params.id; - const user = req.user; - const newData = { - title: req.body.title, - description: req.body.description, - status: req.body.status, - due_date: req.body.due_date - } - let option = {where: {id:id, UserId: user.id }}; - ToDo.update(newData, option) - .then(data => { - if (data !== null && data!=false) { - res.status(200).json(data); - } else { - next({ name: "NotFound" }); - } - }) - .catch(err => next(err)); + const todo = req.todo; + todo.title = req.body.title; + todo.description = req.body.description; + todo.status = req.body.status; + todo.due_date = req.body.due_date; + todo.save() + .then(data => res.status(200).json(data)) + .catch(next); + + // const id = req.params.id; + // const user = req.user; + // const newData = { + // title: req.body.title, + // description: req.body.description, + // status: req.body.status, + // due_date: req.body.due_date + // } + // let option = {where: {id:id, UserId: user.id }}; + // ToDo.update(newData, option) + // .then(data => { + // if (data !== null && data!=false) { + // res.status(200).json(data); + // } else { + // next({ name: "NotFound" }); + // } + // }) + // .catch(err => next(err)); // ToDo.findByPk(id) // .then(data => { @@ -103,18 +112,23 @@ class ToDoController{ // .catch(err => res.status(404).json(err)); } static deleteTodo(req, res){ - const id = req.params.id; - const user = req.user; - let option = { where: {id:id, UserId: user.id}}; - ToDo.destroy(option) - .then(data => { - if (data !== null) { - res.status(200).json(data); - } else { - next({ name: "NotFound" }); - } - }) - .catch(err => next(err)); + const todo = req.todo; + todo.destroy() + .then(data => res.status(200).json(data)) + .catch(next); + + // const id = req.params.id; + // const user = req.user; + // let option = { where: {id:id, UserId: user.id}}; + // ToDo.destroy(option) + // .then(data => { + // if (data !== null) { + // res.status(200).json(data); + // } else { + // next({ name: "NotFound" }); + // } + // }) + // .catch(err => next(err)); } } diff --git a/controllers/UserController.js b/Server/controllers/UserController.js similarity index 85% rename from controllers/UserController.js rename to Server/controllers/UserController.js index ed6ec447..28960028 100644 --- a/controllers/UserController.js +++ b/Server/controllers/UserController.js @@ -49,11 +49,14 @@ class UserController{ User.findOne({ where: {email}}) .then(data=>{ if(data){ - if(Bcrypt.compareSync(password, data.password)){ - let token = jwt.sign({email: User.email}); + if(Bcrypt.compare(password, data.password)){ + let token = jwt.sign({email: data.email}); res.status(200).json({token}) }else{ - next(err); + next({ + status: 401, + message: "Unauthorized" + }); } } else{ diff --git a/helpers/bcrypt.js b/Server/helpers/bcrypt.js similarity index 100% rename from helpers/bcrypt.js rename to Server/helpers/bcrypt.js diff --git a/helpers/jwt.js b/Server/helpers/jwt.js similarity index 89% rename from helpers/jwt.js rename to Server/helpers/jwt.js index 410474ea..4c35822f 100644 --- a/helpers/jwt.js +++ b/Server/helpers/jwt.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const jwt = require("jsonwebtoken"); class Jwt{ static sign(email){ diff --git a/index.js b/Server/index.js similarity index 77% rename from index.js rename to Server/index.js index 1de6e739..68d4f3ca 100644 --- a/index.js +++ b/Server/index.js @@ -2,14 +2,16 @@ const express = require('express') const app = express() -const port = process.env.port || 3000 +const port = process.env.PORT || 3000 const morganChalk = require('./morganChalk'); const router = require('./routers'); const ErrorHandler = require('./middleware/ErrorHandler'); const dotenv = require('dotenv'); const path = require('path'); +const cors = require("cors"); dotenv.config(); +app.use(cors()); app.use('/static', express.static(path.join(__dirname, 'public'))) app.use(morganChalk); app.use(express.urlencoded({extended:true})) @@ -26,6 +28,10 @@ router.get("/docs", swaggerUi.setup(specs, { explorer: true })); +console.log("PORT", process.env.PORT); +console.log("DB_USER", process.env.DB_USER); +console.log("DB_PASS", process.env.DB_PASS); +console.log("JWT_SECRET", process.env.JWT_SECRET); app.listen(port, () => console.log(`Example app listening on port ${port}!`)) module.exports = app \ No newline at end of file diff --git a/Server/middleware/Authentication.js b/Server/middleware/Authentication.js new file mode 100644 index 00000000..4fb045c0 --- /dev/null +++ b/Server/middleware/Authentication.js @@ -0,0 +1,38 @@ +const jwt = require('../helpers/jwt.js'); +const models = require('../models'); +const User = models.User; + +module.exports = (req, res, next) => { + let decodedToken; + try { + const token = req.headers.authorization; + decodedToken = jwt.verify(token); + } catch { + next ({ + status: 401, + message: "Unauthorized" + }); + return; + } + User.findOne({ + where: { + email: decodedToken.email + } + }) + .then(user=>{ + if(user){ + req.user = user; + next(); + } + else{ + next({ + status: 401, + message: "Unauthorized" + }) + } + }) + .catch(err => { + next(err); + }); + +}; \ No newline at end of file diff --git a/middleware/Authorization.js b/Server/middleware/Authorization.js similarity index 76% rename from middleware/Authorization.js rename to Server/middleware/Authorization.js index 7254ca5c..178668e8 100644 --- a/middleware/Authorization.js +++ b/Server/middleware/Authorization.js @@ -3,16 +3,20 @@ const ToDo = models.ToDo; const User = models.User; module.exports = (req, res, next) => { - let UserId = req.jwt.UserId; + let user = req.user; let TodoId = req.params.id; ToDo.findByPk(TodoId) .then(result=>{ - if(result.UserId === UserId){ + if(result.UserId === user.id){ + req.todo = result; next(); } else{ - throw new Error(res.status) + next({ + status: 403, + message: "Forbidden" + }); } }) .catch(next); diff --git a/middleware/ErrorHandler.js b/Server/middleware/ErrorHandler.js similarity index 100% rename from middleware/ErrorHandler.js rename to Server/middleware/ErrorHandler.js diff --git a/migrations/20200303074905-create-user.js b/Server/migrations/20200303074905-create-user.js similarity index 100% rename from migrations/20200303074905-create-user.js rename to Server/migrations/20200303074905-create-user.js diff --git a/migrations/20200303081007-create-to-do.js b/Server/migrations/20200303081007-create-to-do.js similarity index 100% rename from migrations/20200303081007-create-to-do.js rename to Server/migrations/20200303081007-create-to-do.js diff --git a/models/index.js b/Server/models/index.js similarity index 100% rename from models/index.js rename to Server/models/index.js diff --git a/models/todo.js b/Server/models/todo.js similarity index 100% rename from models/todo.js rename to Server/models/todo.js diff --git a/models/user.js b/Server/models/user.js similarity index 100% rename from models/user.js rename to Server/models/user.js diff --git a/morganChalk.js b/Server/morganChalk.js similarity index 100% rename from morganChalk.js rename to Server/morganChalk.js diff --git a/package-lock.json b/Server/package-lock.json similarity index 99% rename from package-lock.json rename to Server/package-lock.json index d09556c0..e61e46bd 100644 --- a/package-lock.json +++ b/Server/package-lock.json @@ -245,6 +245,15 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/Server/package.json similarity index 87% rename from package.json rename to Server/package.json index 8493b4ba..18af474c 100644 --- a/package.json +++ b/Server/package.json @@ -4,8 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "dev": "NODE_ENV=development nodemon app.js" + "dev": "NODE_ENV=development nodemon index.js" }, "repository": { "type": "git", @@ -21,6 +20,7 @@ "dependencies": { "bcrypt": "^4.0.1", "chalk": "^3.0.0", + "cors": "^2.8.5", "express": "^4.17.1", "http-errors": "^1.7.3", "jsonwebtoken": "^8.5.1", diff --git a/routers/index.js b/Server/routers/index.js similarity index 89% rename from routers/index.js rename to Server/routers/index.js index c2af8863..cad49741 100644 --- a/routers/index.js +++ b/Server/routers/index.js @@ -5,6 +5,6 @@ const userRouter = require('./userRouter.js'); const Authentication = require("../middleware/Authentication.js"); router.use('/todos', Authentication, taskRouter); -router.use("/user", userRouter); +router.use('/user', userRouter); module.exports = router; \ No newline at end of file diff --git a/routers/taskRouter.js b/Server/routers/taskRouter.js similarity index 100% rename from routers/taskRouter.js rename to Server/routers/taskRouter.js diff --git a/routers/userRouter.js b/Server/routers/userRouter.js similarity index 100% rename from routers/userRouter.js rename to Server/routers/userRouter.js diff --git a/swaggerOptions.js b/Server/swaggerOptions.js similarity index 100% rename from swaggerOptions.js rename to Server/swaggerOptions.js diff --git a/middleware/Authentication.js b/middleware/Authentication.js deleted file mode 100644 index 7e2d7509..00000000 --- a/middleware/Authentication.js +++ /dev/null @@ -1,25 +0,0 @@ -const jwt = require('../helpers/jwt.js'); -const User = require('../models/user.js'); - -module.exports = (req, res, next) => { - try { - const token = req.headers.authorization; - const decodedToken = jwt.verify(token); - req.jwt = decodedToken; - User.findOne({ - where: { - id: req.jwt.id - } - }) - .then(user=>{ - if(user){next()} - else{ - throw new Error(res.status) - } - }) - .catch(next); - - } catch { - next(err); - } -}; \ No newline at end of file diff --git a/public/index.html b/public/index.html deleted file mode 100644 index 8d5b4da8..00000000 --- a/public/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - -
    -
    -
    -
    -

    - -
    - - - From fe8cc6287d5a1989c529af2949ae22335efb19fb Mon Sep 17 00:00:00 2001 From: Rofandi Date: Sat, 7 Mar 2020 17:41:13 +0700 Subject: [PATCH 08/15] added API and google login --- Client/index.html | 80 +++++- Client/js/add.js | 25 ++ Client/js/dataView.js | 1 - Client/js/delete.js | 32 +++ Client/js/edit.js | 42 ++++ Client/js/login.js | 2 +- Client/js/page.js | 41 +++- Client/js/register.js | 34 +++ Client/js/request.js | 26 ++ Client/js/todos.js | 31 ++- Server/controllers/PasswordWolfController.js | 13 + Server/controllers/ToDoController.js | 65 +---- Server/controllers/UserController.js | 27 ++ Server/index.js | 9 +- Server/middleware/Authorization.js | 16 +- Server/package-lock.json | 245 +++++++++++++++++++ Server/package.json | 2 + Server/routers/index.js | 2 + Server/routers/passwordWolfRouter.js | 8 + Server/routers/userRouter.js | 7 +- 20 files changed, 607 insertions(+), 101 deletions(-) create mode 100644 Client/js/add.js create mode 100644 Client/js/delete.js create mode 100644 Client/js/edit.js create mode 100644 Client/js/register.js create mode 100644 Server/controllers/PasswordWolfController.js create mode 100644 Server/routers/passwordWolfRouter.js diff --git a/Client/index.html b/Client/index.html index 61a5982f..75a45b51 100644 --- a/Client/index.html +++ b/Client/index.html @@ -3,6 +3,8 @@ + + @@ -27,6 +29,9 @@ + @@ -37,10 +42,13 @@

    Register


    -
    +

    -

    - + +
    +


    + +
    @@ -48,23 +56,73 @@

    Register

    Login


    -
    +

    -

    +

    +
    + + + + + + - + @@ -75,7 +133,11 @@

    TODOs

    + + + + \ No newline at end of file diff --git a/Client/js/add.js b/Client/js/add.js new file mode 100644 index 00000000..d61f07ac --- /dev/null +++ b/Client/js/add.js @@ -0,0 +1,25 @@ +$("#todosAdd-submit").click(function() { + const title = $("#todo-title").val(); + const description = $("#todo-description").val(); + const status = $("#todo-status").val(); + const due_date = $("#todo-due_date").val(); + console.log(title, description, status, due_date); + $.ajax('http://localhost:3000/todos',{ + type: "POST", + headers: { + "Authorization": localStorage.jwt + }, + data: { + title, + description, + status, + due_date + } + }) + .done(()=>showPage("todos")) + .fail(message=>console.log(message)); +}); + +$("#todosAdd-cancel").click(function () { + showPage("todos"); +}); \ No newline at end of file diff --git a/Client/js/dataView.js b/Client/js/dataView.js index 8968406a..1a6bb951 100644 --- a/Client/js/dataView.js +++ b/Client/js/dataView.js @@ -1,2 +1 @@ let currentPage = "login"; -let jwt = null; \ No newline at end of file diff --git a/Client/js/delete.js b/Client/js/delete.js new file mode 100644 index 00000000..dfa47797 --- /dev/null +++ b/Client/js/delete.js @@ -0,0 +1,32 @@ +function loadTodoForDelete(id) { + $.ajax(`http://localhost:3000/todos/${id}`, { + type: "GET", + headers: { + "Authorization": localStorage.jwt + } + }) + .done(todo => { + $("#todoDelete-id").val(id); + $("#todoDelete-title").val(todo.title); + $("#todoDelete-description").val(todo.description); + $("#todoDelete-status").val(todo.status); + $("#todoDelete-due_date").val(todo.due_date); + }) + .fail(message => console.log(message)); +} + +$("#todosDelete-submit").click(function () { + const id = $("#todoDelete-id").val(); + $.ajax(`http://localhost:3000/todos/${id}`, { + type: "DELETE", + headers: { + "Authorization": localStorage.jwt + } + }) + .done(() => showPage("todos")) + .fail(message => console.log(message)); +}); + +$("#todosDelete-cancel").click(function () { + showPage("todos"); +}); \ No newline at end of file diff --git a/Client/js/edit.js b/Client/js/edit.js new file mode 100644 index 00000000..6fd3cfd3 --- /dev/null +++ b/Client/js/edit.js @@ -0,0 +1,42 @@ +function loadTodoForEdit(id) { + $.ajax(`http://localhost:3000/todos/${id}`, { + type: "GET", + headers: { + "Authorization": localStorage.jwt + } + }) + .done(todo => { + $("#todoEdit-id").val(id); + $("#todoEdit-title").val(todo.title); + $("#todoEdit-description").val(todo.description); + $("#todoEdit-status").val(todo.status); + $("#todoEdit-due_date").val(todo.due_date); + }) + .fail(message => console.log(message)); +} + +$("#todosEdit-submit").click(function () { + const id = $("#todoEdit-id").val(); + const title = $("#todoEdit-title").val(); + const description = $("#todoEdit-description").val(); + const status = $("#todoEdit-status").val(); + const due_date = $("#todoEdit-due_date").val(); + $.ajax(`http://localhost:3000/todos/${id}`, { + type: "PUT", + headers: { + "Authorization": localStorage.jwt + }, + data: { + title, + description, + status, + due_date + } + }) + .done(() => showPage("todos")) + .fail(message => console.log(message)); +}); + +$("#todosEdit-cancel").click(function () { + showPage("todos"); +}); \ No newline at end of file diff --git a/Client/js/login.js b/Client/js/login.js index 336bd553..a2bf9dde 100644 --- a/Client/js/login.js +++ b/Client/js/login.js @@ -9,7 +9,7 @@ $("#login-submit").click(function() { } }) .done(result => { - jwt = result.token; + localStorage.jwt = result.token; switchToLoggedIn(); showPage("todos"); }) diff --git a/Client/js/page.js b/Client/js/page.js index 23a88c13..17fbacc9 100644 --- a/Client/js/page.js +++ b/Client/js/page.js @@ -2,9 +2,12 @@ function hideAllPages() { $("#register-page").hide(); $("#login-page").hide(); $("#todos-page").hide(); + $("#todosAdd-page").hide(); + $("#todosEdit-page").hide(); + $("#todosDelete-page").hide(); } -function showPage(page) { +function showPage(page, id) { currentPage = page; hideAllPages(); $(".nav-item").removeClass("active"); @@ -22,6 +25,20 @@ function showPage(page) { $("#todos-link").parent().addClass("active"); loadTodos(); break; + case "todoAdd": + $("#todos-page").show(); + $("#todos-link").parent().addClass("active"); + loadTodos(); + $("#todosAdd-page").show(); + break; + case "todoEdit": + loadTodoForEdit(id); + $("#todosEdit-page").show(); + break; + case "todoDelete": + loadTodoForDelete(id); + $("#todosDelete-page").show(); + break; } } @@ -30,6 +47,15 @@ function switchToLoggedIn() { $("#register-link").parent().hide(); $("#todos-link").parent().show(); $("#logout-link").parent().show(); + $("#sign-out-google").parent().hide(); +} + +function switchToLoggedInWithGoogle() { + $("#login-link").parent().hide(); + $("#register-link").parent().hide(); + $("#todos-link").parent().show(); + $("#logout-link").parent().hide(); + $("#sign-out-google").parent().show(); } function switchToLoggedOut() { @@ -37,6 +63,7 @@ function switchToLoggedOut() { $("#register-link").parent().show(); $("#todos-link").parent().hide(); $("#logout-link").parent().hide(); + $("#sign-out-google").parent().hide(); } $("#login-link").click(function () { @@ -52,9 +79,17 @@ $("#todos-link").click(function () { }); $("#logout-link").click(function () { - jwt = null; + delete localStorage.jwt; switchToLoggedOut(); showPage("login"); }); -showPage(currentPage); \ No newline at end of file +$(document).ready(() => { + if (localStorage.jwt) { + switchToLoggedIn(); + showPage("todos"); + } else { + switchToLoggedOut(); + showPage("login"); + } +}); diff --git a/Client/js/register.js b/Client/js/register.js new file mode 100644 index 00000000..f9d8948d --- /dev/null +++ b/Client/js/register.js @@ -0,0 +1,34 @@ +$("#register-submit").click(function() { + const email = $("#register-email").val(); + const password = $("#register-password").val(); + $.ajax("http://localhost:3000/user/register", { + type: "POST", + data: { + email, + password + } + }) + .done(result => { + localStorage.jwt = result.token; + switchToLoggedIn(); + showPage("todos"); + }) + .fail(message => console.log(message)); +}); + +$("#register-generate").click(function () { + $("#register-passwords").html(""); + $.ajax("http://localhost:3000/passwordWolf/generate", { + type: "GET" + }) + .done(result => { + let html = "

    Random passwords:

      "; + for (let i = 0; i < result.length; i++) { + const password = result[i].password; + html += "
    • " + password + "
    • "; + } + html += "
    "; + $("#register-passwords").html(html); + }) + .fail(message => console.log(message)); +}); \ No newline at end of file diff --git a/Client/js/request.js b/Client/js/request.js index e69de29b..311fff29 100644 --- a/Client/js/request.js +++ b/Client/js/request.js @@ -0,0 +1,26 @@ +function signOut() { + var auth2 = gapi.auth2.getAuthInstance(); + auth2.signOut().then(function() { + console.log('User signed out.'); + }); + delete localStorage.jwt; + switchToLoggedOut(); + showPage("login"); +} + +function onSignIn(googleUser) { + var id_token = googleUser.getAuthResponse().id_token; + $.ajax({ + method: 'POST', + url: `http://localhost:3000/user/googlelogin`, + data: { + token: id_token + } + }) + .done(result => { + localStorage.jwt = result.token; + switchToLoggedInWithGoogle(); + showPage("todos"); + }) + .fail(message => console.log(message)); +} \ No newline at end of file diff --git a/Client/js/todos.js b/Client/js/todos.js index 7d8baab0..fc6d3e47 100644 --- a/Client/js/todos.js +++ b/Client/js/todos.js @@ -2,14 +2,37 @@ function loadTodos() { $.ajax("http://localhost:3000/todos", { type: "GET", headers: { - "Authorization": jwt + "Authorization": localStorage.jwt } }) .done(todos => { $("#todos-list").html(""); - for (const todo of todos) { - $("#todos-list").append(`
  • ${todo.title}
    ${todo.description}
  • `); + for (let i = 0; i < todos.length; i++) { + $("#todos-list").append(` +
  • + ${todos[i].title}
    + ${todos[i].description} +
  • + + + `); + $(`#todos-edit-${i}`).click(function () { + const id = todos[i].id; + showPage("todoEdit", id); + }); + $(`#todos-delete-${i}`).click(function () { + const id = todos[i].id; + showPage("todoDelete", id); + }); } }) .fail(message => console.log(message)); -} \ No newline at end of file +} + +$("#todos-add").click(function() { + showPage("todoAdd"); + $("#todo-title").val(""); + $("#todo-description").val(""); + $("#todo-status").val(""); + $("#todo-due_date").val(""); +}); \ No newline at end of file diff --git a/Server/controllers/PasswordWolfController.js b/Server/controllers/PasswordWolfController.js new file mode 100644 index 00000000..231d8680 --- /dev/null +++ b/Server/controllers/PasswordWolfController.js @@ -0,0 +1,13 @@ +const axios = require("axios"); + +class PasswordWolfController { + static generate(req, res, next) { + axios.get("https://passwordwolf.com/api/?special=off&length=6&repeat=5") + .then(response => { + res.status(200).json(response.data); + }) + .catch(err => next(err)); + } +} + +module.exports = PasswordWolfController; \ No newline at end of file diff --git a/Server/controllers/ToDoController.js b/Server/controllers/ToDoController.js index 05438274..a3c5c1c4 100644 --- a/Server/controllers/ToDoController.js +++ b/Server/controllers/ToDoController.js @@ -40,20 +40,6 @@ class ToDoController{ static getTodo(req, res, next){ const todo = req.todo; res.status(200).json(todo); - // const id = req.params.id; - // ToDo.findOne({ - // where: { - // id: id - // } - // }) - // .then(data=> { - // if (data !== null) { - // res.status(200).json(data); - // } else { - // next({ name: "NotFound" }); - // } - // }) - // .catch(err => next(err)); } static addTodo(req, res, next){ const title = req.body.title; @@ -79,56 +65,17 @@ class ToDoController{ todo.due_date = req.body.due_date; todo.save() .then(data => res.status(200).json(data)) - .catch(next); - - // const id = req.params.id; - // const user = req.user; - // const newData = { - // title: req.body.title, - // description: req.body.description, - // status: req.body.status, - // due_date: req.body.due_date - // } - // let option = {where: {id:id, UserId: user.id }}; - // ToDo.update(newData, option) - // .then(data => { - // if (data !== null && data!=false) { - // res.status(200).json(data); - // } else { - // next({ name: "NotFound" }); - // } - // }) - // .catch(err => next(err)); - - // ToDo.findByPk(id) - // .then(data => { - // data.title = req.body.title; - // data.description = req.body.description; - // data.status = req.body.status; - // data.due_date = req.body.due_date; - // return data.save(); - // }) - // .then(data => res.status(201).json(data)) - // .catch(err => res.status(404).json(err)); + .catch(err => { + next(err); + }); } static deleteTodo(req, res){ const todo = req.todo; todo.destroy() .then(data => res.status(200).json(data)) - .catch(next); - - // const id = req.params.id; - // const user = req.user; - // let option = { where: {id:id, UserId: user.id}}; - // ToDo.destroy(option) - // .then(data => { - // if (data !== null) { - // res.status(200).json(data); - // } else { - // next({ name: "NotFound" }); - // } - // }) - // .catch(err => next(err)); + .catch(err => { + next(err); + }); } } diff --git a/Server/controllers/UserController.js b/Server/controllers/UserController.js index 28960028..b96282c6 100644 --- a/Server/controllers/UserController.js +++ b/Server/controllers/UserController.js @@ -2,6 +2,8 @@ const model = require('../models'); const User = model.User; const Bcrypt = require('../helpers/bcrypt.js'); const jwt = require("../helpers/jwt.js"); +const {OAuth2Client} = require('google-auth-library'); +const client = new OAuth2Client(process.env.CLIENT_ID); /** * @swagger @@ -64,6 +66,31 @@ class UserController{ } }) } + static googleLogin(req, res, next) { + let token = req.body.token; + client.verifyIdToken({ + idToken: token, + audience: process.env.CLIENT_ID + }) + .then(response => { + return User.findOne({ where: { email: response.payload.email } }) + .then(user => { + if (!user) { + return User.create({ + email: response.payload.email, + password: '12345' + }); + } else { + return user; + } + }); + }).then(user => { + let token = jwt.sign({email: user.email}); + res.status(200).json({ token }); + }).catch(err => { + next(err); + }); + } } module.exports = UserController; \ No newline at end of file diff --git a/Server/index.js b/Server/index.js index 68d4f3ca..c9130759 100644 --- a/Server/index.js +++ b/Server/index.js @@ -8,11 +8,12 @@ const router = require('./routers'); const ErrorHandler = require('./middleware/ErrorHandler'); const dotenv = require('dotenv'); const path = require('path'); -const cors = require("cors"); +//const cors = require("cors"); dotenv.config(); -app.use(cors()); +//app.use(cors()); app.use('/static', express.static(path.join(__dirname, 'public'))) +app.use('/', express.static(path.join(__dirname, '../Client'))) app.use(morganChalk); app.use(express.urlencoded({extended:true})) app.use(express.json()); @@ -28,10 +29,6 @@ router.get("/docs", swaggerUi.setup(specs, { explorer: true })); -console.log("PORT", process.env.PORT); -console.log("DB_USER", process.env.DB_USER); -console.log("DB_PASS", process.env.DB_PASS); -console.log("JWT_SECRET", process.env.JWT_SECRET); app.listen(port, () => console.log(`Example app listening on port ${port}!`)) module.exports = app \ No newline at end of file diff --git a/Server/middleware/Authorization.js b/Server/middleware/Authorization.js index 178668e8..18e97cd8 100644 --- a/Server/middleware/Authorization.js +++ b/Server/middleware/Authorization.js @@ -4,7 +4,7 @@ const User = models.User; module.exports = (req, res, next) => { let user = req.user; - let TodoId = req.params.id; + let TodoId = parseInt(req.params.id); ToDo.findByPk(TodoId) .then(result=>{ @@ -20,18 +20,4 @@ module.exports = (req, res, next) => { } }) .catch(next); - // const email = req.jwt.email; - // User.findAll({ - // where: { email: email } - // }).then(users => { - // if (users.length === 0) { - // res.status(403).end(); - // } else { - // const user = users[0]; - // req.user = user; - // next(); - // } - // }).catch(err => { - // res.status(500).end(); - // }); }; \ No newline at end of file diff --git a/Server/package-lock.json b/Server/package-lock.json index e61e46bd..f6a76485 100644 --- a/Server/package-lock.json +++ b/Server/package-lock.json @@ -19,6 +19,14 @@ "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "requires": { + "event-target-shim": "^5.0.0" + } + }, "accepts": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", @@ -28,6 +36,29 @@ "negotiator": "0.6.2" } }, + "agent-base": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.0.tgz", + "integrity": "sha512-j1Q7cSCqN+AwrmDd+pzgqc0/NpC655x2bUf5ZjRIO77DcNBFmh+OgRNzF6OKdCC9RSCb19fGd99+bhXFdkRNqw==", + "requires": { + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", @@ -74,11 +105,29 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "arrify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/arrify/-/arrify-2.0.1.tgz", + "integrity": "sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==" + }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, + "base64-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + }, "basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -96,6 +145,11 @@ "node-pre-gyp": "0.14.0" } }, + "bignumber.js": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.2.1.tgz", + "integrity": "sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ==" + }, "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -344,6 +398,11 @@ "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" + }, "express": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", @@ -381,6 +440,16 @@ "vary": "~1.1.2" } }, + "extend": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", + "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==" + }, + "fast-text-encoding": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/fast-text-encoding/-/fast-text-encoding-1.0.1.tgz", + "integrity": "sha512-x4FEgaz3zNRtJfLFqJmHWxkMDDvXVtaznj2V9jiP8ACUJrUgist4bP9FmDL2Vew2Y9mEQI/tG4GqabaitYp9CQ==" + }, "finalhandler": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", @@ -395,6 +464,24 @@ "unpipe": "~1.0.0" } }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", @@ -433,6 +520,27 @@ "wide-align": "^1.1.0" } }, + "gaxios": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-2.3.2.tgz", + "integrity": "sha512-K/+py7UvKRDaEwEKlLiRKrFr+wjGjsMz5qH7Vs549QJS7cpSCOT/BbWL7pzqECflc46FcNPipjSfB+V1m8PAhw==", + "requires": { + "abort-controller": "^3.0.0", + "extend": "^3.0.2", + "https-proxy-agent": "^5.0.0", + "is-stream": "^2.0.0", + "node-fetch": "^2.3.0" + } + }, + "gcp-metadata": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-3.5.0.tgz", + "integrity": "sha512-ZQf+DLZ5aKcRpLzYUyBS3yo3N0JSa82lNDO8rj3nMSlovLcz2riKFBsYgDzeXcv75oo5eqB2lx+B14UvPoCRnA==", + "requires": { + "gaxios": "^2.1.0", + "json-bigint": "^0.3.0" + } + }, "glob": { "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", @@ -446,6 +554,88 @@ "path-is-absolute": "^1.0.0" } }, + "google-auth-library": { + "version": "5.10.1", + "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-5.10.1.tgz", + "integrity": "sha512-rOlaok5vlpV9rSiUu5EpR0vVpc+PhN62oF4RyX/6++DG1VsaulAFEMlDYBLjJDDPI6OcNOCGAKy9UVB/3NIDXg==", + "requires": { + "arrify": "^2.0.0", + "base64-js": "^1.3.0", + "ecdsa-sig-formatter": "^1.0.11", + "fast-text-encoding": "^1.0.0", + "gaxios": "^2.1.0", + "gcp-metadata": "^3.4.0", + "gtoken": "^4.1.0", + "jws": "^4.0.0", + "lru-cache": "^5.0.0" + }, + "dependencies": { + "jwa": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.0.tgz", + "integrity": "sha512-jrZ2Qx916EA+fq9cEAeCROWPTfCwi1IVHqT2tapuqLEVVDKFDENFw1oL+MwrTvH6msKxsd1YTDVw6uKEcsrLEA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.0.tgz", + "integrity": "sha512-KDncfTmOZoOMTFG4mBlG0qUIOlc03fmzH+ru6RgYVZhPkyiy/92Owlt/8UEN+a4TXR1FQetfIpJE8ApdvdVxTg==", + "requires": { + "jwa": "^2.0.0", + "safe-buffer": "^5.0.1" + } + } + } + }, + "google-p12-pem": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/google-p12-pem/-/google-p12-pem-2.0.4.tgz", + "integrity": "sha512-S4blHBQWZRnEW44OcR7TL9WR+QCqByRvhNDZ/uuQfpxywfupikf/miba8js1jZi6ZOGv5slgSuoshCWh6EMDzg==", + "requires": { + "node-forge": "^0.9.0" + } + }, + "gtoken": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/gtoken/-/gtoken-4.1.4.tgz", + "integrity": "sha512-VxirzD0SWoFUo5p8RDP8Jt2AGyOmyYcT/pOUgDKJCK+iSw0TMqwrVfY37RXTNmoKwrzmDHSk0GMT9FsgVmnVSA==", + "requires": { + "gaxios": "^2.1.0", + "google-p12-pem": "^2.0.0", + "jws": "^4.0.0", + "mime": "^2.2.0" + }, + "dependencies": { + "jwa": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.0.tgz", + "integrity": "sha512-jrZ2Qx916EA+fq9cEAeCROWPTfCwi1IVHqT2tapuqLEVVDKFDENFw1oL+MwrTvH6msKxsd1YTDVw6uKEcsrLEA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.0.tgz", + "integrity": "sha512-KDncfTmOZoOMTFG4mBlG0qUIOlc03fmzH+ru6RgYVZhPkyiy/92Owlt/8UEN+a4TXR1FQetfIpJE8ApdvdVxTg==", + "requires": { + "jwa": "^2.0.0", + "safe-buffer": "^5.0.1" + } + }, + "mime": { + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.4.tgz", + "integrity": "sha512-LRxmNwziLPT828z+4YkNzloCFC2YM4wrB99k+AV5ZbEyfGNWfG8SO1FUXLmLDBSo89NrJZ4DIWeLjy1CHGhMGA==" + } + } + }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -475,6 +665,30 @@ } } }, + "https-proxy-agent": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz", + "integrity": "sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA==", + "requires": { + "agent-base": "6", + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -533,6 +747,11 @@ "number-is-nan": "^1.0.0" } }, + "is-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz", + "integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -547,6 +766,14 @@ "esprima": "^4.0.0" } }, + "json-bigint": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-0.3.0.tgz", + "integrity": "sha1-DM2RLEuCcNBfBW+9E4FLU9OCWx4=", + "requires": { + "bignumber.js": "^7.0.0" + } + }, "json-schema-ref-parser": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/json-schema-ref-parser/-/json-schema-ref-parser-7.1.3.tgz", @@ -662,6 +889,14 @@ "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" }, + "lru-cache": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", + "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", + "requires": { + "yallist": "^3.0.2" + } + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -798,6 +1033,16 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz", "integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==" }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, + "node-forge": { + "version": "0.9.1", + "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-0.9.1.tgz", + "integrity": "sha512-G6RlQt5Sb4GMBzXvhfkeFmbqR6MzhtnT7VTHuLadjkii3rdYHNdw0m8zA4BTxVIh68FicCQ2NSUANpsqkr9jvQ==" + }, "node-pre-gyp": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz", diff --git a/Server/package.json b/Server/package.json index 18af474c..8b4af8d2 100644 --- a/Server/package.json +++ b/Server/package.json @@ -18,10 +18,12 @@ }, "homepage": "https://github.com/Rofandi/fancy-todo#readme", "dependencies": { + "axios": "^0.19.2", "bcrypt": "^4.0.1", "chalk": "^3.0.0", "cors": "^2.8.5", "express": "^4.17.1", + "google-auth-library": "^5.10.1", "http-errors": "^1.7.3", "jsonwebtoken": "^8.5.1", "morgan": "^1.9.1", diff --git a/Server/routers/index.js b/Server/routers/index.js index cad49741..8234c5cc 100644 --- a/Server/routers/index.js +++ b/Server/routers/index.js @@ -2,9 +2,11 @@ const router = require('express').Router(); const taskRouter = require('./taskRouter.js'); const userRouter = require('./userRouter.js'); +const passwordWolfRouter = require("./passwordWolfRouter.js"); const Authentication = require("../middleware/Authentication.js"); router.use('/todos', Authentication, taskRouter); router.use('/user', userRouter); +router.use('/passwordWolf', passwordWolfRouter); module.exports = router; \ No newline at end of file diff --git a/Server/routers/passwordWolfRouter.js b/Server/routers/passwordWolfRouter.js new file mode 100644 index 00000000..043edf1d --- /dev/null +++ b/Server/routers/passwordWolfRouter.js @@ -0,0 +1,8 @@ +"use strict" + +const router = require('express').Router(); +const PasswordWolfController = require('../controllers/PasswordWolfController'); + +router.get('/generate', PasswordWolfController.generate); + +module.exports = router; \ No newline at end of file diff --git a/Server/routers/userRouter.js b/Server/routers/userRouter.js index eaba2b1c..2a6446d9 100644 --- a/Server/routers/userRouter.js +++ b/Server/routers/userRouter.js @@ -1,9 +1,10 @@ "use strict" const router = require('express').Router(); -const ToDoController = require('../controllers/UserController'); +const UserController = require('../controllers/UserController'); -router.post('/register', ToDoController.register); -router.post('/login', ToDoController.login); +router.post('/register', UserController.register); +router.post('/login', UserController.login); +router.post('/googlelogin', UserController.googleLogin); module.exports = router; \ No newline at end of file From eaab0a69221d168d0d2c99093a258031df885fe6 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Mon, 9 Mar 2020 16:28:35 +0700 Subject: [PATCH 09/15] additional file : Procfile --- Server/Procfile | 1 + Server/controllers/UserController.js | 4 ++-- Server/package.json | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 Server/Procfile diff --git a/Server/Procfile b/Server/Procfile new file mode 100644 index 00000000..207d22f8 --- /dev/null +++ b/Server/Procfile @@ -0,0 +1 @@ +web: node app.js \ No newline at end of file diff --git a/Server/controllers/UserController.js b/Server/controllers/UserController.js index b96282c6..d8c184a0 100644 --- a/Server/controllers/UserController.js +++ b/Server/controllers/UserController.js @@ -56,8 +56,8 @@ class UserController{ res.status(200).json({token}) }else{ next({ - status: 401, - message: "Unauthorized" + status: 400, + message: "Wrong Password/Username" }); } } diff --git a/Server/package.json b/Server/package.json index 8b4af8d2..01c943ef 100644 --- a/Server/package.json +++ b/Server/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "dev": "NODE_ENV=development nodemon index.js" + "dev": "NODE_ENV=development nodemon index.js", + "start": "node app.js" }, "repository": { "type": "git", @@ -34,5 +35,8 @@ }, "devDependencies": { "dotenv": "^8.2.0" + }, + "engines": { + "node": "10.x" } } From 6ef9f6f6db5b0304e8c07cd7cd5f66db5743fa01 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Mon, 9 Mar 2020 16:30:52 +0700 Subject: [PATCH 10/15] Adjust main entry point --- Server/Procfile | 2 +- Server/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/Procfile b/Server/Procfile index 207d22f8..5ec9cc2c 100644 --- a/Server/Procfile +++ b/Server/Procfile @@ -1 +1 @@ -web: node app.js \ No newline at end of file +web: node index.js \ No newline at end of file diff --git a/Server/package.json b/Server/package.json index 01c943ef..20951438 100644 --- a/Server/package.json +++ b/Server/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "dev": "NODE_ENV=development nodemon index.js", - "start": "node app.js" + "start": "node index.js" }, "repository": { "type": "git", From 2556a14deaeba00cb150952aa7d9ae09accd0aa8 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Mon, 9 Mar 2020 16:34:32 +0700 Subject: [PATCH 11/15] updated .gitignore file --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 69c566b1..1dcef2d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ node_modules -/node_modules .env \ No newline at end of file From 1540367696112bb9eb1da7423889977a5f5f03e1 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Thu, 26 Mar 2020 10:58:28 +0700 Subject: [PATCH 12/15] fix documentation --- .vscode/launch.json | 2 +- Client/index.html | 22 +++--- Client/js/login.js | 2 +- Client/js/register.js | 4 +- Client/js/request.js | 2 +- Server/Procfile | 2 +- Server/{index.js => app.js} | 8 +- Server/bcrypttest.js | 7 -- Server/controllers/ToDoController.js | 113 +++++++++++++++++++++++++++ Server/controllers/UserController.js | 75 ++++++++++++++++-- Server/middleware/Authentication.js | 6 +- Server/package.json | 4 +- Server/routers/index.js | 2 +- Server/swaggerOptions.js | 4 +- 14 files changed, 211 insertions(+), 42 deletions(-) rename Server/{index.js => app.js} (79%) delete mode 100644 Server/bcrypttest.js diff --git a/.vscode/launch.json b/.vscode/launch.json index 47362e96..3b4c2185 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/Server/index.js", + "program": "${workspaceFolder}/Server/app.js", "cwd": "${workspaceFolder}/Server" } ] diff --git a/Client/index.html b/Client/index.html index 75a45b51..0b934534 100644 --- a/Client/index.html +++ b/Client/index.html @@ -40,28 +40,28 @@ diff --git a/Client/js/login.js b/Client/js/login.js index a2bf9dde..d91a7dcf 100644 --- a/Client/js/login.js +++ b/Client/js/login.js @@ -1,7 +1,7 @@ $("#login-submit").click(function() { const email = $("#login-email").val(); const password = $("#login-password").val(); - $.ajax("http://localhost:3000/user/login", { + $.ajax("http://localhost:3000/users/login", { type: "POST", data: { email, diff --git a/Client/js/register.js b/Client/js/register.js index f9d8948d..a50395aa 100644 --- a/Client/js/register.js +++ b/Client/js/register.js @@ -1,7 +1,7 @@ $("#register-submit").click(function() { const email = $("#register-email").val(); const password = $("#register-password").val(); - $.ajax("http://localhost:3000/user/register", { + $.ajax("http://localhost:3000/users/register", { type: "POST", data: { email, @@ -13,7 +13,7 @@ $("#register-submit").click(function() { switchToLoggedIn(); showPage("todos"); }) - .fail(message => console.log(message)); + .fail(message => console.log(message, "masiukakk")); }); $("#register-generate").click(function () { diff --git a/Client/js/request.js b/Client/js/request.js index 311fff29..823a2731 100644 --- a/Client/js/request.js +++ b/Client/js/request.js @@ -12,7 +12,7 @@ function onSignIn(googleUser) { var id_token = googleUser.getAuthResponse().id_token; $.ajax({ method: 'POST', - url: `http://localhost:3000/user/googlelogin`, + url: `http://localhost:3000/users/googlelogin`, data: { token: id_token } diff --git a/Server/Procfile b/Server/Procfile index 5ec9cc2c..207d22f8 100644 --- a/Server/Procfile +++ b/Server/Procfile @@ -1 +1 @@ -web: node index.js \ No newline at end of file +web: node app.js \ No newline at end of file diff --git a/Server/index.js b/Server/app.js similarity index 79% rename from Server/index.js rename to Server/app.js index c9130759..e125a4d5 100644 --- a/Server/index.js +++ b/Server/app.js @@ -7,13 +7,11 @@ const morganChalk = require('./morganChalk'); const router = require('./routers'); const ErrorHandler = require('./middleware/ErrorHandler'); const dotenv = require('dotenv'); -const path = require('path'); -//const cors = require("cors"); +const cors = require("cors"); dotenv.config(); -//app.use(cors()); -app.use('/static', express.static(path.join(__dirname, 'public'))) -app.use('/', express.static(path.join(__dirname, '../Client'))) +app.use(cors()); + app.use(morganChalk); app.use(express.urlencoded({extended:true})) app.use(express.json()); diff --git a/Server/bcrypttest.js b/Server/bcrypttest.js deleted file mode 100644 index 42804ac6..00000000 --- a/Server/bcrypttest.js +++ /dev/null @@ -1,7 +0,0 @@ -const Bcrypt = require("./helpers/bcrypt"); - -const password = "marsupilami"; - -const hashed = Bcrypt.hash(password); - -console.log(hashed, Bcrypt.compare(password, hashed)); \ No newline at end of file diff --git a/Server/controllers/ToDoController.js b/Server/controllers/ToDoController.js index a3c5c1c4..085e99b1 100644 --- a/Server/controllers/ToDoController.js +++ b/Server/controllers/ToDoController.js @@ -8,6 +8,15 @@ const createError = require("http-errors"); * name: ToDo * description: ToDo management */ + +/** + * @swagger + * components: + * securitySchemes: + * BearerAuth: + * type: http + * scheme: bearer + */ class ToDoController{ /** * @swagger @@ -16,6 +25,8 @@ class ToDoController{ * get: * summary: Get all todos * tags: [ToDo] + * security: + * - BearerAuth: [] * responses: * "200": * description: Array of ToDo @@ -37,10 +48,58 @@ class ToDoController{ .then(data => res.status(200).json(data)) .catch(err => next(err)); } + /** + * @swagger + * path: + * /todos/{id}: + * get: + * summary: Get todo by id + * tags: [ToDo] + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the todo + * security: + * - BearerAuth: [] + * responses: + * "200": + * description: Object of ToDo + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + * + */ static getTodo(req, res, next){ const todo = req.todo; res.status(200).json(todo); } + + /** + * @swagger + * path: + * /todos/: + * post: + * summary: Create todo + * tags: [ToDo] + * responses: + * "201": + * description: Object of ToDo + * content: + * application/json: + * schema: + * type: object + * items: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + * + */ static addTodo(req, res, next){ const title = req.body.title; const description = req.body.description; @@ -57,6 +116,33 @@ class ToDoController{ .then(data => res.status(201).json(data)) .catch(err => next(err)); } + /** + * @swagger + * path: + * /todos/{id}: + * put: + * summary: Update todo by id + * tags: [ToDo] + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Update from numeric ID of the todo + * security: + * - BearerAuth: [] + * responses: + * "200": + * description: Object of ToDo + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + * + */ static updateTodo(req, res){ const todo = req.todo; todo.title = req.body.title; @@ -69,6 +155,33 @@ class ToDoController{ next(err); }); } + /** + * @swagger + * path: + * /todos/{id}: + * delete: + * summary: Delete todo by id + * tags: [ToDo] + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Delete from numeric ID of the todo + * security: + * - BearerAuth: [] + * responses: + * "200": + * description: Object of ToDo + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ToDo" + * "500": + * description: Internal Server Error + * + */ static deleteTodo(req, res){ const todo = req.todo; todo.destroy() diff --git a/Server/controllers/UserController.js b/Server/controllers/UserController.js index d8c184a0..e2426968 100644 --- a/Server/controllers/UserController.js +++ b/Server/controllers/UserController.js @@ -11,23 +11,50 @@ const client = new OAuth2Client(process.env.CLIENT_ID); * name: ToDo * description: ToDo management */ +/** + * @swagger + * components: + * schemas: + * AccessToken: + * type: object + * required: + * - jwt + * properties: + * jwt: + * type: string + * description: JWT token + * example: + * jwt: eyHJSDjkhjkHKASHKJAHSKJsahjSJKHAKJh + */ class UserController{ - /** + /** * @swagger * path: * /users/register: - * get: - * summary: Get all todos - * tags: [ToDo] + * post: + * summary: Register new account + * tags: [User] + * requestBody: + * required: true + * content: + * application/x-www-form-urlencoded: + * schema: + * type: object + * properties: + * email: + * type: string + * password: + * type: string + * required: + * - name + * - email * responses: * "200": - * description: Array of ToDo + * description: Object containing jwt * content: * application/json: * schema: - * type: array - * items: - * $ref: "#/components/schemas/ToDo" + * $ref: '#/components/schemas/AccessToken' * "500": * description: Internal Server Error * @@ -45,6 +72,38 @@ class UserController{ }) .catch(next); } + /** + * @swagger + * path: + * /users/login: + * post: + * summary: Login using email/password + * tags: [User] + * requestBody: + * required: true + * content: + * application/x-www-form-urlencoded: + * schema: + * type: object + * properties: + * email: + * type: string + * password: + * type: string + * required: + * - name + * - email + * responses: + * "200": + * description: Object containing jwt + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/AccessToken' + * "500": + * description: Internal Server Error + * + */ static login(req, res, next){ const email = req.body.email; const password = req.body.password; diff --git a/Server/middleware/Authentication.js b/Server/middleware/Authentication.js index 4fb045c0..590b83b9 100644 --- a/Server/middleware/Authentication.js +++ b/Server/middleware/Authentication.js @@ -5,7 +5,11 @@ const User = models.User; module.exports = (req, res, next) => { let decodedToken; try { - const token = req.headers.authorization; + let token = req.headers.authorization; + const split = token.split(' '); + if (split.length == 2) { + token = split[1]; + } decodedToken = jwt.verify(token); } catch { next ({ diff --git a/Server/package.json b/Server/package.json index 20951438..845a2e2f 100644 --- a/Server/package.json +++ b/Server/package.json @@ -4,8 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "dev": "NODE_ENV=development nodemon index.js", - "start": "node index.js" + "dev": "NODE_ENV=development nodemon app.js", + "start": "node app.js" }, "repository": { "type": "git", diff --git a/Server/routers/index.js b/Server/routers/index.js index 8234c5cc..417c51c5 100644 --- a/Server/routers/index.js +++ b/Server/routers/index.js @@ -6,7 +6,7 @@ const passwordWolfRouter = require("./passwordWolfRouter.js"); const Authentication = require("../middleware/Authentication.js"); router.use('/todos', Authentication, taskRouter); -router.use('/user', userRouter); +router.use('/users', userRouter); router.use('/passwordWolf', passwordWolfRouter); module.exports = router; \ No newline at end of file diff --git a/Server/swaggerOptions.js b/Server/swaggerOptions.js index 464cd6f2..58fab3cd 100644 --- a/Server/swaggerOptions.js +++ b/Server/swaggerOptions.js @@ -20,6 +20,8 @@ module.exports = { }, apis: [ "./models/todo.js", - "./controllers/ToDoController.js" + "./models/user.js", + "./controllers/ToDoController.js", + "./controllers/UserController.js" ] }; \ No newline at end of file From 804dc4c4fba25670d15f658d24be09793e1bbc07 Mon Sep 17 00:00:00 2001 From: Rofandi Date: Thu, 26 Mar 2020 20:23:52 +0700 Subject: [PATCH 13/15] change to new look --- Client/index.html | 2 +- Client/js/todos.js | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Client/index.html b/Client/index.html index 0b934534..3a9c3144 100644 --- a/Client/index.html +++ b/Client/index.html @@ -66,7 +66,7 @@

    Login

    diff --git a/Client/js/todos.js b/Client/js/todos.js index fc6d3e47..13e17978 100644 --- a/Client/js/todos.js +++ b/Client/js/todos.js @@ -9,12 +9,14 @@ function loadTodos() { $("#todos-list").html(""); for (let i = 0; i < todos.length; i++) { $("#todos-list").append(` -
  • - ${todos[i].title}
    - ${todos[i].description} -
  • - - +
    +
    +
    ${todos[i].title}
    +

    ${todos[i].description}

    + Edit + Delete +
    +
    `); $(`#todos-edit-${i}`).click(function () { const id = todos[i].id; From 2179f2ce941d16e0add8d5435488ba48671b5ba5 Mon Sep 17 00:00:00 2001 From: Rofandi <33696179+Rofandi@users.noreply.github.com> Date: Sat, 29 May 2021 14:33:20 +0700 Subject: [PATCH 14/15] Create README.md --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..49072eed --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Fancy TODO + +## How to run Server + +``` +cd Server +npm i +npm run start +``` + +## How to run Client + +``` +cd Client +npm i -g parcel +parcel index.html +``` + +## How to test App + +Open in browser: `http://localhost:1234` From 51a40ea0a89af6c4a382c89cd965c91d48e6cc54 Mon Sep 17 00:00:00 2001 From: Rofandi <33696179+Rofandi@users.noreply.github.com> Date: Sat, 29 May 2021 14:38:23 +0700 Subject: [PATCH 15/15] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 49072eed..ae602407 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,18 @@ # Fancy TODO +## How to setup database + +1. Install PostgreSQL +2. Create new database named FancyToDo +3. Create `.env` file: + + ``` + DB_USER=yourpostgresusername + DB_PASSWORD=yourpassword + ``` + +4. Execute: `npx sequelize-cli db:migrate` + ## How to run Server ```