|
1 | | -import { compare } from 'bcrypt-ts'; |
2 | | -import NextAuth, { type User, type Session } from 'next-auth'; |
3 | | -import Credentials from 'next-auth/providers/credentials'; |
| 1 | +import NextAuth from "next-auth"; |
| 2 | +import credentialsProvider from "next-auth/providers/credentials"; |
| 3 | +import { getAddressFromMessage, getChainIdFromMessage, type SIWESession } from "@reown/appkit-siwe"; |
| 4 | +import { createPublicClient, http } from "viem"; |
4 | 5 |
|
5 | | -import { getUser } from '@/lib/db/queries'; |
| 6 | +import { authConfig } from "./auth.config"; |
6 | 7 |
|
7 | | -import { authConfig } from './auth.config'; |
| 8 | +declare module "next-auth" { |
| 9 | + interface Session extends SIWESession { |
| 10 | + address: string; |
| 11 | + chainId: number; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +export const nextAuthSecret = process.env.NEXTAUTH_SECRET; |
| 16 | +if (!nextAuthSecret) { |
| 17 | + throw new Error("NEXTAUTH_SECRET is not set"); |
| 18 | +} |
8 | 19 |
|
9 | | -interface ExtendedSession extends Session { |
10 | | - user: User; |
| 20 | +export const projectId = process.env.NEXT_PUBLIC_PROJECT_ID; |
| 21 | +if (!projectId) { |
| 22 | + throw new Error("NEXT_PUBLIC_PROJECT_ID is not set"); |
11 | 23 | } |
12 | 24 |
|
| 25 | +const providers = [ |
| 26 | + credentialsProvider({ |
| 27 | + name: "Ethereum", |
| 28 | + credentials: { |
| 29 | + message: { |
| 30 | + label: "Message", |
| 31 | + type: "text", |
| 32 | + placeholder: "0x0", |
| 33 | + }, |
| 34 | + signature: { |
| 35 | + label: "Signature", |
| 36 | + type: "text", |
| 37 | + placeholder: "0x0", |
| 38 | + }, |
| 39 | + }, |
| 40 | + async authorize(credentials) { |
| 41 | + try { |
| 42 | + if (!credentials?.message) { |
| 43 | + throw new Error("SiweMessage is undefined"); |
| 44 | + } |
| 45 | + const { message, signature } = credentials as Record<string, string>; |
| 46 | + const address = getAddressFromMessage(message); |
| 47 | + const chainId = getChainIdFromMessage(message); |
| 48 | + |
| 49 | + const publicClient = createPublicClient({ |
| 50 | + transport: http( |
| 51 | + `https://rpc.walletconnect.org/v1/?chainId=${chainId}&projectId=${projectId}` |
| 52 | + ), |
| 53 | + }); |
| 54 | + const isValid = await publicClient.verifyMessage({ |
| 55 | + message, |
| 56 | + address: address as `0x${string}`, |
| 57 | + signature: signature as `0x${string}`, |
| 58 | + }); |
| 59 | + // end o view verifyMessage |
| 60 | + |
| 61 | + if (isValid) { |
| 62 | + return { |
| 63 | + id: `${chainId}:${address}`, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + return null; |
| 68 | + } catch (e) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + }, |
| 72 | + }), |
| 73 | +]; |
| 74 | + |
13 | 75 | export const { |
14 | 76 | handlers: { GET, POST }, |
15 | 77 | auth, |
16 | 78 | signIn, |
17 | 79 | signOut, |
18 | 80 | } = NextAuth({ |
19 | 81 | ...authConfig, |
20 | | - providers: [ |
21 | | - Credentials({ |
22 | | - credentials: {}, |
23 | | - async authorize({ email, password }: any) { |
24 | | - const users = await getUser(email); |
25 | | - if (users.length === 0) return null; |
26 | | - // biome-ignore lint: Forbidden non-null assertion. |
27 | | - const passwordsMatch = await compare(password, users[0].password!); |
28 | | - if (!passwordsMatch) return null; |
29 | | - return users[0] as any; |
30 | | - }, |
31 | | - }), |
32 | | - ], |
33 | | - callbacks: { |
34 | | - async jwt({ token, user }) { |
35 | | - if (user) { |
36 | | - token.id = user.id; |
37 | | - } |
38 | | - |
39 | | - return token; |
40 | | - }, |
41 | | - async session({ |
42 | | - session, |
43 | | - token, |
44 | | - }: { |
45 | | - session: ExtendedSession; |
46 | | - token: any; |
47 | | - }) { |
48 | | - if (session.user) { |
49 | | - session.user.id = token.id as string; |
50 | | - } |
51 | | - |
52 | | - return session; |
53 | | - }, |
54 | | - }, |
| 82 | + providers, |
55 | 83 | }); |
0 commit comments