Compare commits

...

5 Commits

Author SHA1 Message Date
Balázs Orbán
44181750a5 change in core 2022-12-31 10:25:06 +01:00
Balázs Orbán
eda557e147 Merge branch 'main' into fix/add-function-overload-to-jwt 2022-12-31 09:39:44 +01:00
johnmarsden24
8751c18f26 had wrong import 2022-11-16 09:24:44 +00:00
johnmarsden24
4752f5d6c4 use MissingSecret error instead 2022-11-16 09:23:42 +00:00
johnmarsden24
7e7f018d3b added function overload to getToken 2022-11-15 18:02:33 +00:00

View File

@@ -41,6 +41,7 @@ import { EncryptJWT, jwtDecrypt } from "jose"
import { SessionStore } from "./lib/cookie.js"
import { Awaitable } from "./types.js"
import type { LoggerInstance } from "./lib/utils/logger.js"
import { MissingSecret } from "./errors.js"
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60 // 30 days
@@ -97,13 +98,16 @@ export interface GetTokenParams<R extends boolean = false> {
}
/**
* Takes a Auth.js request (`req`) and returns either the Auth.js issued JWT's payload,
* Takes an Auth.js request (`req`) and returns either the Auth.js issued JWT's payload,
* or the raw JWT string. We look for the JWT in the either the cookies, or the `Authorization` header.
* [Documentation](https://authjs.dev/guides/basics/securing-pages-and-api-routes#using-gettoken)
*/
export async function getToken<R extends boolean = false>(
params: GetTokenParams<R>
): Promise<R extends true ? string : JWT | null> {
): Promise<R extends true ? string : JWT | null>
export async function getToken(
params: GetTokenParams
): Promise<string | JWT | null> {
const {
req,
secureCookie = process.env.NEXTAUTH_URL?.startsWith("https://") ??
@@ -118,6 +122,8 @@ export async function getToken<R extends boolean = false>(
} = params
if (!req) throw new Error("Must pass `req` to JWT getToken()")
if (!secret)
throw new MissingSecret("Must pass `secret` if not set to JWT getToken()")
const sessionStore = new SessionStore(
{ name: cookieName, options: { secure: secureCookie } },
@@ -138,17 +144,13 @@ export async function getToken<R extends boolean = false>(
token = decodeURIComponent(urlEncodedToken)
}
// @ts-expect-error
if (!token) return null
// @ts-expect-error
if (raw) return token
try {
// @ts-expect-error
return await _decode({ token, secret })
} catch {
// @ts-expect-error
return null
}
}