Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 1x 1x 11x 11x 11x 1x 10x 10x 10x 10x 10x 3x 7x 1x 6x 6x 1x 5x 5x 1x 4x 1x 3x 3x 1x 2x 2x 2x 2x 8x 1x 3x 3x 3x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 6x 6x 6x 1x 5x 5x 5x 5x 1x 4x 1x 3x 1x 2x 2x 2x 2x 2x 1x 2x 3x | // Pro Website Domain Routes - Custom domain verification and management
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import type { Dal } from "../../../dal";
import { BadRequestError, NotFoundError } from "../../../lib/errors";
import { error, handleError, success } from "../../../lib/response";
import { requireProAccess } from "../../../middleware";
import type { Services } from "../../../services";
import {
resolveEnvironment,
getProjectName,
} from "../../../lib/domain-utils";
import { verifyExternalDnsCname } from "../../../lib/cloudflare";
type Env = {
Bindings: CloudflareBindings;
Variables: {
user: { id: string; name: string; email: string } | null;
session: unknown;
dal: Dal;
services: Services;
proId: string;
proRole: string;
};
};
const domainRoutes = new Hono<Env>();
// Set custom domain
const setCustomDomainSchema = z.object({
domain: z.string().min(3).max(253),
});
domainRoutes.put(
"/:proId/website/custom-domain",
requireProAccess,
zValidator("json", setCustomDomainSchema),
async (c) => {
try {
const proRole = c.get("proRole");
if (proRole !== "owner" && proRole !== "admin") {
return error(c, "FORBIDDEN", "Only owners can manage website settings", 403);
}
const dal = c.get("dal");
const proId = c.get("proId");
const { domain: rawDomain } = c.req.valid("json");
// Normalize: lowercase, trim whitespace
const domain = rawDomain.toLowerCase().trim();
// Validate domain format
if (
domain.includes("://") ||
domain.includes("/") ||
domain.includes(" ")
) {
throw new BadRequestError(
"Invalid domain format. Enter the domain without protocol or path (e.g., www.example.com)",
);
}
if (domain.endsWith(".decorrocket.com")) {
throw new BadRequestError(
"Cannot use a decorrocket.com subdomain as a custom domain",
);
}
// Basic domain pattern check
const domainPattern =
/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/;
if (!domainPattern.test(domain)) {
throw new BadRequestError(
"Invalid domain format. Enter a valid domain (e.g., www.example.com)",
);
}
const [website, pro] = await Promise.all([
dal.proWebsites.findByProId(proId),
dal.pros.findById(proId),
]);
if (!website) {
throw new NotFoundError("Website not found");
}
if (!pro?.slug) {
throw new BadRequestError(
"Pro must have a slug before connecting a custom domain",
);
}
// Check uniqueness (another pro might already use this domain)
const existing = await dal.proWebsites.findByCustomDomain(domain);
if (existing && existing.proId !== proId) {
throw new BadRequestError(
"This domain is already in use by another pro",
);
}
// Save custom domain
const updated = await dal.proWebsites.update(website.id, {
customDomain: domain,
customDomainVerified: false,
});
const env = resolveEnvironment(c.env.ENVIRONMENT);
const projectName = getProjectName(pro.slug, env);
return success(c, {
website: updated,
cnameTarget: `${projectName}.pages.dev`,
});
} catch (err) {
return handleError(c, err);
}
},
);
// Remove custom domain
domainRoutes.delete(
"/:proId/website/custom-domain",
requireProAccess,
async (c) => {
try {
const proRole = c.get("proRole");
if (proRole !== "owner" && proRole !== "admin") {
return error(c, "FORBIDDEN", "Only owners can manage website settings", 403);
}
const dal = c.get("dal");
const proId = c.get("proId");
const website = await dal.proWebsites.findByProId(proId);
if (!website) {
throw new NotFoundError("Website not found");
}
const updated = await dal.proWebsites.update(website.id, {
customDomain: null,
customDomainVerified: false,
});
return success(c, { website: updated });
} catch (err) {
return handleError(c, err);
}
},
);
// Verify custom domain DNS
domainRoutes.post(
"/:proId/website/verify-domain",
requireProAccess,
async (c) => {
try {
const proRole = c.get("proRole");
if (proRole !== "owner" && proRole !== "admin") {
return error(c, "FORBIDDEN", "Only owners can manage website settings", 403);
}
const dal = c.get("dal");
const proId = c.get("proId");
const [website, pro] = await Promise.all([
dal.proWebsites.findByProId(proId),
dal.pros.findById(proId),
]);
if (!website) {
throw new NotFoundError("Website not found");
}
if (!website.customDomain) {
throw new BadRequestError("No custom domain configured");
}
if (!pro?.slug) {
throw new BadRequestError("Pro must have a slug");
}
const env = resolveEnvironment(c.env.ENVIRONMENT);
const projectName = getProjectName(pro.slug, env);
const expectedTarget = `${projectName}.pages.dev`;
const result = await verifyExternalDnsCname(
website.customDomain,
expectedTarget,
);
if (result.verified) {
await dal.proWebsites.update(website.id, {
customDomainVerified: true,
});
}
return success(c, {
verified: result.verified,
domain: website.customDomain,
expectedCname: expectedTarget,
resolvedCname: result.resolvedTarget,
error: result.error,
});
} catch (err) {
return handleError(c, err);
}
},
);
export default domainRoutes;
|