feat: add authorization to opengraph image

This commit is contained in:
AugusDogus
2024-06-23 12:46:55 -05:00
parent b844901f75
commit 582f32a24b
2 changed files with 31 additions and 1 deletions
+25
View File
@@ -0,0 +1,25 @@
const passwordMiddleware = require('./password');
const passwordWrapper = (allowViewAccess, customResponseHandler) => async (req, res, next) => {
// Intercept the response send method
const originalSend = res.send.bind(res);
res.send = function (body) {
// Check if the status code is 401 and a custom response handler is provided
if (res.statusCode === 401 && typeof customResponseHandler === 'function') {
// The password middleware has returned a 401 status code, call the custom response handler
return customResponseHandler(req, res);
}
// Call the original send method for other statuses
return originalSend(body);
};
try {
// Execute the original password middleware
await passwordMiddleware(allowViewAccess)(req, res, next);
} catch (err) {
next(err);
}
};
module.exports = passwordWrapper;
+6 -1
View File
@@ -1,8 +1,13 @@
const express = require("express");
const app = express.Router();
const passwordWrapper = require('../middlewares/passwordWrapper');
const generateOpenGraphImage = require("../controller/opengraph");
app.get("/image", async (req, res) => {
app.get("/image", passwordWrapper(false, (req, res) => {
// If there is a password set and the user does not want others to view their test data, return the project banner
res.redirect('https://repository-images.githubusercontent.com/478222232/b5331514-aa27-4a56-af3e-c4b25446438d');
}), async (req, res) => {
try {
const png = await generateOpenGraphImage(req);