Skip to content

Commit

Permalink
fix: fix set-cookie api route issue due to wrong domain name (#284)
Browse files Browse the repository at this point in the history
Because

- #283 

This commit

- fix set-cookie API route issue due to the wrong domain name (close #283)
  • Loading branch information
EiffelFly authored Sep 23, 2022
1 parent 3fd8d21 commit c3efcdd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion integration-test/onboarding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("should navigate first time user to the onboarding page", async ({
);
});

test("should enable email subscription by defautl", async ({ page }) => {
test("should enable email subscription by default", async ({ page }) => {
await page.goto("/onboarding", { waitUntil: "networkidle" });

// Should check email subscription
Expand Down
6 changes: 3 additions & 3 deletions src/lib/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type SetCookiePayload = {
res: NextApiResponse;
value: string;
key: string;
hostname: Nullable<string>;
domain: Nullable<string>;
maxAge: number;
httpOnly: boolean;
};
Expand All @@ -15,7 +15,7 @@ export const setCookie = ({
res,
value,
key,
hostname,
domain,
maxAge,
httpOnly,
}: SetCookiePayload) => {
Expand All @@ -26,7 +26,7 @@ export const setCookie = ({
secure: process.env.NODE_ENV === "production" ? true : false,
path: "/",
sameSite: "lax",
domain: hostname ? hostname : undefined,
domain: domain ? domain : undefined,
});

res.setHeader("Set-Cookie", cookie);
Expand Down
14 changes: 7 additions & 7 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
useEffect(() => {
if (!router.isReady || !trackingToken.data) return;

if (process.env.NEXT_PUBLIC_DISABLE_USAGE_COLLECTION === "true") {
setAmplitudeIsInit(false);
return;
}

if (trackingToken.data === "redirect-to-onboard") {
// We should clear the trackingToken to avoid once user had successfully setup user data, when we push them to the
// pipelines page, because there has no changes related to state, the trackingToken won't be flushed, so it remains
// redirect-to-onboard, user will be redirected to onboarding page again.
trackingToken.setData(null);
router.push("/onboarding");
return;
}

if (process.env.NODE_ENV === "production" && !amplitudeIsInit) {
initAmplitude(trackingToken.data);
setAmplitudeIsInit(true);
if (process.env.NEXT_PUBLIC_DISABLE_USAGE_COLLECTION === "true") {
setAmplitudeIsInit(false);
} else {
initAmplitude(trackingToken.data);
setAmplitudeIsInit(true);
}
}
}, [router.isReady, trackingToken, router.asPath, amplitudeIsInit, router]);

Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/set-user-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}

try {
const hostname = req.headers.host?.split(":")[0];
const payload: SetCookiePayload = {
res: res,
key: "instill-ai-user",
value: JSON.stringify({
cookie_token: body.token,
}),
hostname: hostname ?? null,
domain: req.headers.origin ? new URL(req.headers.origin).hostname : null,
maxAge: 60 * 60 * 24 * 2,
httpOnly: process.env.NODE_ENV === "production" ? true : false,
};
Expand Down

0 comments on commit c3efcdd

Please sign in to comment.