Not crashing when not supplying push-data thingies

This commit is contained in:
Kasper Rynning-Tønnesen
2020-03-02 13:33:44 +01:00
parent 45c994d473
commit 6589425e54
6 changed files with 28 additions and 8 deletions

View File

@@ -9,12 +9,24 @@ mongoose.connect("mongodb://localhost:27017/vinlottis", {
useNewUrlParser: true 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 Subscription = require(path.join(__dirname + "/../schemas/Subscription"));
const lotteryConfig = require(path.join( const lotteryConfig = require(path.join(
__dirname + "/../config/env/lottery.config" __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 = { const vapidKeys = {
publicKey: config.publicKey, publicKey: config.publicKey,
privateKey: config.privateKey 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) => { router.route("/save-subscription").post(async (req, res) => {
const subscription = req.body; const subscription = req.body;
await saveToDatabase(subscription); //Method to save the subscription to Database await saveToDatabase(subscription); //Method to save the subscription to Database

8
config/env/push.js vendored Normal file
View File

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

View File

@@ -42,7 +42,7 @@ const ServiceWorkerConfig = {
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__DATE__: new Date().getTime(), __DATE__: new Date().getTime(),
__PUBLICKEY__: JSON.stringify(require("./env/push.config").publicKey) __PUBLICKEY__: JSON.stringify(require("./env/push").publicKey)
}) })
] ]
}; };

View File

@@ -78,7 +78,8 @@ const webpackConfig = function(isDev) {
__PRICE__: env.price, __PRICE__: env.price,
__MESSAGE__: JSON.stringify(env.message), __MESSAGE__: JSON.stringify(env.message),
__DATE__: env.date, __DATE__: env.date,
__HOURS__: env.hours __HOURS__: env.hours,
__PUSHENABLED__: JSON.stringify(require("./env/push") != false)
}) })
] ]
}; };

View File

@@ -4,7 +4,7 @@ var serviceWorkerRegistrationMixin = {
console.log("Nettleseren din støtter ikke service-workers."); console.log("Nettleseren din støtter ikke service-workers.");
return; return;
} }
if ("PushManager" in window) { if ("PushManager" in window && __PUSHENABLED__) {
if (Notification.permission !== "granted") { if (Notification.permission !== "granted") {
localStorage.removeItem("push"); localStorage.removeItem("push");
} }

View File

@@ -15,6 +15,9 @@ self.addEventListener("activate", event => {
}); });
self.addEventListener("message", event => { self.addEventListener("message", event => {
if (!__PUBLICKEY__) {
return;
}
if (event.data === "updatePush") { if (event.data === "updatePush") {
event.waitUntil( event.waitUntil(
new Promise((resolve, reject) => { new Promise((resolve, reject) => {