Second State
Second State
BABetter Auth
Created by Second State on 4/8/2025 in #help
SIWE Auth plugin
made 2 actions which I can now call from my client comp.
export async function getSiweNonce(publicKey: string) {
return await auth.api.getNonce({
body: {
publicKey,
},
});
}

export async function verifySiweSignature(
message: string,
signature: string,
publicKey: string
) {
return await auth.api.verify({
body: {
message,
signature,
publicKey,
},
});
}
export async function getSiweNonce(publicKey: string) {
return await auth.api.getNonce({
body: {
publicKey,
},
});
}

export async function verifySiweSignature(
message: string,
signature: string,
publicKey: string
) {
return await auth.api.verify({
body: {
message,
signature,
publicKey,
},
});
}
I now get an user and session created after signing the nonce. so is there anything missing from here, or it's good enough? Thanks!
5 replies
BABetter Auth
Created by Second State on 4/8/2025 in #help
SIWE Auth plugin
index.ts, too long to paste: https://privatebin.net/?9eb49b1afc9c5e5b#BmYfj1t5AJZVMyv62eiowufQcse6WVsK5cs7EUaTaiXA client.ts:
import type { BetterAuthClientPlugin } from "better-auth";
import { createSiweMessage } from "viem/siwe";
import type { Address, Hex } from "viem";
import type { siwePlugin } from "./index";

type SiwePlugin = typeof siwePlugin;

export type SiweClientPluginOptions = {
statement?: string;
domain?: string;
origin?: string;
chainId?: number;
};

// Extend the client plugin type to include our SIWE methods
declare module "better-auth" {
interface BetterAuthClientPlugin {
signIn?: {
siwe: (params: {
address: Address;
chainId: number;
signMessage: (message: string) => Promise<Hex>;
}) => Promise<{
user: {
id: string;
ethAddress: string;
name: string;
avatar: string;
email: string;
createdAt: Date;
updatedAt: Date;
};
session: {
token: string;
expiresAt: Date;
};
}>;
};
}
}

export const siweClientPlugin = (options?: SiweClientPluginOptions) => {
return {
id: "siwe",
$InferServerPlugin: {} as ReturnType<SiwePlugin>,

signIn: {
siwe: async ({
address,
chainId,
signMessage,
}: {
address: Address;
chainId: number;
signMessage: (message: string) => Promise<Hex>;
}) => {
// Get nonce from server
const nonceRes = await fetch("/api/auth/siwe/nonce", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
publicKey: address,
}),
});

if (!nonceRes.ok) {
throw new Error("Failed to get nonce");
}
const { nonce } = await nonceRes.json();

// Create SIWE message
const messageObj = createSiweMessage({
domain: options?.domain || window.location.host,
address,
statement:
options?.statement || "Sign in with Ethereum to authenticate.",
uri: options?.origin || window.location.origin,
version: "1",
chainId: options?.chainId || chainId,
nonce,
});

// Get signature from wallet
const signature = await signMessage(messageObj);

// Verify signature with server
const verifyRes = await fetch("/api/auth/siwe/verify", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: messageObj,
signature,
publicKey: address,
}),
});

if (!verifyRes.ok) {
const error = await verifyRes.json();
throw new Error(error.message || "Failed to verify signature");
}

return verifyRes.json();
},
},
} satisfies BetterAuthClientPlugin;
};
import type { BetterAuthClientPlugin } from "better-auth";
import { createSiweMessage } from "viem/siwe";
import type { Address, Hex } from "viem";
import type { siwePlugin } from "./index";

type SiwePlugin = typeof siwePlugin;

export type SiweClientPluginOptions = {
statement?: string;
domain?: string;
origin?: string;
chainId?: number;
};

// Extend the client plugin type to include our SIWE methods
declare module "better-auth" {
interface BetterAuthClientPlugin {
signIn?: {
siwe: (params: {
address: Address;
chainId: number;
signMessage: (message: string) => Promise<Hex>;
}) => Promise<{
user: {
id: string;
ethAddress: string;
name: string;
avatar: string;
email: string;
createdAt: Date;
updatedAt: Date;
};
session: {
token: string;
expiresAt: Date;
};
}>;
};
}
}

export const siweClientPlugin = (options?: SiweClientPluginOptions) => {
return {
id: "siwe",
$InferServerPlugin: {} as ReturnType<SiwePlugin>,

signIn: {
siwe: async ({
address,
chainId,
signMessage,
}: {
address: Address;
chainId: number;
signMessage: (message: string) => Promise<Hex>;
}) => {
// Get nonce from server
const nonceRes = await fetch("/api/auth/siwe/nonce", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
publicKey: address,
}),
});

if (!nonceRes.ok) {
throw new Error("Failed to get nonce");
}
const { nonce } = await nonceRes.json();

// Create SIWE message
const messageObj = createSiweMessage({
domain: options?.domain || window.location.host,
address,
statement:
options?.statement || "Sign in with Ethereum to authenticate.",
uri: options?.origin || window.location.origin,
version: "1",
chainId: options?.chainId || chainId,
nonce,
});

// Get signature from wallet
const signature = await signMessage(messageObj);

// Verify signature with server
const verifyRes = await fetch("/api/auth/siwe/verify", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: messageObj,
signature,
publicKey: address,
}),
});

if (!verifyRes.ok) {
const error = await verifyRes.json();
throw new Error(error.message || "Failed to verify signature");
}

return verifyRes.json();
},
},
} satisfies BetterAuthClientPlugin;
};
5 replies
TTCTheo's Typesafe Cult
Created by gave_one on 8/12/2023 in #questions
shadcn/ui is not really working ,it's rendering the elements but the styles are not there
No description
24 replies
TTCTheo's Typesafe Cult
Created by gave_one on 8/12/2023 in #questions
shadcn/ui is not really working ,it's rendering the elements but the styles are not there
Sorry for the ping, is this still the case, or did you make any advancements in it? I have also found that repo as well, but it's making me insane that if I do create t3-app + add shadcn 1:1 how the owner did it, I get a shitty off styled calendar (and other components), if I clone that repo, I get a properly styled one Facepalm . I can't seem to figure out what the hell is causing this, tried everything from npm,pnpm, bun, running this crap in a docker container on linux, etc, but holy shit, is this annoying. (ps. I also got the same issue by just creating a regular next app.)
24 replies