Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
pnpm publish --no-git-checks --access public --tag experimental
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN_PKG }}
- name: Comment version on PR
uses: NejcZdovc/comment-pr@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ packages/next-auth/providers
packages/next-auth/src/providers/oauth-types.ts
packages/next-auth/client
packages/next-auth/css
packages/next-auth/lib
packages/next-auth/utils
packages/next-auth/core
packages/next-auth/jwt
packages/next-auth/react
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-test/jest/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
transform: {
".(ts|tsx)$": "ts-jest",
".(js|jsx)$": "babel-jest", // jest's default
".(ts|tsx)$": "@swc/jest",
".(js|jsx)$": "@swc/jest", // jest's default
},
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@types/nodemailer": "^6.4.4",
"@typescript-eslint/eslint-plugin": "^4.24.0",
"@typescript-eslint/parser": "^4.24.0",
"babel-jest": "^27.4.2",
"eslint": "^7.27.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard-with-typescript": "^20.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/config/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ module.exports = (api) => {
ignore: [
"../src/**/__tests__/**",
"../src/adapters.ts",
"../src/lib/types.ts",
"../src/core/types.ts",
"../src/providers/oauth-types.ts",
],
comments: false,
overrides: [
{
test: [
"../src/react/index.tsx",
"../src/lib/logger.ts",
"../src/utils/logger.ts",
"../src/core/errors.ts",
"../src/client/**",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
transform: {
"\\.(js|jsx|ts|tsx)$": [
"babel-jest",
{ configFile: "./config/babel.config.js" },
],
"\\.(js|jsx|ts|tsx)$": ["@swc/jest", require("./swc.config")],
},
rootDir: "../src",
setupFilesAfterEnv: ["../config/jest-setup.js"],
Expand Down
13 changes: 13 additions & 0 deletions packages/next-auth/config/jest.core.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
transform: {
"\\.(js|jsx|ts|tsx)$": ["@swc/jest", require("./swc.config")],
},
rootDir: "..",
testMatch: ["**/*.test.ts"],
setupFilesAfterEnv: ["./config/jest-setup.js"],
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname",
],
}
17 changes: 17 additions & 0 deletions packages/next-auth/config/swc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
transform: {
react: {
runtime: "automatic",
pragma: "React.createElement",
pragmaFrag: "React.Fragment",
throwIfNamespace: true,
useBuiltins: true,
},
},
},
}
60 changes: 32 additions & 28 deletions packages/next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
},
"scripts": {
"build": "pnpm clean && pnpm build:js && pnpm build:css",
"clean": "rm -rf client css lib providers core jwt react next index.d.ts index.js adapters.d.ts middleware.d.ts middleware.js",
"clean": "rm -rf client css utils providers core jwt react next index.d.ts index.js adapters.d.ts middleware.d.ts middleware.js",
"build:js": "pnpm clean && pnpm generate-providers && tsc && babel --config-file ./config/babel.config.js src --out-dir . --extensions \".tsx,.ts,.js,.jsx\"",
"build:css": "postcss --config config/postcss.config.js src/**/*.css --base src --dir . && node config/wrap-css.js",
"watch:css": "postcss --config config/postcss.config.js --watch src/**/*.css --base src --dir .",
"test": "jest --config ./config/jest.config.js",
"test:client": "jest --config ./config/jest.client.config.js",
"test:core": "jest --config ./config/jest.core.config.js",
"test": "pnpm test:core && pnpm test:client",
"prepublishOnly": "pnpm build",
"generate-providers": "node ./config/generate-providers.js",
"setup": "pnpm generate-providers",
Expand Down Expand Up @@ -85,38 +87,40 @@
}
},
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/core": "^7.16.0",
"@babel/plugin-proposal-optional-catch-binding": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.4",
"@babel/preset-env": "^7.16.4",
"@babel/preset-react": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@babel/cli": "^7.17.10",
"@babel/core": "^7.18.2",
"@babel/plugin-proposal-optional-catch-binding": "^7.16.7",
"@babel/plugin-transform-runtime": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"@babel/preset-typescript": "^7.17.12",
"@next-auth/tsconfig": "workspace:^0.0.0",
"@testing-library/dom": "^8.11.3",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^13.5.0",
"@types/node": "^16.11.12",
"@swc/core": "^1.2.198",
"@swc/jest": "^0.2.21",
"@testing-library/dom": "^8.13.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/react-hooks": "^8.0.0",
"@testing-library/user-event": "^14.2.0",
"@types/node": "^17.0.42",
"@types/nodemailer": "^6.4.4",
"@types/oauth": "^0.9.1",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"autoprefixer": "^10.4.0",
"babel-jest": "^27.4.2",
"@types/react": "^18.0.2",
"@types/react-dom": "^18.0.5",
"autoprefixer": "^10.4.7",
"babel-plugin-jsx-pragmatic": "^1.0.2",
"babel-preset-preact": "^2.0.0",
"cssnano": "^5.0.12",
"jest": "^27.4.3",
"jest-watch-typeahead": "^1.0.0",
"msw": "^0.36.3",
"next": "12.1.0",
"postcss": "^8.4.12",
"postcss-cli": "^9.0.2",
"cssnano": "^5.1.11",
"jest": "^28.1.1",
"jest-environment-jsdom": "^28.1.1",
"jest-watch-typeahead": "^1.1.0",
"msw": "^0.42.1",
"next": "^12.1.6",
"postcss": "^8.4.14",
"postcss-cli": "^9.1.0",
"postcss-nested": "^5.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"whatwg-fetch": "^3.6.2"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/client/__tests__/csrf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { useState } from "react"
import userEvent from "@testing-library/user-event"
import { render, screen, waitFor } from "@testing-library/react"
import { server, mockCSRFToken } from "./helpers/mocks"
import logger from "../../lib/logger"
import logger from "../../utils/logger"
import { getCsrfToken } from "../../react"
import { rest } from "msw"

