Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(registry): extract token from auth response object in invite page

vholik1e2ecc955f0a
diff --git a/packages/registry/r/team-management.json b/packages/registry/r/team-management.json
index bba4baec9..6c40d938e 100644
--- a/packages/registry/r/team-management.json
+++ b/packages/registry/r/team-management.json
@@ -213,7 +213,7 @@
},
{
"path": "team-management/vendor/pages/invite/page.tsx",
- "content": "import { zodResolver } from \"@hookform/resolvers/zod\";\nimport { Alert, Button, Heading, Hint, Input, Text } from \"@medusajs/ui\";\nimport { useState } from \"react\";\nimport { useForm } from \"react-hook-form\";\nimport { decodeToken } from \"react-jwt\";\nimport { Link, useSearchParams } from \"react-router-dom\";\nimport { z } from \"zod\";\n\nimport { useSignUpForInvite, useAcceptInvite } from \"../../hooks/api/invites\";\n\nexport const config = {\n public: true,\n};\n\nconst CreateAccountSchema = z\n .object({\n email: z.string().email(),\n first_name: z.string().min(1, \"First name is required\"),\n last_name: z.string().min(1, \"Last name is required\"),\n password: z.string().min(1, \"Password is required\"),\n repeat_password: z.string().min(1, \"Please repeat password\"),\n })\n .superRefine(({ password, repeat_password }, ctx) => {\n if (password !== repeat_password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"Passwords do not match\",\n path: [\"repeat_password\"],\n });\n }\n });\n\ntype DecodedInvite = {\n id: string;\n exp: number;\n iat: number;\n};\n\nconst InviteSchema = z.object({\n id: z.string(),\n exp: z.number(),\n iat: z.number(),\n});\n\nconst validateDecodedInvite = (decoded: unknown): decoded is DecodedInvite => {\n return InviteSchema.safeParse(decoded).success;\n};\n\nconst InviteAcceptPage = () => {\n const [searchParams] = useSearchParams();\n const [success, setSuccess] = useState(false);\n\n const token = searchParams.get(\"token\") || null;\n const invite: DecodedInvite | null = token ? decodeToken(token) : null;\n const isValidInvite = invite && validateDecodedInvite(invite);\n\n return (\n <div className=\"bg-ui-bg-subtle relative flex min-h-dvh w-dvw items-center justify-center p-4\">\n <div className=\"flex w-full max-w-[360px] flex-col items-center\">\n {isValidInvite ? (\n !success ? (\n <CreateView onSuccess={() => setSuccess(true)} token={token!} />\n ) : (\n <SuccessView />\n )\n ) : (\n <InvalidView />\n )}\n </div>\n </div>\n );\n};\n\nconst LoginLink = () => (\n <div className=\"flex w-full flex-col items-center\">\n <div className=\"my-6 h-px w-full border-b border-dotted\" />\n <Link\n to=\"/login\"\n className=\"txt-small text-ui-fg-base transition-fg hover:text-ui-fg-base-hover font-medium outline-none\"\n >\n Back to login\n </Link>\n </div>\n);\n\nconst InvalidView = () => (\n <div className=\"flex flex-col items-center\">\n <div className=\"flex flex-col items-center gap-y-1\">\n <Heading>Invalid invite</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n The invite link is invalid or has expired. Please request a new invite.\n </Text>\n </div>\n <LoginLink />\n </div>\n);\n\nconst CreateView = ({\n onSuccess,\n token,\n}: {\n onSuccess: () => void;\n token: string;\n}) => {\n const [invalid, setInvalid] = useState(false);\n\n const form = useForm<z.infer<typeof CreateAccountSchema>>({\n resolver: zodResolver(CreateAccountSchema),\n defaultValues: {\n email: \"\",\n first_name: \"\",\n last_name: \"\",\n password: \"\",\n repeat_password: \"\",\n },\n });\n\n const { mutateAsync: signUp, isPending: isCreating } = useSignUpForInvite();\n const { mutateAsync: acceptInvite, isPending: isAccepting } =\n useAcceptInvite(token);\n\n const handleSubmit = form.handleSubmit(async (data) => {\n try {\n const authToken = await signUp({\n email: data.email,\n password: data.password,\n });\n\n await acceptInvite({\n name: `${data.first_name} ${data.last_name}`,\n auth_token: authToken as string,\n });\n\n onSuccess();\n } catch (error: any) {\n if (error?.status === 400) {\n form.setError(\"root\", {\n type: \"manual\",\n message: \"This invite is no longer valid.\",\n });\n setInvalid(true);\n return;\n }\n form.setError(\"root\", {\n type: \"manual\",\n message: error.message || \"Something went wrong\",\n });\n }\n });\n\n const serverError = form.formState.errors.root?.message;\n const validationError =\n form.formState.errors.email?.message ||\n form.formState.errors.first_name?.message ||\n form.formState.errors.last_name?.message ||\n form.formState.errors.password?.message ||\n form.formState.errors.repeat_password?.message;\n\n return (\n <div className=\"flex w-full flex-col items-center\">\n <div className=\"mb-4 flex flex-col items-center\">\n <Heading>Create your account</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n Enter your details to accept the invitation and join the team.\n </Text>\n </div>\n <form onSubmit={handleSubmit} className=\"flex w-full flex-col gap-y-6\">\n <div className=\"flex flex-col gap-y-2\">\n <Input\n autoComplete=\"off\"\n placeholder=\"Email\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"email\")}\n />\n <Input\n autoComplete=\"given-name\"\n placeholder=\"First name\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"first_name\")}\n />\n <Input\n autoComplete=\"family-name\"\n placeholder=\"Last name\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"last_name\")}\n />\n <Input\n autoComplete=\"new-password\"\n type=\"password\"\n placeholder=\"Password\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"password\")}\n />\n <Input\n autoComplete=\"off\"\n type=\"password\"\n placeholder=\"Repeat password\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"repeat_password\")}\n />\n {validationError && (\n <div className=\"mt-2 text-center\">\n <Hint variant=\"error\">{validationError}</Hint>\n </div>\n )}\n {serverError && (\n <Alert\n className=\"bg-ui-bg-base items-center p-2\"\n dismissible\n variant=\"error\"\n >\n {serverError}\n </Alert>\n )}\n </div>\n <Button\n className=\"w-full\"\n type=\"submit\"\n isLoading={isCreating || isAccepting}\n disabled={invalid}\n >\n Create account\n </Button>\n </form>\n <LoginLink />\n </div>\n );\n};\n\nconst SuccessView = () => (\n <div className=\"flex w-full flex-col items-center gap-y-6\">\n <div className=\"flex flex-col items-center gap-y-1\">\n <Heading className=\"text-center\">Account created!</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n Your account has been created. You can now log in.\n </Text>\n </div>\n <Button variant=\"secondary\" asChild className=\"w-full\">\n <Link to=\"/login\" replace>\n Go to login\n </Link>\n </Button>\n </div>\n);\n\nexport default InviteAcceptPage;\n",
+ "content": "import { zodResolver } from \"@hookform/resolvers/zod\";\nimport { Alert, Button, Heading, Hint, Input, Text } from \"@medusajs/ui\";\nimport { useState } from \"react\";\nimport { useForm } from \"react-hook-form\";\nimport { decodeToken } from \"react-jwt\";\nimport { Link, useSearchParams } from \"react-router-dom\";\nimport { z } from \"zod\";\n\nimport { useSignUpForInvite, useAcceptInvite } from \"../../hooks/api/invites\";\n\nexport const config = {\n public: true,\n};\n\nconst CreateAccountSchema = z\n .object({\n email: z.string().email(),\n first_name: z.string().min(1, \"First name is required\"),\n last_name: z.string().min(1, \"Last name is required\"),\n password: z.string().min(1, \"Password is required\"),\n repeat_password: z.string().min(1, \"Please repeat password\"),\n })\n .superRefine(({ password, repeat_password }, ctx) => {\n if (password !== repeat_password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"Passwords do not match\",\n path: [\"repeat_password\"],\n });\n }\n });\n\ntype DecodedInvite = {\n id: string;\n exp: number;\n iat: number;\n};\n\nconst InviteSchema = z.object({\n id: z.string(),\n exp: z.number(),\n iat: z.number(),\n});\n\nconst validateDecodedInvite = (decoded: unknown): decoded is DecodedInvite => {\n return InviteSchema.safeParse(decoded).success;\n};\n\nconst InviteAcceptPage = () => {\n const [searchParams] = useSearchParams();\n const [success, setSuccess] = useState(false);\n\n const token = searchParams.get(\"token\") || null;\n const invite: DecodedInvite | null = token ? decodeToken(token) : null;\n const isValidInvite = invite && validateDecodedInvite(invite);\n\n return (\n <div className=\"bg-ui-bg-subtle relative flex min-h-dvh w-dvw items-center justify-center p-4\">\n <div className=\"flex w-full max-w-[360px] flex-col items-center\">\n {isValidInvite ? (\n !success ? (\n <CreateView onSuccess={() => setSuccess(true)} token={token!} />\n ) : (\n <SuccessView />\n )\n ) : (\n <InvalidView />\n )}\n </div>\n </div>\n );\n};\n\nconst LoginLink = () => (\n <div className=\"flex w-full flex-col items-center\">\n <div className=\"my-6 h-px w-full border-b border-dotted\" />\n <Link\n to=\"/login\"\n className=\"txt-small text-ui-fg-base transition-fg hover:text-ui-fg-base-hover font-medium outline-none\"\n >\n Back to login\n </Link>\n </div>\n);\n\nconst InvalidView = () => (\n <div className=\"flex flex-col items-center\">\n <div className=\"flex flex-col items-center gap-y-1\">\n <Heading>Invalid invite</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n The invite link is invalid or has expired. Please request a new invite.\n </Text>\n </div>\n <LoginLink />\n </div>\n);\n\nconst CreateView = ({\n onSuccess,\n token,\n}: {\n onSuccess: () => void;\n token: string;\n}) => {\n const [invalid, setInvalid] = useState(false);\n\n const form = useForm<z.infer<typeof CreateAccountSchema>>({\n resolver: zodResolver(CreateAccountSchema),\n defaultValues: {\n email: \"\",\n first_name: \"\",\n last_name: \"\",\n password: \"\",\n repeat_password: \"\",\n },\n });\n\n const { mutateAsync: signUp, isPending: isCreating } = useSignUpForInvite();\n const { mutateAsync: acceptInvite, isPending: isAccepting } =\n useAcceptInvite(token);\n\n const handleSubmit = form.handleSubmit(async (data) => {\n try {\n const authToken = await signUp({\n email: data.email,\n password: data.password,\n });\n\n await acceptInvite({\n name: `${data.first_name} ${data.last_name}`,\n auth_token: (authToken as { token: string }).token,\n });\n\n onSuccess();\n } catch (error: any) {\n if (error?.status === 400) {\n form.setError(\"root\", {\n type: \"manual\",\n message: \"This invite is no longer valid.\",\n });\n setInvalid(true);\n return;\n }\n form.setError(\"root\", {\n type: \"manual\",\n message: error.message || \"Something went wrong\",\n });\n }\n });\n\n const serverError = form.formState.errors.root?.message;\n const validationError =\n form.formState.errors.email?.message ||\n form.formState.errors.first_name?.message ||\n form.formState.errors.last_name?.message ||\n form.formState.errors.password?.message ||\n form.formState.errors.repeat_password?.message;\n\n return (\n <div className=\"flex w-full flex-col items-center\">\n <div className=\"mb-4 flex flex-col items-center\">\n <Heading>Create your account</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n Enter your details to accept the invitation and join the team.\n </Text>\n </div>\n <form onSubmit={handleSubmit} className=\"flex w-full flex-col gap-y-6\">\n <div className=\"flex flex-col gap-y-2\">\n <Input\n autoComplete=\"off\"\n placeholder=\"Email\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"email\")}\n />\n <Input\n autoComplete=\"given-name\"\n placeholder=\"First name\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"first_name\")}\n />\n <Input\n autoComplete=\"family-name\"\n placeholder=\"Last name\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"last_name\")}\n />\n <Input\n autoComplete=\"new-password\"\n type=\"password\"\n placeholder=\"Password\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"password\")}\n />\n <Input\n autoComplete=\"off\"\n type=\"password\"\n placeholder=\"Repeat password\"\n className=\"bg-ui-bg-field-component\"\n {...form.register(\"repeat_password\")}\n />\n {validationError && (\n <div className=\"mt-2 text-center\">\n <Hint variant=\"error\">{validationError}</Hint>\n </div>\n )}\n {serverError && (\n <Alert\n className=\"bg-ui-bg-base items-center p-2\"\n dismissible\n variant=\"error\"\n >\n {serverError}\n </Alert>\n )}\n </div>\n <Button\n className=\"w-full\"\n type=\"submit\"\n isLoading={isCreating || isAccepting}\n disabled={invalid}\n >\n Create account\n </Button>\n </form>\n <LoginLink />\n </div>\n );\n};\n\nconst SuccessView = () => (\n <div className=\"flex w-full flex-col items-center gap-y-6\">\n <div className=\"flex flex-col items-center gap-y-1\">\n <Heading className=\"text-center\">Account created!</Heading>\n <Text size=\"small\" className=\"text-ui-fg-subtle text-center\">\n Your account has been created. You can now log in.\n </Text>\n </div>\n <Button variant=\"secondary\" asChild className=\"w-full\">\n <Link to=\"/login\" replace>\n Go to login\n </Link>\n </Button>\n </div>\n);\n\nexport default InviteAcceptPage;\n",
"type": "registry:vendor"
}
]
diff --git a/packages/registry/src/team-management/vendor/pages/invite/page.tsx b/packages/registry/src/team-management/vendor/pages/invite/page.tsx
index c92575793..00c414567 100644
--- a/packages/registry/src/team-management/vendor/pages/invite/page.tsx
+++ b/packages/registry/src/team-management/vendor/pages/invite/page.tsx
@@ -128,7 +128,7 @@ const CreateView = ({
await acceptInvite({
name: `${data.first_name} ${data.last_name}`,
- auth_token: authToken as string,
+ auth_token: (authToken as { token: string }).token,
});
onSuccess();