diff --git a/index.js b/index.js index 97066ee..eacb2fb 100644 --- a/index.js +++ b/index.js @@ -65,13 +65,21 @@ DB.getDB.then((DB) => { // route for Home-Page app.get('/', sessionChecker, async (req, res) => { - if (req.userInfo) return res.json({ - status: "ok", - userInfo: req.userInfo, - profileInfo: req.profileInfo - }); - // This should not happend, since the sessionChecker should redirect to login - res.json({ status: "ok" }); + try { + const userInfo = req.userInfo; + if(!userInfo) { + // This should not happend, since the sessionChecker should redirect to login + return res.status(401).json({ status: "Unauthorized" }); + } + return res.json({ + status: "ok", + userInfo: req.userInfo, + profileInfo: req.profileInfo + }); + } catch (error) { + console.error("Error processing / path", error); + return res.status(500).json({ status: "Internal server error" }); + } }); // Check for an invitation for an email @@ -136,12 +144,24 @@ DB.getDB.then((DB) => { // This is the endpoint to refresh the push notification token app.post('/token/', sessionChecker, async (req, res) => { - const profileid = getProfileId(req); - let token = req.body.token - DB.setProfileToken(profileid, token); - return res.json({ - status: "ok" - }); + try { + const profileid = getProfileId(req); + const { token } = req.body; + // Validate token presence + if (!token) { + return res.status(400).json({ status: 'Token is required' }); + } + // Set the token in the database and wait for completion + const result = await DB.setProfileToken(profileid, token); + // Assuming DB.setProfileToken() could fail, handle it appropriately + if (!result) { + return res.status(500).json({ status: 'Failed to update token' }); + } + return res.json({ status: 'ok' }); + } catch (error) { + console.error('Error setting profile token:', error); + return res.status(500).json({ status: 'Internal server error' }); + } }); // Used for webpush notifications diff --git a/translations/en.json b/translations/en.json new file mode 100644 index 0000000..8ceb0a1 --- /dev/null +++ b/translations/en.json @@ -0,0 +1,5 @@ +{ + "hello": "Hello", + "new_password": "This is your new password: {{password}}", + "login_link": "Log in here" +} \ No newline at end of file diff --git a/translations/translations.js b/translations/translations.js new file mode 100644 index 0000000..704040e --- /dev/null +++ b/translations/translations.js @@ -0,0 +1,15 @@ +const path = require('path'); +const fs = require('fs'); + +const loadTranslation = (languageCode) => { + try { + const filePath = path.join(__dirname, 'translations', `${languageCode}.json`); + return JSON.parse(fs.readFileSync(filePath, 'utf8')); + } catch (error) { + console.error('Error loading translation:', error); + if(languageCode !== 'en') { + return loadTranslation('en'); // Fallback to English if the specific language file is missing + } + return {}; // Fallback to empty object if language file is missing + } +}; \ No newline at end of file