|
1 | 1 | import NextAuth, { type DefaultSession } from "next-auth" |
| 2 | +import Credentials from "next-auth/providers/credentials" |
2 | 3 | import GitHub from "next-auth/providers/github" |
| 4 | +import { SiweMessage } from "siwe" |
3 | 5 |
|
4 | | -import { storeUser } from "@/lib/data/kv" |
| 6 | +import { getUserIdByWallet, storeUser } from "@/lib/data/kv" |
5 | 7 |
|
6 | 8 | declare module "next-auth" { |
7 | 9 | interface Session extends DefaultSession { |
8 | 10 | user: { |
9 | 11 | id: string |
| 12 | + walletAddress?: string | null |
| 13 | + githubId?: string | null |
10 | 14 | } & DefaultSession["user"] |
11 | 15 | } |
12 | 16 | } |
13 | 17 |
|
| 18 | +declare module "next-auth/jwt" { |
| 19 | + interface JWT { |
| 20 | + id?: string |
| 21 | + walletAddress?: string | null |
| 22 | + githubId?: string | null |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function getCsrfTokenFromRequest(request: Request) { |
| 27 | + const cookieHeader = request.headers.get("cookie") |
| 28 | + if (!cookieHeader) return undefined |
| 29 | + |
| 30 | + const csrfCookie = cookieHeader |
| 31 | + .split(";") |
| 32 | + .map((part) => part.trim()) |
| 33 | + .find((part) => part.startsWith("next-auth.csrf-token=")) |
| 34 | + |
| 35 | + if (!csrfCookie) return undefined |
| 36 | + |
| 37 | + const [, value] = csrfCookie.split("=") |
| 38 | + return decodeURIComponent(value ?? "").split("|")[0] |
| 39 | +} |
| 40 | + |
| 41 | +function resolveDomain(request: Request) { |
| 42 | + const origin = request.headers.get("origin") |
| 43 | + if (origin) { |
| 44 | + return new URL(origin).host |
| 45 | + } |
| 46 | + |
| 47 | + const forwardedHost = request.headers.get("x-forwarded-host") |
| 48 | + const forwardedProto = request.headers.get("x-forwarded-proto") ?? "https" |
| 49 | + if (forwardedHost) { |
| 50 | + return new URL(`${forwardedProto}://${forwardedHost}`).host |
| 51 | + } |
| 52 | + |
| 53 | + return new URL(request.url).host |
| 54 | +} |
| 55 | + |
14 | 56 | export const { |
15 | 57 | handlers: { GET, POST }, |
16 | 58 | auth, |
17 | 59 | signIn, |
18 | 60 | signOut, |
19 | 61 | } = NextAuth({ |
20 | | - providers: [GitHub], |
| 62 | + providers: [ |
| 63 | + Credentials({ |
| 64 | + name: "Ethereum", |
| 65 | + credentials: { |
| 66 | + message: { |
| 67 | + label: "Message", |
| 68 | + type: "text", |
| 69 | + placeholder: "0x0", |
| 70 | + }, |
| 71 | + signature: { |
| 72 | + label: "Signature", |
| 73 | + type: "text", |
| 74 | + placeholder: "0x0", |
| 75 | + }, |
| 76 | + }, |
| 77 | + async authorize(credentials, request) { |
| 78 | + try { |
| 79 | + const message = credentials?.message |
| 80 | + const signature = credentials?.signature |
| 81 | + |
| 82 | + if (!message || typeof message !== "string") { |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + if (!signature || typeof signature !== "string") { |
| 87 | + return null |
| 88 | + } |
| 89 | + |
| 90 | + const siweMessage = new SiweMessage(JSON.parse(message)) |
| 91 | + const nonce = getCsrfTokenFromRequest(request) |
| 92 | + if (!nonce) { |
| 93 | + return null |
| 94 | + } |
| 95 | + |
| 96 | + const domain = resolveDomain(request) |
| 97 | + const verification = await siweMessage.verify({ |
| 98 | + signature, |
| 99 | + nonce, |
| 100 | + domain, |
| 101 | + }) |
| 102 | + |
| 103 | + if (!verification.success) { |
| 104 | + return null |
| 105 | + } |
| 106 | + |
| 107 | + const normalizedAddress = siweMessage.address.toLowerCase() |
| 108 | + const linkedUserId = await getUserIdByWallet(normalizedAddress) |
| 109 | + |
| 110 | + return { |
| 111 | + id: linkedUserId ?? normalizedAddress, |
| 112 | + address: siweMessage.address, |
| 113 | + walletAddress: siweMessage.address, |
| 114 | + } |
| 115 | + } catch (error) { |
| 116 | + console.error("Failed SIWE authorization", error) |
| 117 | + return null |
| 118 | + } |
| 119 | + }, |
| 120 | + }), |
| 121 | + GitHub, |
| 122 | + ], |
| 123 | + session: { |
| 124 | + strategy: "jwt", |
| 125 | + }, |
21 | 126 | callbacks: { |
22 | | - async jwt({ token, profile }) { |
23 | | - if (profile?.id) { |
| 127 | + async jwt({ token, user, account, profile }) { |
| 128 | + if (account?.provider === "credentials" && user) { |
| 129 | + const walletAddress = |
| 130 | + (user as { walletAddress?: string | null; address?: string | null })?.walletAddress || |
| 131 | + (user as { address?: string | null })?.address || |
| 132 | + String(user.id) |
| 133 | + |
| 134 | + token.walletAddress = walletAddress |
| 135 | + |
| 136 | + if (!token.id) { |
| 137 | + token.id = String(user.id) |
| 138 | + } |
| 139 | + |
| 140 | + await storeUser({ |
| 141 | + id: String(token.id), |
| 142 | + walletAddress, |
| 143 | + githubId: token.githubId ?? null, |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + if (account?.provider === "github" && profile) { |
24 | 148 | const profileId = String(profile.id) |
25 | 149 | token.id = profileId |
26 | | - const user = { |
27 | | - ...token, |
28 | | - ...profile, |
| 150 | + token.githubId = profileId |
| 151 | + token.name = profile.name ?? profile.login ?? token.name |
| 152 | + token.email = profile.email ?? token.email |
| 153 | + token.picture = (profile as { avatar_url?: string })?.avatar_url ?? token.picture |
| 154 | + |
| 155 | + await storeUser({ |
29 | 156 | id: profileId, |
30 | | - } |
31 | | - await storeUser(user) |
| 157 | + name: token.name ?? null, |
| 158 | + email: token.email ?? null, |
| 159 | + image: token.picture ?? null, |
| 160 | + githubId: profileId, |
| 161 | + walletAddress: token.walletAddress ?? null, |
| 162 | + }) |
32 | 163 | } |
| 164 | + |
33 | 165 | return token |
34 | 166 | }, |
35 | 167 |
|
36 | 168 | session({ session, token }) { |
| 169 | + if (!session.user) { |
| 170 | + return session |
| 171 | + } |
| 172 | + |
37 | 173 | if (token?.id) { |
38 | 174 | session.user.id = String(token.id) |
39 | 175 | } |
40 | | - return { |
41 | | - ...session, |
42 | | - user: { |
43 | | - ...session.user, |
44 | | - id: String(token.id), |
45 | | - }, |
| 176 | + |
| 177 | + session.user.walletAddress = token.walletAddress ?? null |
| 178 | + session.user.githubId = token.githubId ?? null |
| 179 | + |
| 180 | + if (token?.name && !session.user.name) { |
| 181 | + session.user.name = token.name |
46 | 182 | } |
| 183 | + |
| 184 | + if (token?.email && !session.user.email) { |
| 185 | + session.user.email = token.email |
| 186 | + } |
| 187 | + |
| 188 | + if (token?.picture && !session.user.image) { |
| 189 | + session.user.image = token.picture |
| 190 | + } |
| 191 | + |
| 192 | + return session |
47 | 193 | }, |
48 | 194 | }, |
49 | 195 | pages: { |
|
0 commit comments