Add custom domain support
This commit is contained in:
44
client/middleware/custom-domain.global.js
vendored
44
client/middleware/custom-domain.global.js
vendored
@@ -1,16 +1,42 @@
|
||||
import opnformConfig from "~/opnform.config.js";
|
||||
import {customDomainUsed, getDomain, getHost} from "~/lib/utils.js";
|
||||
|
||||
function getDomain (url) {
|
||||
return (new URL(url)).hostname
|
||||
/**
|
||||
* Added by Caddy when proxying to the app
|
||||
* @type {string}
|
||||
*/
|
||||
const customDomainHeaderName = 'CUSTOM_DOMAIN_HEADER'
|
||||
|
||||
/**
|
||||
* List of routes that can be used with a custom domain
|
||||
* @type {string[]}
|
||||
*/
|
||||
const customDomainAllowedRoutes = ['forms-slug']
|
||||
|
||||
function redirectToMainDomain() {
|
||||
return navigateTo(useRuntimeConfig().public.appUrl + '?utm_source=failed_custom_domain_redirect', { redirectCode: 301, external: true })
|
||||
}
|
||||
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
if (opnformConfig.custom_domains_enabled && process.client) {
|
||||
const isCustomDomain = getDomain(window.location.href) !== getDomain(opnformConfig.app_url)
|
||||
if (isCustomDomain && !['forms.show_public'].includes(to.name)) {
|
||||
// If route isn't a public form, redirect
|
||||
return navigateTo({name: 'home',query: {utm_source: 'failed_custom_domain_redirect'}});
|
||||
}
|
||||
if (process.client) return
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
if (!customDomainUsed()) return
|
||||
|
||||
const customDomainHeaderValue = useRequestHeaders()[customDomainHeaderName]
|
||||
if (!customDomainHeaderValue || customDomainHeaderValue !== getDomain(getHost())) {
|
||||
// If custom domain header doesn't match, redirect
|
||||
return redirectToMainDomain()
|
||||
}
|
||||
|
||||
if (!config.public.customDomainsEnabled) {
|
||||
// If custom domain not allowed, redirect
|
||||
return redirectToMainDomain()
|
||||
}
|
||||
|
||||
if (!customDomainAllowedRoutes.includes(to.name)) {
|
||||
// Custom domain only allowed for form url
|
||||
return redirectToMainDomain()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user