Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ts): handle NextRequest type #4472

Merged
merged 4 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next-auth/src/core/lib/cookie.ts
Expand Up @@ -120,7 +120,7 @@ export class SessionStore {
option: CookieOption,
req: {
cookies?: Record<string, string>
headers?: Record<string, string> | IncomingHttpHeaders
headers?: Headers | IncomingHttpHeaders | Record<string, string>
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
},
logger: LoggerInstance | Console
) {
Expand Down
15 changes: 10 additions & 5 deletions packages/next-auth/src/jwt/index.ts
Expand Up @@ -2,8 +2,8 @@ import { EncryptJWT, jwtDecrypt } from "jose"
import hkdf from "@panva/hkdf"
import { v4 as uuid } from "uuid"
import { SessionStore } from "../core/lib/cookie"
import { NextRequest } from "next/server"
import type { NextApiRequest} from "next"
import type { NextApiRequest } from "next"
import type { NextRequest } from "next/server"
import type { JWT, JWTDecodeParams, JWTEncodeParams, JWTOptions } from "./types"
import type { LoggerInstance } from ".."

Expand Down Expand Up @@ -38,7 +38,7 @@ export async function decode(params: JWTDecodeParams): Promise<JWT | null> {

export interface GetTokenParams<R extends boolean = false> {
/** The request containing the JWT either in the cookies or in the `Authorization` header. */
req: NextRequest | NextApiRequest | Pick<NextApiRequest, "cookies" | "headers">
req: NextRequest | NextApiRequest
/**
* Use secure prefix for cookie name, unless URL in `NEXTAUTH_URL` is http://
* or not set (e.g. development or test instance) case use unprefixed name
Expand Down Expand Up @@ -91,8 +91,13 @@ export async function getToken<R extends boolean = false>(

let token = sessionStore.value

if (!token && req.headers.authorization?.split(" ")[0] === "Bearer") {
const urlEncodedToken = req.headers.authorization.split(" ")[1]
let authorizationHeader =
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
req.headers instanceof Headers
? req.headers.get("authorization")
: req.headers.authorization

if (!token && authorizationHeader?.split(" ")[0] === "Bearer") {
const urlEncodedToken = authorizationHeader.split(" ")[1]
token = decodeURIComponent(urlEncodedToken)
}

Expand Down