sst / ion

SST v3
https://sst.dev
MIT License
1.58k stars 201 forks source link

SolidStart API Routes Download Empty File #785

Closed claughinghouse closed 3 weeks ago

claughinghouse commented 1 month ago

When defining a API route with the SolidStart component local dev mode functions properly but when deployed the lambda just downloads an empty file when the route is visited.

Sample API route at routes/auth/logout.ts:

import { deleteCookie } from "vinxi/http";

export async function GET() {
  deleteCookie("cookie_name");

  return Response.redirect("http://localhost:3000");
}

When running in dev mode this will clear the cookie called cookie_name but when deployed to AWS this just downloads a file called logout.ts.

Same for the api route at src/routes/auth/callback.ts

import { redirect } from "@solidjs/router";
import type { APIEvent } from "@solidjs/start/server";
import { getRequestEvent } from "solid-js/web";
import { setCookie } from "vinxi/http";

export async function GET({ request, nativeEvent }: APIEvent) {
  const { searchParams } = new URL(request.url);

  const code = searchParams.get("code");
  const host = new URL(
// https://discord.com/channels/722131463138705510/910635844119982080/1263579007723507884
    getRequestEvent()!.request.url,
  ).host;

  if (code) {
    const response = await fetch(`${import.meta.env.VITE_AUTH_URL}token`, {
      method: "POST",
      body: new URLSearchParams({
        grant_type: "authorization_code",
        client_id: "astro",
        code,
        redirect_uri: import.meta.env.DEV
          ? `http://${host}/auth/callback`
          : `https://${host}/auth/callback`,
      }),
    });

    if (response.ok) {
      const { access_token } = await response.json();
      setCookie(nativeEvent, "auth_token", access_token, {
        httpOnly: import.meta.env.PROD,
        maxAge: 60 * 60 * 24 * 7, // 7 days
        sameSite: "strict",
      });
      return redirect("/");
    }
  }
  return redirect("/");
}
dragonautdev commented 3 weeks ago

Just jumping into this issue to say it'd be amazing if we could have a fix for this

claughinghouse commented 3 weeks ago

@thdxr resolved this with https://github.com/sst/ion/releases/tag/v3.0.60