Skip to content

Commit

Permalink
Merge pull request #100 from openstad/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
LorenzoJokhan authored Oct 23, 2023
2 parents 474019c + 4e660e2 commit 1603865
Show file tree
Hide file tree
Showing 24 changed files with 1,757 additions and 8,401 deletions.
2 changes: 0 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ app.use((req, res, next) => {
next();
});

const dbExists = require('./services/mongo').dbExists;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '200mb' }));
app.use(bodyMw.parseBoolean);
Expand Down
59 changes: 31 additions & 28 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
const apiUrl = process.env.API_URL;
const siteId = process.env.SITE_ID;
const rp = require('request-promise');
const fetch = require('node-fetch');

const fetchUserData = async(req, res, next) => {

const fetchUserData = (req, res, next) => {
const jwt = req.query.jwt ? req.query.jwt : req.session.jwt;

if (!jwt) {
next();
} else {

const thisHost = req.headers['x-forwarded-host'] || req.get('host');
const fullUrl = req.protocol + '://' + thisHost;

const options = {
uri: `${apiUrl}/oauth/site/${siteId}/me`,
try {
let response = await fetch(`${apiUrl}/oauth/site/${siteId}/me`, {
headers: {
'Accept': 'application/json',
"X-Authorization" : `Bearer ${jwt}`,
"Cache-Control": "no-cache"
},
json: true // Automatically parses the JSON string in the response
}

rp(options)
.then(function (user) {

if (user) {
req.user = user
res.locals.user = user;
next();
} else {
// if not valid clear the JWT and redirect
req.session.jwt = '';

req.session.save(() => {
res.redirect('/');
return;
})
}
method: 'GET',
})
.catch((e) => {
// if not valid clear the JWT and redirect
if (!response.ok) {
console.log(response);
throw new Error('Fetch failed')
}

let user = await response.json();

if (user) {
req.user = user
res.locals.user = user;
return next();
} else {
req.session.jwt = '';

req.session.save(() => {
res.redirect('/');
return;
return res.redirect('/');
})
});
}

} catch(err) {
// if not valid clear the JWT and redirect
req.session.jwt = '';
req.session.save(() => {
res.redirect('/');
return;
})
}

}
}

Expand Down
74 changes: 39 additions & 35 deletions middleware/idea.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
const rp = require('request-promise');
const fetch = require('node-fetch');
const apiUrl = process.env.API_URL;
const siteApiKey = process.env.SITE_API_KEY;

exports.allForSite = (req, res, next) => {
var options = {
uri: `${apiUrl}/api/site/${req.params.siteId}/idea?includeUser=1&includeVoteCount=1&includeUserVote=1`,
exports.allForSite = async(req, res, next) => {

try {
let response = await fetch(`${apiUrl}/api/site/${req.params.siteId}/idea?includeUser=1&includeVoteCount=1&includeUserVote=1`, {
headers: {
'Accept': 'application/json',
"X-Authorization": siteApiKey
'Accept': 'application/json',
"X-Authorization": siteApiKey
},
json: true // Automatically parses the JSON string in the response
};


rp(options)
.then(function (ideas) {
req.ideas = ideas;
res.locals.ideas = ideas;
next();
method: 'GET',
})
.catch(function (err) {
next();
});
if (!response.ok) {
console.log(response);
throw new Error('Fetch failed')
}
let ideas = await response.json();
req.ideas = ideas;
res.locals.ideas = ideas;
return next();
} catch(err) {
return next();
}

}

exports.oneForSite = (req, res, next) => {
var options = {
uri: `${apiUrl}/api/site/${req.params.siteId}/idea/${req.params.ideaId}?includeUser=1&includeVoteCount=1&includeUserVote=1`,
exports.oneForSite = async(req, res, next) => {

try {
let response = await fetch(`${apiUrl}/api/site/${req.params.siteId}/idea/${req.params.ideaId}?includeUser=1&includeVoteCount=1&includeUserVote=1`, {
headers: {
'Accept': 'application/json',
"X-Authorization": siteApiKey
// "Authorization" : auth
'Accept': 'application/json',
"X-Authorization": siteApiKey
},
json: true // Automatically parses the JSON string in the response
};

rp(options)
.then(function (idea) {
req.idea = idea;
res.locals.idea = idea;
next();
method: 'GET',
})
.catch(function (err) {
next();
});
if (!response.ok) {
console.log(response);
throw new Error('Fetch failed')
}
let idea = await response.json();
req.idea = idea;
res.locals.idea = idea;
return next();
} catch(err) {
return next();
}

}
48 changes: 27 additions & 21 deletions middleware/vote.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const rp = require('request-promise');
const fetch = require('node-fetch');
const apiUrl = process.env.API_URL;
const cache = require('../services/cache').cache;
const cacheLifespan = 10*60; // set lifespan of 10 minutes;

exports.allForSite = (req, res, next) => {
exports.allForSite = async (req, res, next) => {

let voteKey = 'votesForSite' + req.params.siteId;
let votesForSite = cache.get(voteKey);

Expand All @@ -12,27 +13,32 @@ exports.allForSite = (req, res, next) => {
res.locals.votes = votesForSite;
next();
} else {
var options = {
uri: `${apiUrl}/api/site/${req.params.siteId}/vote`,

try {
let response = await fetch(`${apiUrl}/api/site/${req.params.siteId}/vote`, {
headers: {
'Accept': 'application/json',
"X-Authorization": process.env.SITE_API_KEY
"X-Authorization": process.env.SITE_API_KEY,
},
json: true // Automatically parses the JSON string in the response
};

rp(options)
.then(function (votes) {
const allVotes = votes;
const userVotedCount = votes;
req.votes = allVotes;
res.locals.votes = allVotes;
cache.set(voteKey, allVotes, { life: cacheLifespan });
next();
method: 'GET',
})
.catch(function (err) {
console.log('-->>>> err', err);
next();
});
if (!response.ok) {
console.log(response);
throw new Error('Fetch failed')
}

let votes = await response.json();

const allVotes = votes;
const userVotedCount = votes;
req.votes = allVotes;
res.locals.votes = allVotes;
cache.set(voteKey, allVotes, { life: cacheLifespan });
return next();

} catch(err) {
console.log(err);
return next(err)
}
}

}
Loading

3 comments on commit 1603865

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Published new image: openstad/admin:master-1603865

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Published new image: openstad/admin:development-1603865

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Published new image: openstad/admin:release-wolkenstad-1603865

Please sign in to comment.