Skip to content

Commit 681d53c

Browse files
committed
chore(core): cleanup
1 parent 06e891c commit 681d53c

File tree

8 files changed

+24
-29
lines changed

8 files changed

+24
-29
lines changed

packages/core/src/lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export async function AuthInternal<
7272
if (pages.signIn) {
7373
let signinUrl = `${pages.signIn}${
7474
pages.signIn.includes("?") ? "&" : "?"
75-
}callbackUrl=${encodeURIComponent(options.callbackUrl)}`
75+
}${new URLSearchParams({ callbackUrl: options.callbackUrl })}`
7676
if (error)
77-
signinUrl = `${signinUrl}&error=${encodeURIComponent(error)}`
77+
signinUrl = `${signinUrl}&${new URLSearchParams({ error })}`
7878
return { redirect: signinUrl, cookies }
7979
}
8080

packages/core/src/lib/oauth/authorization-url.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ export async function getAuthorizationUrl(
9090
}
9191

9292
logger.debug("authorization url is ready", { url, cookies, provider })
93-
return { redirect: url, cookies }
93+
return { redirect: url.toString(), cookies }
9494
}

packages/core/src/lib/routes/callback.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export async function callback(params: {
149149
return {
150150
redirect: `${pages.newUser}${
151151
pages.newUser.includes("?") ? "&" : "?"
152-
}callbackUrl=${encodeURIComponent(callbackUrl)}`,
152+
}${new URLSearchParams({ callbackUrl })}`,
153153
cookies,
154154
}
155155
}
@@ -256,7 +256,7 @@ export async function callback(params: {
256256
return {
257257
redirect: `${pages.newUser}${
258258
pages.newUser.includes("?") ? "&" : "?"
259-
}callbackUrl=${encodeURIComponent(callbackUrl)}`,
259+
}${new URLSearchParams({ callbackUrl })}`,
260260
cookies,
261261
}
262262
}
@@ -350,6 +350,6 @@ export async function callback(params: {
350350
logger.error(error)
351351
url.searchParams.set("error", CallbackRouteError.name)
352352
url.pathname += "/error"
353-
return { redirect: url, cookies }
353+
return { redirect: url.toString(), cookies }
354354
}
355355
}

packages/core/src/lib/routes/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export async function handleAuthorized(
1313
if (!authorized) {
1414
logger.debug("User not authorized", params)
1515
url.searchParams.set("error", "AccessDenied")
16-
return { status: 403 as const, redirect: url }
16+
return { status: 403 as const, redirect: url.toString() }
1717
}
1818
} catch (e) {
1919
const error = new AuthorizedCallbackError(e as Error)
2020
logger.error(error)
2121
url.searchParams.set("error", "Configuration")
22-
return { status: 500 as const, redirect: url }
22+
return { status: 500 as const, redirect: url.toString() }
2323
}
2424
}
2525

packages/core/src/lib/routes/signin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function signin(
5454
logger.error(error)
5555
url.searchParams.set("error", error.name)
5656
url.pathname += "/error"
57-
return { redirect: url }
57+
return { redirect: url.toString() }
5858
}
5959
}
6060

packages/core/src/lib/routes/signout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SessionStore } from "../cookie.js"
88
* If the session strategy is database,
99
* The session is also deleted from the database.
1010
* In any case, the session cookie is cleared and
11-
* an `events.signOut` is emitted.
11+
* {@link EventCallbacks.signOut} is emitted.
1212
*/
1313
export async function signout(
1414
sessionStore: SessionStore,

packages/core/src/lib/web.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,22 @@ export function toResponse(res: ResponseInternal): Response {
7676
res.cookies?.forEach((cookie) => {
7777
const { name, value, options } = cookie
7878
const cookieHeader = serialize(name, value, options)
79-
if (headers.has("Set-Cookie")) {
80-
headers.append("Set-Cookie", cookieHeader)
81-
} else {
82-
headers.set("Set-Cookie", cookieHeader)
83-
}
79+
if (headers.has("Set-Cookie")) headers.append("Set-Cookie", cookieHeader)
80+
else headers.set("Set-Cookie", cookieHeader)
8481
// headers.set("Set-Cookie", cookieHeader) // TODO: Remove. Seems to be a bug with Headers in the runtime
8582
})
8683

87-
const body =
88-
headers.get("content-type") === "application/json"
89-
? JSON.stringify(res.body)
90-
: res.body
84+
let body = res.body
9185

92-
const response = new Response(body, {
93-
headers,
94-
status: res.redirect ? 302 : res.status ?? 200,
95-
})
86+
if (headers.get("content-type") === "application/json")
87+
body = JSON.stringify(res.body)
88+
else if (headers.get("content-type") === "application/x-www-form-urlencoded")
89+
body = new URLSearchParams(res.body).toString()
9690

97-
if (res.redirect) {
98-
response.headers.set("Location", res.redirect.toString())
99-
}
91+
const status = res.redirect ? 302 : res.status ?? 200
92+
const response = new Response(body, { headers, status })
93+
94+
if (res.redirect) response.headers.set("Location", res.redirect)
10095

10196
return response
10297
}

packages/core/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
203203
* Its content is forwarded to the `session` callback,
204204
* where you can control what should be returned to the client.
205205
* Anything else will be kept inaccessible from the client.
206-
*
206+
*
207207
* Returning `null` will invalidate the JWT session by clearing
208208
* the user's cookies. You'll still have to monitor and invalidate
209209
* unexpired tokens from future requests yourself to prevent
@@ -220,7 +220,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
220220
account?: A | null
221221
profile?: P
222222
isNewUser?: boolean
223-
}) => Awaitable<JWT|null>
223+
}) => Awaitable<JWT | null>
224224
}
225225

226226
/** [Documentation](https://authjs.dev/reference/configuration/auth-config#cookies) */
@@ -452,7 +452,7 @@ export interface ResponseInternal<
452452
status?: number
453453
headers?: Headers | HeadersInit
454454
body?: Body
455-
redirect?: URL | string
455+
redirect?: string
456456
cookies?: Cookie[]
457457
}
458458

0 commit comments

Comments
 (0)