Skip to content

Commit

Permalink
fix: SIWE domain issue (#1564)
Browse files Browse the repository at this point in the history
* feat(SignInDialog): add error toast

* fix: add `https://` to the `NEXT_PUBLIC_URL` env var

* add logs

* feat: add `NEXT_PUBLIC_SIWE_URL` env var

* feat(SIWE): include hostname only in the domain field
  • Loading branch information
BrickheadJohnny authored Nov 25, 2024
1 parent 99d59ea commit fe568d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
20 changes: 11 additions & 9 deletions src/components/SignInDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import { signIn } from "@/actions/auth";
import { signInDialogOpenAtom } from "@/config/atoms";
import { env } from "@/lib/env";
import { SignIn, User, Wallet } from "@phosphor-icons/react/dist/ssr";
import { SignIn, User, Wallet, XCircle } from "@phosphor-icons/react/dist/ssr";
import { DialogDescription } from "@radix-ui/react-dialog";
import { useMutation } from "@tanstack/react-query";
import { useAtom, useSetAtom } from "jotai";
import { shortenHex } from "lib/shortenHex";
import { toast } from "sonner";
import { createSiweMessage } from "viem/siwe";
import { useAccount, useConnect, useSignMessage } from "wagmi";
import { z } from "zod";
Expand Down Expand Up @@ -89,19 +90,13 @@ const SignInWithEthereum = () => {
const { nonce } = await fetch(`${env.NEXT_PUBLIC_API}/auth/siwe/nonce`)
.then((res) => res.json())
.then((data) => z.object({ nonce: z.string() }).parse(data));
const urlHostname = [
new URL(env.NEXT_PUBLIC_URL).hostname,
window.location.port,
]
.filter(Boolean)
.join(":");

const message = createSiweMessage({
address: address!,
chainId: 1,
domain: urlHostname,
domain: new URL(env.NEXT_PUBLIC_SIWE_URL).hostname,
nonce,
uri: urlHostname,
uri: env.NEXT_PUBLIC_SIWE_URL,
version: "1",
});

Expand All @@ -110,6 +105,13 @@ const SignInWithEthereum = () => {
return signIn({ message, signature });
},
onSuccess: () => setSignInDialogOpen(false),
onError: (error) => {
toast("Sign in error", {
description: error.message,
icon: <XCircle weight="fill" className="text-icon-error" />,
});
console.error(error);
},
});

return (
Expand Down
8 changes: 4 additions & 4 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export const env = createEnv({
},
client: {
NEXT_PUBLIC_API: z.string(),
NEXT_PUBLIC_URL: z.string(),
NEXT_PUBLIC_SIWE_URL: z.string(),
NEXT_PUBLIC_PINATA_GATEWAY_URL: z.string(),
},
runtimeEnv: {
NEXT_PUBLIC_API: process.env.NEXT_PUBLIC_API_V3,
NEXT_PUBLIC_URL:
NEXT_PUBLIC_SIWE_URL:
process.env.NODE_ENV === "production"
? process.env.NEXT_PUBLIC_VERCEL_URL
: "http://localhost",
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: "http://localhost:3000",
PINATA_ADMIN_JWT: process.env.PINATA_ADMIN_JWT,
NEXT_PUBLIC_PINATA_GATEWAY_URL: process.env.NEXT_PUBLIC_PINATA_GATEWAY_URL,
},
Expand Down

0 comments on commit fe568d4

Please sign in to comment.