add signIn example

This commit is contained in:
Tony D'Addeo
2024-12-17 14:26:20 -06:00
committed by lucas-neynar
parent e1a87ff00f
commit cd4149abd5
9 changed files with 539 additions and 20 deletions

77
src/auth.ts Normal file
View File

@@ -0,0 +1,77 @@
import { AuthOptions, getServerSession } from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";
import { createAppClient, viemConnector } from "@farcaster/auth-client";
declare module "next-auth" {
interface Session {
user: {
fid: number;
};
}
}
export const authOptions: AuthOptions = {
// Configure one or more authentication providers
providers: [
CredentialsProvider({
name: "Sign in with Farcaster",
credentials: {
message: {
label: "Message",
type: "text",
placeholder: "0x0",
},
signature: {
label: "Signature",
type: "text",
placeholder: "0x0",
},
// In a production app with a server, these should be fetched from
// your Farcaster data indexer rather than have them accepted as part
// of credentials.
name: {
label: "Name",
type: "text",
placeholder: "0x0",
},
pfp: {
label: "Pfp",
type: "text",
placeholder: "0x0",
},
},
async authorize(credentials, req) {
const csrfToken = req?.body?.csrfToken;
const appClient = createAppClient({
ethereum: viemConnector(),
});
const verifyResponse = await appClient.verifySignInMessage({
message: credentials?.message as string,
signature: credentials?.signature as `0x${string}`,
domain: process.env.NEXTAUTH_URL ?? '',
nonce: csrfToken,
});
const { success, fid } = verifyResponse;
if (!success) {
return null;
}
return {
id: fid.toString(),
};
},
}),
],
callbacks: {
session: async ({ session, token }) => {
if (session?.user) {
session.user.fid = parseInt(token.sub ?? '');
}
return session;
},
}
}
export const getSession = () => getServerSession(authOptions)