jest.mock("../../lib/logger", () => ({
jest.mock("../../utils/logger", () => ({
__esModule: true,
default: {
warn: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/client/__tests__/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import userEvent from "@testing-library/user-event"
import { render, screen, waitFor } from "@testing-library/react"
import { server, mockProviders } from "./helpers/mocks"
import { getProviders } from "../../react"
import logger from "../../lib/logger"
import logger from "../../utils/logger"
import { rest } from "msw"

jest.mock("../../lib/logger", () => ({
jest.mock("../../utils/logger", () => ({
__esModule: true,
default: {
warn: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/client/__tests__/session.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { render, screen, waitFor } from "@testing-library/react"
import { rest } from "msw"
import { server, mockSession } from "./helpers/mocks"
import logger from "../../lib/logger"
import logger from "../../utils/logger"
import { useState, useEffect } from "react"
import { getSession } from "../../react"
import { getBroadcastEvents } from "./helpers/utils"

jest.mock("../../lib/logger", () => ({
jest.mock("../../utils/logger", () => ({
__esModule: true,
default: {
warn: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/next-auth/src/client/__tests__/sign-in.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react"
import userEvent from "@testing-library/user-event"
import { render, screen, waitFor } from "@testing-library/react"
import logger from "../../lib/logger"
import logger from "../../utils/logger"
import {
server,
mockCredentialsResponse,
Expand All @@ -13,7 +13,7 @@ import { rest } from "msw"

const { location } = window

jest.mock("../../lib/logger", () => ({
jest.mock("../../utils/logger", () => ({
__esModule: true,
default: {
warn: jest.fn(),
Expand Down
51 changes: 44 additions & 7 deletions packages/next-auth/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import logger, { setLogger } from "../lib/logger"
import logger, { setLogger } from "../utils/logger"
import { detectHost } from "../utils/detect-host"
import * as routes from "./routes"
import renderPage from "./pages"
import { init } from "./init"
import { assertConfig } from "./lib/assert"
import { SessionStore } from "./lib/cookie"

import type { NextAuthOptions } from "./types"
import type { NextAuthAction } from "../lib/types"
import type { NextAuthAction, NextAuthOptions } from "./types"
import type { Cookie } from "./lib/cookie"
import type { ErrorType } from "./pages/error"

export interface IncomingRequest {
export interface RequestInternal {
/** @default "http://localhost:3000" */
host?: string
method?: string
Expand Down Expand Up @@ -39,18 +39,55 @@ export interface OutgoingResponse<
}

export interface NextAuthHandlerParams {
req: IncomingRequest
req: Request | RequestInternal
options: NextAuthOptions
}

async function getBody(req: Request): Promise<Record<string, any> | undefined> {
try {
return await req.json()
} catch {}
}

// TODO:
async function toInternalRequest(
req: RequestInternal | Request
): Promise<RequestInternal> {
if (req instanceof Request) {
const url = new URL(req.url)
// TODO: handle custom paths?
const nextauth = url.pathname.split("/").slice(3)
const headers = Object.fromEntries(req.headers.entries())
const query: Record<string, any> = Object.fromEntries(
url.searchParams.entries()
)
query.nextauth = nextauth

return {
action: nextauth[0] as NextAuthAction,
method: req.method,
headers,
body: await getBody(req),
cookies: {},
providerId: nextauth[1],
error: url.searchParams.get("error") ?? nextauth[1],
host: detectHost(headers["x-forwarded-host"] ?? headers.host),
query,
}
}
return req
}

export async function NextAuthHandler<
Body extends string | Record<string, any> | any[]
>(params: NextAuthHandlerParams): Promise<OutgoingResponse<Body>> {
const { options: userOptions, req } = params
const { options: userOptions, req: incomingRequest } = params

const req = await toInternalRequest(incomingRequest)

setLogger(userOptions.logger, userOptions.debug)

const assertionResult = assertConfig(params)
const assertionResult = assertConfig({ options: userOptions, req })

if (typeof assertionResult === "string") {
logger.warn(assertionResult)
Expand Down
11 changes: 6 additions & 5 deletions packages/next-auth/src/core/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextAuthOptions } from ".."
import logger from "../lib/logger"
import parseUrl from "../lib/parse-url"
import { InternalOptions } from "../lib/types"
import logger from "../utils/logger"
import parseUrl from "../utils/parse-url"
import { adapterErrorHandler, eventsErrorHandler } from "./errors"
import parseProviders from "./lib/providers"
import createSecret from "./lib/utils"
Expand All @@ -10,7 +9,9 @@ import * as jwt from "../jwt"
import { defaultCallbacks } from "./lib/default-callbacks"
import { createCSRFToken } from "./lib/csrf-token"
import { createCallbackUrl } from "./lib/callback-url"
import { IncomingRequest } from "."
import { RequestInternal } from "."

import type { InternalOptions } from "./types"

interface InitParams {
host?: string
Expand All @@ -23,7 +24,7 @@ interface InitParams {
csrfToken?: string
/** Is the incoming request a POST request? */
isPost: boolean
cookies: IncomingRequest["cookies"]
cookies: RequestInternal["cookies"]
}

/** Initialize all internal options and cookies. */
Expand Down
10 changes: 6 additions & 4 deletions packages/next-auth/src/core/lib/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
UnsupportedStrategy,
InvalidCallbackUrl,
} from "../errors"
import parseUrl from "../../lib/parse-url"
import parseUrl from "../../utils/parse-url"
import { defaultCookies } from "./cookie"

import type { NextAuthHandlerParams } from ".."
import type { WarningCode } from "../../lib/logger"
import type { NextAuthHandlerParams, RequestInternal } from ".."
import type { WarningCode } from "../../utils/logger"

type ConfigError =
| MissingAPIRoute
Expand Down Expand Up @@ -38,7 +38,9 @@ function isValidHttpUrl(url: string, baseUrl: string) {
* REVIEW: Make some of these and corresponding docs less Next.js specific?
*/
export function assertConfig(
params: NextAuthHandlerParams
params: NextAuthHandlerParams & {
req: RequestInternal
}
): ConfigError | WarningCode | undefined {
const { options, req } = params

Expand Down
13 changes: 7 additions & 6 deletions packages/next-auth/src/core/lib/callback-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { randomBytes, randomUUID } from "crypto"
import { AccountNotLinkedError } from "../errors"
import { fromDate } from "./utils"
import { randomBytes, randomUUID } from "crypto"
import { InternalOptions } from "../../lib/types"
import { AdapterSession, AdapterUser } from "../../adapters"
import { JWT } from "../../jwt"
import { Account, User } from "../.."
import { SessionToken } from "./cookie"

import type { InternalOptions } from "../types"
import type { AdapterSession, AdapterUser } from "../../adapters"
import type { JWT } from "../../jwt"
import type { Account, User } from "../.."
import type { SessionToken } from "./cookie"

/**
* This function handles the complex flow of signing users in, and either creating,
Expand Down
Loading