diff --git a/api/subscriptions.js b/api/subscriptions.js index c6a8c33..6066fea 100644 --- a/api/subscriptions.js +++ b/api/subscriptions.js @@ -9,12 +9,24 @@ mongoose.connect("mongodb://localhost:27017/vinlottis", { useNewUrlParser: true }); -const config = require(path.join(__dirname + "/../config/env/push.config")); +const config = require(path.join(__dirname + "/../config/env/push")); const Subscription = require(path.join(__dirname + "/../schemas/Subscription")); const lotteryConfig = require(path.join( __dirname + "/../config/env/lottery.config" )); +router.use((req, res, next) => { + next(); +}); + +if (!config.publicKey) { + console.error( + "You are missing the push-setup! Server will continue running even without this." + ); + module.exports = router; + return; +} + const vapidKeys = { publicKey: config.publicKey, privateKey: config.privateKey @@ -34,10 +46,6 @@ const sendNotification = (subscription, dataToSend = "") => { } }; -router.use((req, res, next) => { - next(); -}); - router.route("/save-subscription").post(async (req, res) => { const subscription = req.body; await saveToDatabase(subscription); //Method to save the subscription to Database diff --git a/config/env/push.js b/config/env/push.js new file mode 100644 index 0000000..ac90471 --- /dev/null +++ b/config/env/push.js @@ -0,0 +1,8 @@ +try { + module.exports = require("./push.config"); +} catch (e) { + console.error( + "You haven't defined push-parameters, you sure you want to continue without them?" + ); + module.exports = { publicKey: false, privateKey: false, mailto: false }; +} diff --git a/config/service-worker.config.js b/config/service-worker.config.js index 12e2b40..34533bd 100644 --- a/config/service-worker.config.js +++ b/config/service-worker.config.js @@ -42,7 +42,7 @@ const ServiceWorkerConfig = { plugins: [ new webpack.DefinePlugin({ __DATE__: new Date().getTime(), - __PUBLICKEY__: JSON.stringify(require("./env/push.config").publicKey) + __PUBLICKEY__: JSON.stringify(require("./env/push").publicKey) }) ] }; diff --git a/config/webpack.config.common.js b/config/webpack.config.common.js index dcd817f..711bd56 100644 --- a/config/webpack.config.common.js +++ b/config/webpack.config.common.js @@ -78,7 +78,8 @@ const webpackConfig = function(isDev) { __PRICE__: env.price, __MESSAGE__: JSON.stringify(env.message), __DATE__: env.date, - __HOURS__: env.hours + __HOURS__: env.hours, + __PUSHENABLED__: JSON.stringify(require("./env/push") != false) }) ] }; diff --git a/src/mixins/ServiceWorkerMixin.js b/src/mixins/ServiceWorkerMixin.js index 51f4e3e..c7a64d9 100644 --- a/src/mixins/ServiceWorkerMixin.js +++ b/src/mixins/ServiceWorkerMixin.js @@ -4,7 +4,7 @@ var serviceWorkerRegistrationMixin = { console.log("Nettleseren din støtter ikke service-workers."); return; } - if ("PushManager" in window) { + if ("PushManager" in window && __PUSHENABLED__) { if (Notification.permission !== "granted") { localStorage.removeItem("push"); } diff --git a/src/service-worker/service-worker.js b/src/service-worker/service-worker.js index 22a0d02..37ac4c0 100644 --- a/src/service-worker/service-worker.js +++ b/src/service-worker/service-worker.js @@ -15,6 +15,9 @@ self.addEventListener("activate", event => { }); self.addEventListener("message", event => { + if (!__PUBLICKEY__) { + return; + } if (event.data === "updatePush") { event.waitUntil( new Promise((resolve, reject) => {