mirror of
https://github.com/gnmyt/myspeed.git
synced 2026-04-25 22:22:23 -05:00
feat: add authorization to opengraph image
This commit is contained in:
@@ -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;
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user