diff --git a/public/css/redirect.css b/public/css/redirect.css new file mode 100644 index 0000000..08186fd --- /dev/null +++ b/public/css/redirect.css @@ -0,0 +1,75 @@ +.redirect-container { + display: flex; + justify-content: center; + align-items: center; + padding: 2rem; +} + +.redirect-content { + text-align: center; + background: #ffffff; + padding: 3rem 2rem; + border-radius: 6px; + box-shadow: 0 2px 6px rgba(0,0,0,0.05); + max-width: 500px; + width: 100%; +} + +.spinner { + width: 40px; + height: 40px; + border: 4px solid #e9ecef; + border-top: 4px solid #2c3e50; + border-radius: 50%; + animation: spin 1s linear infinite; + margin: 0 auto 1.5rem; +} + +@keyframes spin { + 0% { transform: rotate(0deg); } + 100% { transform: rotate(360deg); } +} + +h1 { + font-size: 1.5rem; + font-weight: 600; + margin-bottom: 1rem; + color: #2c3e50; +} + +p { + font-size: 1rem; + color: #444; + margin-bottom: 1rem; +} + +.redirect-link { + color: #2c3e50; + text-decoration: none; + font-weight: 600; + padding: 0.5rem 1rem; + border: 2px solid #2c3e50; + border-radius: 4px; + display: inline-block; + transition: all 0.2s ease-in-out; +} + +.redirect-link:hover { + background-color: #2c3e50; + color: #ffffff; + text-decoration: none; +} + +@media (max-width: 600px) { + .redirect-content { + padding: 2rem 1rem; + } + + h1 { + font-size: 1.3rem; + } + + p { + font-size: 0.9rem; + } +} diff --git a/public/js/redirect.js b/public/js/redirect.js new file mode 100644 index 0000000..6ca98f9 --- /dev/null +++ b/public/js/redirect.js @@ -0,0 +1,12 @@ +document.addEventListener("DOMContentLoaded", function () { + const container = document.querySelector(".redirect-container"); + if (!container) return; + + const url = container.getAttribute("data-redirect-url"); + if (!url) return; + + // Redirect methods + setTimeout(() => (window.location.href = url), 100); + setTimeout(() => window.location.replace(url), 1000); + setTimeout(() => (document.location = url), 2000); +}); diff --git a/src/middleware/index.js b/src/middleware/index.js index 6ff00b6..393e2ab 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -12,6 +12,7 @@ const baseContext = require("./baseContext"); const hbs = require("./hbs"); const authCheck = require("./authCheck"); +const { redirectMiddleware } = require("./redirect"); const { loggingMiddleware, @@ -86,6 +87,7 @@ app.use(compression()); app.use(validateRequestIntegrity); app.use(formatHtml); + app.use(redirectMiddleware); app.use(routes); app.use(errorHandler); return app; diff --git a/src/middleware/redirect.js b/src/middleware/redirect.js new file mode 100644 index 0000000..da79b8d --- /dev/null +++ b/src/middleware/redirect.js @@ -0,0 +1,68 @@ +// src/middleware/redirect.js + +const { baseUrl } = require("../utils/baseUrl"); + +// Configuration - adjust these as needed +const redirectConfig = { + "/": `${baseUrl}/blog`, + // Add more redirects as needed + // '/old-path': '/new-path', +}; + +// Helper function to build full URL +function buildRedirectUrl(req, targetPath) { + const protocol = req.get("x-forwarded-proto") || req.protocol; + const host = req.get("host"); + + // If targetPath is already a full URL, return it as-is + if (targetPath.startsWith("http")) { + return targetPath; + } + + // Build full URL + return `${protocol}://${host}${targetPath}`; +} + +// Generic redirect handler +function handleRedirect(req, res, targetPath) { + const redirectUrl = buildRedirectUrl(req, targetPath); + + // Log the redirect for debugging + console.log(`[REDIRECT] ${req.originalUrl} -> ${redirectUrl}`); + + // Check if this is a request that expects JSON (API calls) + if (req.accepts("json") && !req.accepts("html")) { + return res.status(301).json({ + redirect: true, + url: redirectUrl, + }); + } + + res.set("Location", redirectUrl); + + // For browsers, render the redirect page + res.status(301); + res.renderWithBaseContext("pages/redirect", { + redirectUrl: redirectUrl, + originalUrl: req.originalUrl, + }); +} + +// Middleware function to check for redirects +function redirectMiddleware(req, res, next) { + const targetPath = redirectConfig[req.path]; + + if (targetPath) { + return handleRedirect(req, res, targetPath); + } + + // No redirect needed, continue to next middleware + next(); +} + +// Export the middleware and utility functions +module.exports = { + redirectMiddleware, + handleRedirect, + redirectConfig, +}; diff --git a/src/routes/index.js b/src/routes/index.js index d1bc2c3..40d1d4f 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -109,7 +109,8 @@ router.get("/", (req, res) => { console.log(qualifyLink("/blog")); - res.redirect(301, qualifyLink("/blog")); + // res.redirect(301, qualifyLink("/blog")); + res.redirect(301, "/blog"); }); router.use( diff --git a/src/views/layouts/main.handlebars b/src/views/layouts/main.handlebars index fff88fa..48d2394 100644 --- a/src/views/layouts/main.handlebars +++ b/src/views/layouts/main.handlebars @@ -15,6 +15,7 @@ + {{{_sections.headers}}} diff --git a/src/views/pages/authRedirect.handlebars b/src/views/pages/authRedirect.handlebars new file mode 100644 index 0000000..23bb188 --- /dev/null +++ b/src/views/pages/authRedirect.handlebars @@ -0,0 +1,5 @@ +
Please wait while we redirect you to the authentication service.
+If you are not redirected automatically, click here.
+Please wait while we redirect you to the authentication service.
-If you are not redirected automatically, click here.
+{{#section "headers"}} + + +{{/section}} +{{#section "styles"}} + +{{/section}} +{{#section " scripts"}} + +{{/section}} +