Skip to content

Commit

Permalink
Fixed https missing from url
Browse files Browse the repository at this point in the history
  • Loading branch information
ericallam committed Jun 22, 2023
1 parent 31d8669 commit fd8fd22
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
EnvironmentsPresenter,
} from "~/presenters/EnvironmentsPresenter.server";
import { requireUserId } from "~/services/session.server";
import { formatDateTime } from "~/utils";
import { formatDateTime, requestUrl } from "~/utils";
import { Handle } from "~/utils/handle";
import { ProjectParamSchema } from "~/utils/pathBuilder";
import { RuntimeEnvironmentType } from "../../../../../packages/database/src";
Expand All @@ -41,7 +41,7 @@ export const loader = async ({ request, params }: LoaderArgs) => {
const { projectParam } = ProjectParamSchema.parse(params);

try {
const url = new URL(request.url);
const url = requestUrl(request);
const baseUrl = `${url.protocol}//${url.host}`;
const presenter = new EnvironmentsPresenter();
const { environments, clients } = await presenter.call({
Expand Down
13 changes: 8 additions & 5 deletions apps/webapp/app/routes/login._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@ import { FormTitle } from "~/components/primitives/FormTitle";
import { NamedIcon } from "~/components/primitives/NamedIcon";
import { Paragraph, TextLink } from "~/components/primitives/Paragraph";
import { isGithubAuthSupported } from "~/services/auth.server";

import { commitSession, setRedirectTo } from "~/services/redirectTo.server";
import { getUserId } from "~/services/session.server";
import { requestUrl } from "~/utils";

export async function loader({ request }: LoaderArgs) {
const userId = await getUserId(request);
if (userId) return redirect("/");

const url = new URL(request.url);
const url = requestUrl(request);
const redirectTo = url.searchParams.get("redirectTo");

if (redirectTo) {
const session = await setRedirectTo(request, redirectTo);

return typedjson(
{ redirectTo, isGithubAuthSupported },
{ redirectTo, showGithubAuth: isGithubAuthSupported },
{
headers: {
"Set-Cookie": await commitSession(session),
},
}
);
} else {
return typedjson({ redirectTo: null, isGithubAuthSupported });
return typedjson({
redirectTo: null,
showGithubAuth: isGithubAuthSupported,
});
}
}

Expand Down Expand Up @@ -64,7 +67,7 @@ export default function LoginPage() {
<FormTitle divide={false} title="Create your Trigger.dev account" />
<Fieldset>
<div className="flex flex-col gap-y-2">
{isGithubAuthSupported && (
{data.showGithubAuth && (
<Button type="submit" variant="primary/large" fullWidth>
<NamedIcon name={"github"} className={"mr-1.5 h-4 w-4"} />
Continue with GitHub
Expand Down
3 changes: 2 additions & 1 deletion apps/webapp/app/routes/oauth2.callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { env } from "~/env.server";
import { integrationAuthRepository } from "~/services/externalApis/integrationAuthRepository.server";
import { OAuthClient, OAuthClientSchema } from "~/services/externalApis/types";
import { getSecretStore } from "~/services/secrets/secretStore.server";
import { requestUrl } from "~/utils";

const ParamsSchema = z
.object({
Expand All @@ -20,7 +21,7 @@ export async function loader({ request }: LoaderArgs) {
return { status: 405, body: "Method Not Allowed" };
}

const url = new URL(request.url);
const url = requestUrl(request);
const parsedParams = ParamsSchema.safeParse(
Object.fromEntries(url.searchParams)
);
Expand Down
5 changes: 4 additions & 1 deletion apps/webapp/app/services/sources/handleHttpSource.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PrismaClient } from "~/db.server";
import { prisma } from "~/db.server";
import { workerQueue } from "../worker.server";
import { requestUrl } from "~/utils";

export class HandleHttpSourceService {
#prismaClient: PrismaClient;
Expand All @@ -10,6 +11,8 @@ export class HandleHttpSourceService {
}

public async call(id: string, request: Request) {
const url = requestUrl(request);

const triggerSource = await this.#prismaClient.triggerSource.findUnique({
where: { id },
include: {
Expand All @@ -35,7 +38,7 @@ export class HandleHttpSourceService {
sourceId: triggerSource.id,
endpointId: triggerSource.endpointId,
environmentId: triggerSource.environmentId,
url: request.url,
url: url.href,
method: request.method,
headers: Object.fromEntries(request.headers),
body: ["POST", "PUT", "PATCH"].includes(request.method)
Expand Down
18 changes: 14 additions & 4 deletions apps/webapp/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
ErrorWithStack,
ErrorWithStackSchema,
} from "@trigger.dev/internal";
import { ErrorWithStack, ErrorWithStackSchema } from "@trigger.dev/internal";
import type { RouteMatch } from "@remix-run/react";
import { useMatches } from "@remix-run/react";
import humanizeDuration from "humanize-duration";
Expand Down Expand Up @@ -214,3 +211,16 @@ export function formatUnknownError(

return "Unknown error";
}

// This will read the X-Forwarded-Proto header from the request, and make sure the
// returned url has the matching proto if the request.url does not
export function requestUrl(request: Request): URL {
const url = new URL(request.url);
const proto = request.headers.get("X-Forwarded-Proto");

if (proto && url.protocol !== proto) {
url.protocol = proto;
}

return url;
}

0 comments on commit fd8fd22

Please sign in to comment.