2024-01-12 14:43:28 +00:00
|
|
|
import {customDomainUsed, getDomain, getHost} from "~/lib/utils.js";
|
2023-12-16 18:21:03 +00:00
|
|
|
|
2024-01-12 14:43:28 +00:00
|
|
|
/**
|
|
|
|
* Added by Caddy when proxying to the app
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2024-01-16 09:54:12 +00:00
|
|
|
const customDomainHeaderName = 'user-custom-domain'
|
2024-01-12 14:43:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of routes that can be used with a custom domain
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
const customDomainAllowedRoutes = ['forms-slug']
|
|
|
|
|
2024-01-16 14:31:14 +00:00
|
|
|
function redirectToMainDomain(reason = 'unknown') {
|
|
|
|
console.warn('Redirecting to main domain', { reason })
|
2024-01-12 14:43:28 +00:00
|
|
|
return navigateTo(useRuntimeConfig().public.appUrl + '?utm_source=failed_custom_domain_redirect', { redirectCode: 301, external: true })
|
2023-12-16 18:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defineNuxtRouteMiddleware((to, from) => {
|
2024-01-16 10:33:38 +00:00
|
|
|
if (!customDomainUsed()) return
|
2024-01-12 14:43:28 +00:00
|
|
|
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
|
|
const customDomainHeaderValue = useRequestHeaders()[customDomainHeaderName]
|
|
|
|
if (!customDomainHeaderValue || customDomainHeaderValue !== getDomain(getHost())) {
|
|
|
|
// If custom domain header doesn't match, redirect
|
2024-01-16 10:33:38 +00:00
|
|
|
console.error('Custom domain header does not match, redirecting',{
|
2024-01-15 15:49:24 +00:00
|
|
|
'customDomainHeaderValue': customDomainHeaderValue,
|
|
|
|
'host': getDomain(getHost()),
|
|
|
|
})
|
2024-01-16 14:31:14 +00:00
|
|
|
return redirectToMainDomain('header_mismatch')
|
2024-01-12 14:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.public.customDomainsEnabled) {
|
|
|
|
// If custom domain not allowed, redirect
|
2024-01-16 14:31:14 +00:00
|
|
|
return redirectToMainDomain('custom_domains_disabled')
|
2024-01-12 14:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!customDomainAllowedRoutes.includes(to.name)) {
|
|
|
|
// Custom domain only allowed for form url
|
2024-01-16 14:31:14 +00:00
|
|
|
return redirectToMainDomain('route_not_allowed')
|
2023-12-16 18:21:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|