T
tRPC

How to force SSL on projects using tRPC?

How to force SSL on projects using tRPC?

Zzrilman6/3/2023
Hello everyone, I'm working on a project that uses create-t3-app as boilerplate. My project is hosted on heroku. I have an issue with forcing HTTPS connection. Before tRPC, I was able to solve this issue by using express-sslify like it was explained in this post on Stack overflow https://stackoverflow.com/questions/67490760/how-to-enforce-ssl-on-a-node-js-app-on-heroku Is it possible to enforce SSL on projects using tRPC?
Nnlucas6/4/2023
I would typically handle this at the infrastructure level. DNS and Reverse Proxy, etc So it helps to know how you're hosting the app
Zzrilman6/5/2023
Hey. I'm hosting my project on Heroku Basically I've tried to use the code from https://www.npmjs.com/package/express-sslify package By adding a middleware to my procedures, but that resulted in ,dns_probe_finished_nxdomain error on production. Then I found this article on tRPC website https://trpc.io/docs/server/adapters/nextjs And I've tried to write a custom handler that uses similar logic from the above-mentioned package. But the same error occurred 😔 Code snippet:
type Environment = "production" | "development" | "other";

// @see https://nextjs.org/docs/api-routes/introduction
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const currentEnv = process.env.NODE_ENV as Environment;

const host = req?.headers?.["host"] ?? "";
const forwaredProto = req?.headers?.["x-forwarded-proto"] ?? "";
const local = req.url ?? "";
const schema = forwaredProto.indexOf("https") !== -1 ? "https" : "http";
const www = host.replace(/www\./gi, "");
const fullUrl = `https://${www}${local}`;
const removeSlash = (site: string) => site.replace(/\/$/, "");
console.log("👉 file: [trpc].ts:69 handler fullUrl:", fullUrl);

const notLocalHost = !www?.includes("localhost");

if (notLocalHost) {
if (schema !== "https") {
return res.redirect(removeSlash(fullUrl));
}
if (/^www\./i.test(host) && schema === "https") {
return res.redirect(removeSlash(fullUrl));
}
if (/\/$/.test(fullUrl) && fullUrl !== `https://${www}/`) {
return res.redirect(removeSlash(fullUrl));
}
}

// finally pass the request on to the tRPC handler
return apiHandler(req, res);
}
type Environment = "production" | "development" | "other";

// @see https://nextjs.org/docs/api-routes/introduction
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const currentEnv = process.env.NODE_ENV as Environment;

const host = req?.headers?.["host"] ?? "";
const forwaredProto = req?.headers?.["x-forwarded-proto"] ?? "";
const local = req.url ?? "";
const schema = forwaredProto.indexOf("https") !== -1 ? "https" : "http";
const www = host.replace(/www\./gi, "");
const fullUrl = `https://${www}${local}`;
const removeSlash = (site: string) => site.replace(/\/$/, "");
console.log("👉 file: [trpc].ts:69 handler fullUrl:", fullUrl);

const notLocalHost = !www?.includes("localhost");

if (notLocalHost) {
if (schema !== "https") {
return res.redirect(removeSlash(fullUrl));
}
if (/^www\./i.test(host) && schema === "https") {
return res.redirect(removeSlash(fullUrl));
}
if (/\/$/.test(fullUrl) && fullUrl !== `https://${www}/`) {
return res.redirect(removeSlash(fullUrl));
}
}

// finally pass the request on to the tRPC handler
return apiHandler(req, res);
}

Looking for more? Join the community!

T
tRPC

How to force SSL on projects using tRPC?

Join Server
Recommended Posts
AWS Lambda / How to set Cookies inside ProceduresHow can I set and remove cookies similar to ctx.res.cookie(..) with Express when using the aws lambdMassive Type Errors on tRPC 10 Legacy Router against MergeRouterWe're trying to upgrade to tRPC 10 but I am running into a massive type clash when trying to merge rIs it possible to create 2 routers inside a single file?I am having an issue in which it is impossible to me to use a class instance within 2 routers. I triUse onError to change an application error into a TRPCError?I want to use the onError handler to change any instance of a custom application error into a TRPCErIncreasing Body 1mb limitHey, Im trying to build an application that allows sending of base64 encoded files to my next.js serNitro and tRPC in vercel-edgeHey there! I hope this is the right place to ask for help. I am trying to deploy an application witHow to retrieve and receive Bigint data to/from TRPC procedureNode: `v16.15.1` I'm trying to return an object which contains an `amount` property from one of my React Router Loaders + tRPC?In Vite project: would it make sense to use React Router loaders with tRPC? I like more the concept TypeError: Cannot read properties of null (reading 'useContext') when using useMutation with TRPC inI'm encountering an error in my Next.js application when trying to use the useMutation hook with TRPInitial websockets getToken() returns null: next-auth + websockets :)Hello! So the way I am trying to authenticate websockets is like this: ``` import { getToken } fro