Skip to content

Commit

Permalink
fix auth on contract deploy (#4647)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Sep 17, 2024
1 parent 3be53f4 commit 5ee7489
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export default async function DirectDeployPage(props: DirectDeployPageProps) {
description={metadata.description}
logo={metadata.logo}
/>
<DeployFormForUri contractMetadata={metadata} modules={null} />
<DeployFormForUri
contractMetadata={metadata}
modules={null}
pathname={`/contracts/deploy/${props.params.compiler_uri}`}
/>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function DeployFormForPublishInfo(props: PublishBasedDeployProps) {
<DeployFormForUri
contractMetadata={contractMetadata}
modules={fetchedModules.filter((m) => m !== null)}
pathname={`/${props.publisher}/${props.contract_id}${props.version ? `/${props.version}` : ""}/deploy`}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import { ChakraProviderSetup } from "@/components/ChakraProviderSetup";
import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie";
import { CustomContractForm } from "components/contract-components/contract-deploy-form/custom-contract";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getAddress } from "thirdweb";
import type { FetchDeployMetadataResult } from "thirdweb/contract";

type DeployFormForUriProps = {
contractMetadata: FetchDeployMetadataResult | null;
modules: FetchDeployMetadataResult[] | null;
pathname: string;
};

export async function DeployFormForUri(props: DeployFormForUriProps) {
const { contractMetadata, modules } = props;
const { contractMetadata, modules, pathname } = props;

if (!contractMetadata) {
return <div>Could not fetch metadata</div>;
}

const cookieStore = cookies();
const address = cookieStore.get(COOKIE_ACTIVE_ACCOUNT)?.value;
if (!address) {
redirect(`/login?next=${encodeURIComponent(pathname)}`);
}
const authCookieName = COOKIE_PREFIX_TOKEN + getAddress(address);
const token = cookieStore.get(authCookieName)?.value;
if (!token) {
redirect(`/login?next=${encodeURIComponent(pathname)}`);
}

// TODO: remove the `ChakraProviderSetup` wrapper once the form is updated to no longer use chrakra
return (
<ChakraProviderSetup>
<CustomContractForm
metadata={contractMetadata}
modules={modules?.filter((m) => m !== null)}
jwt={token}
/>
</ChakraProviderSetup>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { TrustedForwardersFieldset } from "./trusted-forwarders-fieldset";

interface CustomContractFormProps {
metadata: FetchDeployMetadataResult;
jwt: string;
modules?: FetchDeployMetadataResult[];
}

Expand Down Expand Up @@ -112,6 +113,7 @@ function checkTwPublisher(publisher: string | undefined) {
export const CustomContractForm: React.FC<CustomContractFormProps> = ({
metadata,
modules,
jwt,
}) => {
const activeAccount = useActiveAccount();
const walletChain = useActiveWalletChain();
Expand Down Expand Up @@ -454,10 +456,12 @@ export const CustomContractForm: React.FC<CustomContractFormProps> = ({
id="custom-contract-form"
as="form"
onSubmit={form.handleSubmit(async (formData) => {
if (!walletChain?.id || !activeAccount) {
if (!walletChain?.id || !activeAccount || !jwt) {
return;
}

window.TW_AUTH_TOKEN = jwt;

// open the status modal
let steps: DeployModalStep[] = [
{
Expand Down

0 comments on commit 5ee7489

Please sign in to comment.