Skip to content

Commit

Permalink
fix: fix onboarding form (#182)
Browse files Browse the repository at this point in the history
Because

- Onboarding form subscription is not working 
- Api route is failed at docker but working at when development

This commit

- Fix subscription issue
- Examine api route issue
  • Loading branch information
EiffelFly committed Jul 28, 2022
1 parent ac51161 commit a944e1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
7 changes: 1 addition & 6 deletions src/components/forms/mgmt/OnboardingForm/OnboardingForm.tsx
Expand Up @@ -189,12 +189,7 @@ const OnboardingForm: FC<OnBoardingFormProps> = ({ user }) => {
id="newsletterSubscription"
name="newsletterSubscription"
label="Newsletter subscription"
value={
formik.values.newsletterSubscription ??
user?.newsletter_subscription
? user?.newsletter_subscription ?? false
: false
}
value={formik.values.newsletterSubscription ?? false}
additionalMessageOnLabel={null}
disabled={false}
readOnly={false}
Expand Down
27 changes: 18 additions & 9 deletions src/lib/cookie.ts
Expand Up @@ -2,22 +2,31 @@ import { NextApiResponse } from "next";
import { serialize } from "cookie";
import { Nullable } from "@/types/general";

export const setCookie = (
res: NextApiResponse,
value: string,
key: string,
domain: Nullable<string>,
maxAge: number,
httOnly: boolean
) => {
export type SetCookiePayload = {
res: NextApiResponse;
value: string;
key: string;
hostname: Nullable<string>;
maxAge: number;
httOnly: boolean;
};

export const setCookie = ({
res,
value,
key,
hostname,
maxAge,
httOnly,
}: SetCookiePayload) => {
const cookie = serialize(key, value, {
maxAge: maxAge,
expires: new Date(Date.now() + maxAge * 1000),
httpOnly: httOnly,
secure: process.env.NODE_ENV === "production" ? true : false,
path: "/",
sameSite: "lax",
domain: domain ? domain : undefined,
domain: hostname ? hostname : undefined,
});

res.setHeader("Set-Cookie", cookie);
Expand Down
40 changes: 21 additions & 19 deletions src/pages/api/set-user-cookie.ts
@@ -1,4 +1,4 @@
import { setCookie } from "@/lib/cookie";
import { setCookie, SetCookiePayload } from "@/lib/cookie";
import { NextApiRequest, NextApiResponse } from "next";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Expand All @@ -13,28 +13,30 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(500).end("Token not provided");
}

const payload = {
cookie_token: body.token,
};

if (!process.env.NEXT_PUBLIC_CONSOLE_BASE_URL) {
return res.status(500).end("Env CONSOLE_BASE_URL is not provided");
}

const hostname = new URL(process.env.NEXT_PUBLIC_CONSOLE_BASE_URL).hostname;

setCookie(
res,
JSON.stringify(payload),
"instill-ai-user",
hostname,
60 * 60 * 24 * 30,
process.env.NODE_ENV === "production" ? true : false
);

return res.status(200).json({
status: "ok",
});
try {
const payload: SetCookiePayload = {
res: res,
key: "instill-ai-user",
value: JSON.stringify({
cookie_token: body.token,
}),
hostname: req.headers.host ?? null,
maxAge: 60 * 60 * 24 * 30,
httOnly: process.env.NODE_ENV === "production" ? true : false,
};

setCookie(payload);

return res.status(200).json({
status: "ok",
});
} catch (err) {
return res.status(500).end(String(err));
}
};

export default handler;

0 comments on commit a944e1d

Please sign in to comment.