Skip to content

Commit fe7aaed

Browse files
ThangHuuVubalazsorban44
andauthoredOct 13, 2022
fix(ts): TS Module Augmentation (#5556)
* fix: TS Module Augmentation * match type to AdapterUser * refactor authorize callback * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Balázs Orbán <info@balazsorban.com>
1 parent c53c09e commit fe7aaed

File tree

6 files changed

+41
-23
lines changed

6 files changed

+41
-23
lines changed
 

‎apps/dev/types/nextauth.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2+
import NextAuth from "next-auth"
3+
4+
declare module "next-auth" {
5+
/**
6+
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
7+
*/
8+
interface Session {
9+
user: {
10+
/** The user's postal address. */
11+
address: string
12+
} & User
13+
}
14+
15+
interface User {
16+
foo: string
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
import type { AdapterUser } from "../../../adapters"
12
import type { InternalOptions } from "../../types"
23

3-
export default async function getUserFromEmail({
4+
/**
5+
* Query the database for a user by email address.
6+
* If is an existing user return a user object (otherwise use placeholder).
7+
*/
8+
export default async function getAdapterUserFromEmail({
49
email,
510
adapter,
6-
withId = false,
711
}: {
812
email: string
913
adapter: InternalOptions<"email">["adapter"]
10-
withId: boolean
11-
}) {
14+
}): Promise<AdapterUser> {
1215
const { getUserByEmail } = adapter
13-
// If is an existing user return a user object (otherwise use placeholder)
14-
return (email ? await getUserByEmail(email) : null) ?? withId
15-
? { id: email, email }
16-
: {
17-
email,
18-
}
16+
const adapterUser = email ? await getUserByEmail(email) : null
17+
if (adapterUser) return adapterUser
18+
19+
return { id: email, email, emailVerified: null }
1920
}

‎packages/next-auth/src/core/routes/callback.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import oAuthCallback from "../lib/oauth/callback"
22
import callbackHandler from "../lib/callback-handler"
33
import { hashToken } from "../lib/utils"
4-
import getUserFromEmail from "../lib/email/getUserFromEmail"
4+
import getAdapterUserFromEmail from "../lib/email/getUserFromEmail"
55

66
import type { InternalOptions } from "../types"
77
import type { RequestInternal, OutgoingResponse } from ".."
@@ -217,7 +217,7 @@ export default async function callback(params: {
217217
return { redirect: `${url}/error?error=Verification`, cookies }
218218
}
219219

220-
const profile = await getUserFromEmail({
220+
const profile = await getAdapterUserFromEmail({
221221
email: identifier,
222222
// @ts-expect-error -- Verified in `assertConfig`. adapter: Adapter<true>
223223
adapter,
@@ -320,14 +320,14 @@ export default async function callback(params: {
320320
} else if (provider.type === "credentials" && method === "POST") {
321321
const credentials = body
322322

323-
let user: User
323+
let user: User | null
324324
try {
325-
user = (await provider.authorize(credentials, {
325+
user = await provider.authorize(credentials, {
326326
query,
327327
body,
328328
headers,
329329
method,
330-
})) as User
330+
})
331331
if (!user) {
332332
return {
333333
status: 401,

‎packages/next-auth/src/core/routes/signin.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getAuthorizationUrl from "../lib/oauth/authorization-url"
22
import emailSignin from "../lib/email/signin"
3-
import getUserFromEmail from "../lib/email/getUserFromEmail"
3+
import getAdapterUserFromEmail from "../lib/email/getUserFromEmail"
44
import type { RequestInternal, OutgoingResponse } from ".."
55
import type { InternalOptions } from "../types"
66
import type { Account } from "../.."
@@ -55,11 +55,10 @@ export default async function signin(params: {
5555
return { redirect: `${url}/error?error=EmailSignin` }
5656
}
5757

58-
const user = await getUserFromEmail({
58+
const user = await getAdapterUserFromEmail({
5959
email,
6060
// @ts-expect-error -- Verified in `assertConfig`. adapter: Adapter<true>
6161
adapter: options.adapter,
62-
withId: true,
6362
})
6463

6564
const account: Account = {

‎packages/next-auth/src/core/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
265265
* [Documentation](https://next-auth.js.org/configuration/callbacks#sign-in-callback)
266266
*/
267267
signIn: (params: {
268-
user: User | { email: string }
268+
user: User | AdapterUser
269269
account: A | null
270270
/**
271271
* If OAuth provider is used, it contains the full
@@ -317,7 +317,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
317317
*/
318318
session: (params: {
319319
session: Session
320-
user: User
320+
user: User | AdapterUser
321321
token: JWT
322322
}) => Awaitable<Session>
323323
/**
@@ -385,9 +385,9 @@ export interface EventCallbacks {
385385
createUser: (message: { user: User }) => Awaitable<void>
386386
updateUser: (message: { user: User }) => Awaitable<void>
387387
linkAccount: (message: {
388-
user: User | AdapterUser | { email: string }
388+
user: User | AdapterUser
389389
account: Account
390-
profile: User | AdapterUser | { email: string }
390+
profile: User | AdapterUser
391391
}) => Awaitable<void>
392392
/**
393393
* The message object will contain one of these depending on

‎packages/next-auth/src/providers/credentials.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface CredentialsConfig<
1717
authorize: (
1818
credentials: Record<keyof C, string> | undefined,
1919
req: Pick<RequestInternal, "body" | "query" | "headers" | "method">
20-
) => Awaitable<(Omit<User, "id"> | { id?: string }) | null>
20+
) => Awaitable<User | null>
2121
}
2222

2323
export type CredentialsProvider = <C extends Record<string, CredentialInput>>(

0 commit comments

Comments
 (0)
Please sign in to comment.