Skip to content

Commit 128154f

Browse files
committed
Fix NextAuth typings for SIWE build
1 parent 4f36035 commit 128154f

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

auth.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
import NextAuth, { type DefaultSession } from "next-auth"
1+
import NextAuth from "next-auth"
22
import Credentials from "next-auth/providers/credentials"
33
import GitHub from "next-auth/providers/github"
44
import { SiweMessage } from "siwe"
55

66
import { getUserIdByWallet, storeUser } from "@/lib/data/kv"
7-
8-
declare module "next-auth" {
9-
interface Session extends DefaultSession {
10-
user: {
11-
id: string
12-
walletAddress?: string | null
13-
githubId?: string | null
14-
} & DefaultSession["user"]
15-
}
16-
}
17-
18-
declare module "next-auth/jwt" {
19-
interface JWT {
20-
id?: string
21-
walletAddress?: string | null
22-
githubId?: string | null
23-
}
24-
}
25-
267
function getCsrfTokenFromRequest(request: Request) {
278
const cookieHeader = request.headers.get("cookie")
289
if (!cookieHeader) return undefined
@@ -146,11 +127,28 @@ export const {
146127

147128
if (account?.provider === "github" && profile) {
148129
const profileId = String(profile.id)
130+
const profileData = profile as {
131+
name?: string | null
132+
login?: string | null
133+
email?: string | null
134+
avatar_url?: string | null
135+
}
136+
149137
token.id = profileId
150138
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
139+
140+
const profileName = profileData.name ?? profileData.login
141+
if (profileName) {
142+
token.name = profileName
143+
}
144+
145+
if (profileData.email) {
146+
token.email = profileData.email
147+
}
148+
149+
if (profileData.avatar_url) {
150+
token.picture = profileData.avatar_url
151+
}
154152

155153
await storeUser({
156154
id: profileId,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"include": [
4545
"next-env.d.ts",
46-
"next-auth.d.ts",
46+
"types/**/*.d.ts",
4747
"**/*.ts",
4848
"**/*.tsx",
4949
".next/types/**/*.ts",

types/next-auth.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { DefaultSession } from "next-auth"
2+
3+
declare module "next-auth" {
4+
interface Session extends DefaultSession {
5+
user: {
6+
id: string
7+
walletAddress?: string | null
8+
githubId?: string | null
9+
} & DefaultSession["user"]
10+
}
11+
}
12+
13+
declare module "@auth/core/jwt" {
14+
interface JWT {
15+
id?: string
16+
walletAddress?: string | null
17+
githubId?: string | null
18+
}
19+
}

0 commit comments

Comments
 (0)