mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
* WIP use `Request` and `Response` for core * bump Next.js * rename ts types * refactor * simplify * upgrade Next.js * implement body reader * use `Request`/`Response` in `next-auth/next` * make linter happy * revert * fix tests * remove workaround for middleware return type * return session in protected api route example * don't export internal handler * fall back host to localhost * refactor `getBody` * refactor `next-auth/next` * chore: add `@edge-runtime/jest-environment` * fix tests, using Node 18 as runtime * fix test * remove patch * upgrade/add dependencies * type and default import on one line * don't import all adapters by default in dev * simplify internal endpoint config Instead of passing url and params around as a string and an object, we parse them into a `URL` instance. * assert if both endpoint and issuer config is missing * allow internal redirect to be `URL` * mark clientId as always internally, fix comments * add web-compatible authorization URL handling * fix type * fix neo4j build * remove new-line * reduce file changes in the PR * simplify types * refactor `crypto` usage In Node.js, inject `globalThis.crypto` instead of import * add `next-auth/web` * refactor * send header instead of body to indicate redirect response * fix eslint * fix tests * chore: upgrade dep * fix import * refactor: more renames * wip core * support OIDC * remove `openid-client` * temprarily remove duplicate logos * revert * move redirect logic to core * wip fix css * revert Logo component * output ESM * fix logout * deprecate OAuth 1, simplify internals, improve defaults * refactor providers, test facebook * fix providers * target es2020 * fix CSS * update lock file * make logos optional * sync with `next-auth` * clean up `next-auth/edge` * sync * remove uuid * make secret required in dev * remove todo comments * pass through OAuth client options * generate declaration map * default env secret to AUTH_SECRET * temporary Headers fix * move pages to lib * move errors to lib * move pages/index to lib * move routes to lib * move init to lib * move styles to lib * move types to lib * move utils to lib * fix imports * update ignore/clean patterns * fix imports * update styles ts * update gitignore * update exports field * revert `next-auth` * remove extra tsconfig files * remove `private` from package.json * remove unused file, expose type * move gitignore, reduce exposed types * add back tsconfig files * remove leftover * revert gitignore * remove test script
113 lines
3.1 KiB
TypeScript
113 lines
3.1 KiB
TypeScript
import { UnknownError } from "../errors"
|
|
|
|
/** Makes sure that error is always serializable */
|
|
function formatError(o: unknown): unknown {
|
|
if (o instanceof Error && !(o instanceof UnknownError)) {
|
|
return { message: o.message, stack: o.stack, name: o.name }
|
|
}
|
|
if (hasErrorProperty(o)) {
|
|
o.error = formatError(o.error) as Error
|
|
o.message = o.message ?? o.error.message
|
|
}
|
|
return o
|
|
}
|
|
|
|
function hasErrorProperty(
|
|
x: unknown
|
|
): x is { error: Error; [key: string]: unknown } {
|
|
return !!(x as any)?.error
|
|
}
|
|
|
|
export type WarningCode = "NEXTAUTH_URL" | "DEBUG_ENABLED"
|
|
|
|
/**
|
|
* Override any of the methods, and the rest will use the default logger.
|
|
*
|
|
* [Documentation](https://next-auth.js.org/configuration/options#logger)
|
|
*/
|
|
export interface LoggerInstance extends Record<string, Function> {
|
|
warn: (code: WarningCode) => void
|
|
error: (
|
|
code: string,
|
|
/**
|
|
* Either an instance of (JSON serializable) Error
|
|
* or an object that contains some debug information.
|
|
* (Error is still available through `metadata.error`)
|
|
*/
|
|
metadata: Error | { error: Error; [key: string]: unknown }
|
|
) => void
|
|
debug: (code: string, metadata: unknown) => void
|
|
}
|
|
|
|
const _logger: LoggerInstance = {
|
|
error(code, metadata) {
|
|
metadata = formatError(metadata) as Error
|
|
console.error(
|
|
`[next-auth][error][${code}]`,
|
|
`\nhttps://next-auth.js.org/errors#${code.toLowerCase()}`,
|
|
metadata.message,
|
|
metadata
|
|
)
|
|
},
|
|
warn(code) {
|
|
console.warn(
|
|
`[next-auth][warn][${code}]`,
|
|
`\nhttps://next-auth.js.org/warnings#${code.toLowerCase()}`
|
|
)
|
|
},
|
|
debug(code, metadata) {
|
|
console.log(`[next-auth][debug][${code}]`, metadata)
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Override the built-in logger with user's implementation.
|
|
* Any `undefined` level will use the default logger.
|
|
*/
|
|
export function setLogger(
|
|
newLogger: Partial<LoggerInstance> = {},
|
|
debug?: boolean
|
|
) {
|
|
// Turn off debug logging if `debug` isn't set to `true`
|
|
if (!debug) _logger.debug = () => {}
|
|
|
|
if (newLogger.error) _logger.error = newLogger.error
|
|
if (newLogger.warn) _logger.warn = newLogger.warn
|
|
if (newLogger.debug) _logger.debug = newLogger.debug
|
|
}
|
|
|
|
export default _logger
|
|
|
|
/** Serializes client-side log messages and sends them to the server */
|
|
export function proxyLogger(
|
|
logger: LoggerInstance = _logger,
|
|
basePath?: string
|
|
): LoggerInstance {
|
|
try {
|
|
if (typeof window === "undefined") {
|
|
return logger
|
|
}
|
|
|
|
const clientLogger: Record<string, unknown> = {}
|
|
for (const level in logger) {
|
|
clientLogger[level] = (code: string, metadata: Error) => {
|
|
_logger[level](code, metadata) // Logs to console
|
|
|
|
if (level === "error") {
|
|
metadata = formatError(metadata) as Error
|
|
}
|
|
;(metadata as any).client = true
|
|
const url = `${basePath}/_log`
|
|
const body = new URLSearchParams({ level, code, ...(metadata as any) })
|
|
if (navigator.sendBeacon) {
|
|
return navigator.sendBeacon(url, body)
|
|
}
|
|
return fetch(url, { method: "POST", body, keepalive: true })
|
|
}
|
|
}
|
|
return clientLogger as unknown as LoggerInstance
|
|
} catch {
|
|
return _logger
|
|
}
|
|
}
|