{"_id":"@authok/nextjs-authok","_rev":"1-0747a15ccab05f009187d1c417f6bf3b","name":"@authok/nextjs-authok","dist-tags":{"latest":"2.0.2"},"versions":{"2.0.1":{"name":"@authok/nextjs-authok","version":"2.0.1","description":"Next.js SDK for signing in with Authok","exports":{".":"./dist/index.js","./client":"./dist/client/index.js","./edge":"./dist/edge.js","./testing":"./dist/helpers/testing.js"},"main":"dist/index.js","types":"dist/index.d.ts","browser":"dist/client/index.js","directories":{"test":"tests"},"engines":{"node":">=12.19.0"},"scripts":{"clean":"rimraf dist","pretty":"prettier --write \"src/**/*.{ts,tsx}\" \"src/*.ts\"","lint":"tsc --noEmit && eslint --fix --ext .ts ./src","docs":"typedoc --options typedoc.js src","prepack":"npm run build","install:basic":"npm i --prefix=examples/basic-example --no-package-lock","install:kitchen-sink":"npm i --prefix=examples/kitchen-sink-example --no-package-lock","install:examples":"npm run install:basic && npm run install:kitchen-sink","build":"npm run clean && tsc -p tsconfig.build.json","build:test":"next build tests/fixtures/test-app","build:basic":"npm run build --prefix=examples/basic-example","build:kitchen-sink":"npm run build --prefix=examples/kitchen-sink-example","build:examples":"npm run build:basic && npm run build:kitchen-sink","build:vercel":"npm run install:kitchen-sink && npm run build && npm run build:kitchen-sink","start:basic":"npm run dev --prefix=examples/basic-example","start:kitchen-sink":"npm run dev --prefix=examples/kitchen-sink-example","start:kitchen-sink-local":"npm run dev:local --prefix=examples/kitchen-sink-example","test":"jest tests --coverage --maxWorkers=4","test:watch":"jest --coverage --watch","test:kitchen-sink":"start-server-and-test start:kitchen-sink http-get://localhost:3000 cypress:run","test:kitchen-sink:watch":"start-server-and-test start:kitchen-sink 3000 cypress:open","test:kitchen-sink-local":"npx start-server-and-test 'start:kitchen-sink-local' http://localhost:3000 'cypress run --config-file=./cypress-local.config.js'","test:kitchen-sink-local:watch":"npx start-server-and-test 'start:kitchen-sink-local' http://localhost:3000 'cypress open --config-file=./cypress-local.config.js'","cypress:run":"cypress run","cypress:open":"cypress open"},"repository":{"type":"git","url":"git+https://github.com/authok/nextjs-authok.git"},"keywords":["authok","next.js","react","oidc","authentication","vercel"],"author":{"name":"Authok","url":"https://authok.cn"},"license":"MIT","bugs":{"url":"https://github.com/authok/nextjs-authok/issues"},"homepage":"https://github.com/authok/nextjs-authok#readme","devDependencies":{"@edge-runtime/jest-environment":"^1.1.0-beta.31","@testing-library/jest-dom":"^5.11.9","@testing-library/react":"^13.4.0","@testing-library/react-hooks":"^5.0.3","@types/body-parser":"^1.19.0","@types/clone":"^2.1.0","@types/cookie":"^0.5.1","@types/debug":"^4.1.5","@types/http-errors":"^1.8.0","@types/jest":"^27.0.1","@types/jsonwebtoken":"^8.5.0","@types/node":"^14.14.20","@types/on-headers":"^1.0.0","@types/react":"^18.0.24","@types/react-dom":"^17.0.9","@types/tough-cookie":"^4.0.0","@types/url-join":"^4.0.0","@types/webpack":"^4.41.26","@typescript-eslint/eslint-plugin":"^4.13.0","@typescript-eslint/parser":"^4.13.0","body-parser":"^1.19.0","browserstack-cypress-cli":"^1.19.1","cypress":"^11.2.0","eslint":"^7.17.0","eslint-config-prettier":"^8.1.0","eslint-import-resolver-typescript":"^3.5.2","eslint-plugin-import":"^2.22.1","eslint-plugin-jsx-a11y":"^6.4.1","eslint-plugin-prettier":"^3.3.1","eslint-plugin-react":"^7.22.0","eslint-plugin-react-hooks":"^4.2.0","jest":"^27.2.0","next":"^13.0.3","nock":"^13.0.5","oidc-provider":"^7.6.0","on-headers":"^1.0.2","prettier":"^2.2.1","react":"^18.2.0","react-dom":"^18.2.0","react-test-renderer":"^18.2.0","rimraf":"^3.0.2","start-server-and-test":"^1.11.7","timekeeper":"^2.2.0","tough-cookie":"^4.0.0","ts-jest":"^27.0.5","typedoc":"^0.20.37","typescript":"^4.1.3"},"dependencies":{"@panva/hkdf":"^1.0.2","cookie":"^0.5.0","debug":"^4.3.4","http-errors":"^1.8.1","joi":"^17.6.0","jose":"^4.9.2","openid-client":"^5.2.1","tslib":"^2.4.0","url-join":"^4.0.1"},"peerDependencies":{"next":">=10"},"jest":{"testEnvironment":"node","rootDir":".","moduleFileExtensions":["ts","tsx","js"],"collectCoverageFrom":["<rootDir>/src/**/*.*","!<rootDir>/src/edge.ts","!<rootDir>/src/authok-session/config.ts","!<rootDir>/src/authok-session/index.ts","!<rootDir>/src/authok-session/session-cache.ts"],"coverageReporters":["lcov","text","text-summary"],"coverageThreshold":{"global":{"branches":100,"functions":100,"lines":100,"statements":100}},"coverageProvider":"v8","preset":"ts-jest","globalSetup":"./tests/global-setup.ts","setupFilesAfterEnv":["./tests/setup.ts"]},"gitHead":"4f4a9b9eb506ec466902c7dd736f633266872df0","_id":"@authok/nextjs-authok@2.0.1","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-EXSAkRWRLvMyGYA3oOztx6vzYQU89UJpT5P7HWH4CnA6FQ4YIZb0vjLszPEe285o3jm4CX1IjEHlAusuWsbIhw==","shasum":"67aee9a2a1bb542c18ce0b1e9b0509aa54e6ccc9","tarball":"https://registry.npmjs.org/@authok/nextjs-authok/-/nextjs-authok-2.0.1.tgz","fileCount":244,"unpackedSize":527556,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCm1HrDCq+raaWp80at+8ZydBh/OLZvQpojKRWhCiQ79QIgNeOJFrJfSMZBXDPTrbT/ZYXNNcUeZ7j6ROBuwRJYIHw="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjlPY+ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqmQxAAl2Gyogq1CfEbF7XEceuNsJsKXdKaSbmNdNG+HrojClyi0toX\r\n7Au3vnsco/nj4WnBDmIA8sH1M7eWOeqz8YURLmPuBfrXu8uPtLk97fImTFOr\r\nV2Gq4a2E8PPA4prcLpwk8bGhj0do99MBkqOEJSO5qjSEbxcWYRhGIOPZRmRO\r\nKF1YGGzUywzeXNriscNOl5psrmul5czvmKIppMV9wDJKu0LL9bjW/9smPHto\r\ny0FApdfnkPPy1YvEXxG4nUWoFLjHtDPlrdjhL7jMvbp5KLMroIQVChP2sq5l\r\nh8HXZ/IrdPqPbLYITGXS5n+juRbKKd8CxvNn5jGvONxQu2xkuo4Bs79AwWmD\r\nyyhJEonYDG8iKy7jDTGgrnOuPZrzMKSRwalAGDVZ6pEU2J8IWsUwkpctVvIF\r\n90QsuPw19s9vVxlzZNX/Q3FgyS776NhAXIj6BcTN16sTWOfviJGatGqIscWt\r\nemh9x0a3UVQ/Y5Va6IbUWhg9yFjui3rGul3WQCI1oZzo/9r1mlFwnduCuur5\r\nLGaUMZjk218ufAVawp8Yc+rYgHaZ6XsRh4MT8ncf+RTcVfA8nMOy875ohXdO\r\nJnBhWBouS5LKmJdaghNnXF4+CaUSS4jFNGq6U+B8DHZZPyPs98d/wo7X8sqj\r\nfkOTWfv6XSD/uhTvz6mWYYLZZtUFsiZj3iA=\r\n=37zV\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"newtalentxp","email":"newtalentxp@126.com"},"maintainers":[{"name":"newtalentxp","email":"newtalentxp@126.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nextjs-authok_2.0.1_1670706750154_0.8318389600919132"},"_hasShrinkwrap":false},"2.0.2":{"name":"@authok/nextjs-authok","version":"2.0.2","description":"Next.js SDK for signing in with Authok","exports":{".":"./dist/index.js","./client":"./dist/client/index.js","./edge":"./dist/edge.js","./testing":"./dist/helpers/testing.js"},"main":"dist/index.js","types":"dist/index.d.ts","browser":"dist/client/index.js","directories":{"test":"tests"},"engines":{"node":">=12.19.0"},"scripts":{"clean":"rimraf dist","pretty":"prettier --write \"src/**/*.{ts,tsx}\" \"src/*.ts\"","lint":"tsc --noEmit && eslint --fix --ext .ts ./src","docs":"typedoc --options typedoc.js src","prepack":"npm run build","install:basic":"npm i --prefix=examples/basic-example --no-package-lock","install:kitchen-sink":"npm i --prefix=examples/kitchen-sink-example --no-package-lock","install:examples":"npm run install:basic && npm run install:kitchen-sink","build":"npm run clean && tsc -p tsconfig.build.json","build:test":"next build tests/fixtures/test-app","build:basic":"npm run build --prefix=examples/basic-example","build:kitchen-sink":"npm run build --prefix=examples/kitchen-sink-example","build:examples":"npm run build:basic && npm run build:kitchen-sink","build:vercel":"npm run install:kitchen-sink && npm run build && npm run build:kitchen-sink","start:basic":"npm run dev --prefix=examples/basic-example","start:kitchen-sink":"npm run dev --prefix=examples/kitchen-sink-example","start:kitchen-sink-local":"npm run dev:local --prefix=examples/kitchen-sink-example","test":"jest tests --coverage --maxWorkers=4","test:watch":"jest --coverage --watch","test:kitchen-sink":"start-server-and-test start:kitchen-sink http-get://localhost:3000 cypress:run","test:kitchen-sink:watch":"start-server-and-test start:kitchen-sink 3000 cypress:open","test:kitchen-sink-local":"npx start-server-and-test 'start:kitchen-sink-local' http://localhost:3000 'cypress run --config-file=./cypress-local.config.js'","test:kitchen-sink-local:watch":"npx start-server-and-test 'start:kitchen-sink-local' http://localhost:3000 'cypress open --config-file=./cypress-local.config.js'","cypress:run":"cypress run","cypress:open":"cypress open"},"repository":{"type":"git","url":"git+https://github.com/authok/nextjs-authok.git"},"keywords":["authok","next.js","react","oidc","authentication","vercel"],"author":{"name":"Authok","url":"https://authok.cn"},"license":"MIT","bugs":{"url":"https://github.com/authok/nextjs-authok/issues"},"homepage":"https://github.com/authok/nextjs-authok#readme","devDependencies":{"@edge-runtime/jest-environment":"^1.1.0-beta.31","@testing-library/jest-dom":"^5.11.9","@testing-library/react":"^13.4.0","@testing-library/react-hooks":"^5.0.3","@types/body-parser":"^1.19.0","@types/clone":"^2.1.0","@types/cookie":"^0.5.1","@types/debug":"^4.1.5","@types/http-errors":"^1.8.0","@types/jest":"^27.0.1","@types/jsonwebtoken":"^8.5.0","@types/node":"^14.14.20","@types/on-headers":"^1.0.0","@types/react":"^18.0.24","@types/react-dom":"^17.0.9","@types/tough-cookie":"^4.0.0","@types/url-join":"^4.0.0","@types/webpack":"^4.41.26","@typescript-eslint/eslint-plugin":"^4.13.0","@typescript-eslint/parser":"^4.13.0","body-parser":"^1.19.0","browserstack-cypress-cli":"^1.19.1","cypress":"^11.2.0","eslint":"^7.17.0","eslint-config-prettier":"^8.1.0","eslint-import-resolver-typescript":"^3.5.2","eslint-plugin-import":"^2.22.1","eslint-plugin-jsx-a11y":"^6.4.1","eslint-plugin-prettier":"^3.3.1","eslint-plugin-react":"^7.22.0","eslint-plugin-react-hooks":"^4.2.0","jest":"^27.2.0","next":"^13.0.3","nock":"^13.0.5","oidc-provider":"^7.6.0","on-headers":"^1.0.2","prettier":"^2.2.1","react":"^18.2.0","react-dom":"^18.2.0","react-test-renderer":"^18.2.0","rimraf":"^3.0.2","start-server-and-test":"^1.11.7","timekeeper":"^2.2.0","tough-cookie":"^4.0.0","ts-jest":"^27.0.5","typedoc":"^0.20.37","typescript":"^4.1.3"},"dependencies":{"@panva/hkdf":"^1.0.2","cookie":"^0.5.0","debug":"^4.3.4","http-errors":"^1.8.1","joi":"^17.6.0","jose":"^4.9.2","openid-client":"^5.2.1","tslib":"^2.4.0","url-join":"^4.0.1"},"peerDependencies":{"next":">=10"},"jest":{"testEnvironment":"node","rootDir":".","moduleFileExtensions":["ts","tsx","js"],"collectCoverageFrom":["<rootDir>/src/**/*.*","!<rootDir>/src/edge.ts","!<rootDir>/src/authok-session/config.ts","!<rootDir>/src/authok-session/index.ts","!<rootDir>/src/authok-session/session-cache.ts"],"coverageReporters":["lcov","text","text-summary"],"coverageThreshold":{"global":{"branches":100,"functions":100,"lines":100,"statements":100}},"coverageProvider":"v8","preset":"ts-jest","globalSetup":"./tests/global-setup.ts","setupFilesAfterEnv":["./tests/setup.ts"]},"gitHead":"b644d36ba6819c8ba844ab3dcb595385edc35738","_id":"@authok/nextjs-authok@2.0.2","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-YcmV7amuKj9xTTsadmMjgjE3+XEMNjB6bXeyqW56kD/U283gMi9YO9KVV+YDZ7uGi7sTRgBs26j6+tnnJXi7aw==","shasum":"92984fd72faca269afe5eb4402d7234d472c75cd","tarball":"https://registry.npmjs.org/@authok/nextjs-authok/-/nextjs-authok-2.0.2.tgz","fileCount":244,"unpackedSize":527049,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDPPypdB6gcuyVTvI9Jq1WbzhNdYbcqciFoOKFf0TrgpAIgVxCV+1MalTSgrfg/EkkIhYujzUePqvb9tlGOHzfIhVQ="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjlXc2ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoTkw//d3hQ7DG0RWVB1EUerr9A1EMwPZiQP+rLZWfT6JSj+L+EAKm9\r\nRVYlIoRlLz/Uc1w6DQFJkEB/sUZE9pRXI8umkjOmxV/iILr1CvcToBmO/xS8\r\n75u5buz1iZnzB5Wz5S0IQ7K7X2oJJioZRbKIXGvBhlDtuaxVee564+J4+W9B\r\nsXgmPzEX7zNdqgl3axnT7mgntkeOXGJMMim5ilG9pBtAjDdRg5BilTpKgePM\r\n4V7QrglOgfR92sqCRpHPt3l0ZU7/N5Mk37yPM2h/canLJr+zwkwJcGUM7EPJ\r\n9iRrIyVko+zjFYeTMo0RwSWUyv0aLzkEIbInXIi3OvJui2V2T0Y0c6imsAQe\r\nT9espo0nffTWjwx2HiCizgCboQiaM+WbAvRzx1IXix3jtFo/2h798Ix1XRxV\r\n04K5LAKJdRcjXdlK8UEezpukkf6zWcCeYjsFyMdFNigXBvwfmHc+XmEeeJpW\r\nTJtjNpNVXYfaKMhAUaGBvVGGAfmq3boTH1j0AzNEoq+AW714tNpirypte/F5\r\ngecqbpluAzZT8sctqW88TdY0n7HK3ARnfNZZiidq9ii7pLRcJVa49uDs2IEy\r\nQTbYhR19q5ji9sEZDDcgzMuCoAFu+NOdaG6sDNflFWDhV6iFe7IYGyHV6KYD\r\nKmV3KqaOdqez7QHjIJ9v9i3L4MoZpy7ROds=\r\n=naTu\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"newtalentxp","email":"newtalentxp@126.com"},"maintainers":[{"name":"newtalentxp","email":"newtalentxp@126.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nextjs-authok_2.0.2_1670739766236_0.25032264047236996"},"_hasShrinkwrap":false}},"time":{"created":"2022-12-10T21:12:30.061Z","2.0.1":"2022-12-10T21:12:30.311Z","modified":"2022-12-11T06:22:46.454Z","2.0.2":"2022-12-11T06:22:46.360Z"},"maintainers":[{"name":"newtalentxp","email":"newtalentxp@126.com"}],"description":"Next.js SDK for signing in with Authok","homepage":"https://github.com/authok/nextjs-authok#readme","keywords":["authok","next.js","react","oidc","authentication","vercel"],"repository":{"type":"git","url":"git+https://github.com/authok/nextjs-authok.git"},"author":{"name":"Authok","url":"https://authok.cn"},"bugs":{"url":"https://github.com/authok/nextjs-authok/issues"},"license":"MIT","readme":"![nextjs-authok](https://cdn.authok.cn/website/sdks/banners/nextjs-authok-banner.png)\n\nThe Authok Next.js SDK is a library for implementing user authentication in Next.js applications.\n\n![Release](https://img.shields.io/npm/v/@authok/nextjs-authok)\n[![Coverage](https://img.shields.io/badge/dynamic/json?color=brightgreen&label=coverage&query=jest.coverageThreshold.global.lines&suffix=%25&url=https%3A%2F%2Fraw.githubusercontent.com%2Fauthok%2Fnextjs-authok%2Fmain%2Fpackage.json)](https://github.com/authok/nextjs-authok/blob/main/package.json#L147)\n![Downloads](https://img.shields.io/npm/dw/@authok/nextjs-authok)\n[![License](https://img.shields.io/:license-mit-blue.svg?style=flat)](https://opensource.org/licenses/MIT)\n![CircleCI](https://img.shields.io/circleci/build/github/authok/nextjs-authok)\n\n📚 [文档](#documentation) - 🚀 [快速开始](#getting-started)- 💻 [API 参考](#api-reference) - 💬 [反馈](#feedback)\n\n## 文档\n\n- [快速开始](https://authok.cn/docs/quickstart/webapp/nextjs)- 在您的 Next.js 应用中引入 authok.\n- [FAQs](https://github.com/authok/nextjs-authok/blob/main/FAQ.md) - 关于 nextjs-authok 的常见问题.\n- [示例](https://github.com/authok/nextjs-authok/blob/main/EXAMPLES.md) - 不同使用场景的示例.\n- [安全](https://github.com/authok/nextjs-authok/blob/main/SECURITY.md) - 一些重要的安全注意事项.\n- [架构](https://github.com/authok/nextjs-authok/blob/main/ARCHITECTURE.md) - SDK 的架构.\n- [测试](https://github.com/authok/nextjs-authok/blob/main/TESTING.md) - 测试 nextjs-authok 应用的一些辅助.\n- [部署](https://github.com/authok/nextjs-authok/blob/main/examples/README.md) - 如何将示例应用程序部署到 Vercel.\n- [文档网站](https://authok.cn/docs) - 浏览我们的文档网站，了解有关Authok的更多信息.\n\n## 快速开始\n\n### 安装\n\n使用 [npm](https://npmjs.org):\n\n```sh\nnpm install @authok/nextjs-authok\n```\n\n本模块支持以下核心库版本:\n\n- Node.js: 12 LTS and newer LTS releases are supported.\n- Next.js: `>=10`\n\n### Authok 配置\n\n在 [Authok 仪表盘](https://mgmt.authok.cn/#/applications) 创建一个 **常规 Web 应用**.\n\n> **If you're using an existing application**, verify that you have configured the following settings in your Regular Web Application:\n>\n> - 在应用页面点击 \"设置\" 标签.\n> - 点击 \"显示高级设置\" 链接.\n> - 在 \"高级设置\" 中, 点击 \"OAuth\" 标签页.\n> - 确保 \"JsonWebToken 签名算法\" 设置为 `RS256` 还有 \"OIDC Conformant\" 被启用.\n\n接下来, 在 \"设置\" 页面 的 \"应用 URIs\" 部分配置如下 URL:\n\n- **Allowed Callback URLs**: `http://localhost:3000/api/auth/callback`\n- **Allowed Logout URLs**: `http://localhost:3000/`\n\n在 \"基本信息\" 部分 获取 **Client ID**, **Client Secret**, 和 **Domain** . 你将会在后续步骤用到这些配置.\n\n### 基本设置\n\n#### 配置应用\n\n你需要允许你的 Next.js 应用 与 Authok 正确通信. 你可以在项目根目录下创建 `.env.local` 文件, 该文件定义了必要的 Authok 配置:\n\n```bash\n# A long, secret value used to encrypt the session cookie\nAUTHOK_SECRET='LONG_RANDOM_VALUE'\n# The base url of your application\nAUTHOK_BASE_URL='http://localhost:3000'\n# The url of your Authok tenant domain\nAUTHOK_ISSUER_BASE_URL='https://YOUR_AUTHOK_DOMAIN.authok.cn'\n# Your Authok application's Client ID\nAUTHOK_CLIENT_ID='YOUR_AUTHOK_CLIENT_ID'\n# Your Authok application's Client Secret\nAUTHOK_CLIENT_SECRET='YOUR_AUTHOK_CLIENT_SECRET'\n```\n\n你可以执行以下命令来为 `AUTHOK_SECRET` 生成合适的值:\n\n```bash\nnode -e \"console.log(crypto.randomBytes(32).toString('hex'))\"\n```\n\n你可以在 \"模块配置\" 文档的 [\"Configuration properties\"](https://authok.github.io/nextjs-authok/modules/config.html#configuration-properties)章节查看 Authok 配置选项的完整列表.\n\n> For more details about loading environment variables in Next.js, visit the [\"Environment Variables\"](https://nextjs.org/docs/basic-features/environment-variables) document.\n\n#### 添加动态 API 路由\n\n在 Next.js 应用的 `/pages/api` 目录中创建一个 [catch-all, dynamic API route handler](https://nextjs.org/docs/api-routes/dynamic-api-routes#optional-catch-all-api-routes):\n\n- 在 `/pages/api/` 下创建一个 `auth` 目录.\n\n- 在 `auth` 目录下 创建一个 `[...authok].js` 文件.\n\n访问 动态 API 路由文件 的路径为 `/pages/api/auth/[...authok].js`. 按如下方式填充该文件:\n\n```js\nimport { handleAuth } from '@authok/nextjs-authok';\n\nexport default handleAuth();\n```\n\n执行 `handleAuth()` 将在后台创建以下路由处理器，以之行身份验证流的不同部分:\n\n- `/api/auth/login`: 你的 Next.js 应用重定向用户到身份提供者进行登录 (你可以传递一个可选参数 `returnTo`，用于在登录后跳转到自定义的相对URL, 例如 `/api/auth/login?returnTo=/profile`).\n- `/api/auth/callback`: 登录成功后，身份提供者重定向用户到此路由.\n- `/api/auth/logout`: Next.js 应用退登用户.\n- `/api/auth/me`: 用于获取JSON格式的用户详情.\n\n#### 把 UserProvider 添加到 自定义 App\n\n用 `UserProvider` 组件来包装 `pages/_app.js` 组件:\n\n```jsx\n// pages/_app.js\nimport React from 'react';\nimport { UserProvider } from '@authok/nextjs-authok/client';\n\nexport default function App({ Component, pageProps }) {\n  return (\n    <UserProvider>\n      <Component {...pageProps} />\n    </UserProvider>\n  );\n}\n```\n\n#### 使用身份认证\n\n现在，你可以通过检查 `useUser()` 返回的 `user` 对象是否已定义来确定用户是否通过身份验证. 你还可以从 Next.js 应用的前端来 登录 或 注销用户(通过重定向到对应的自动生成的路由):\n\n```jsx\n// pages/index.js\nimport { useUser } from '@authok/nextjs-authok/client';\n\nexport default function Index() {\n  const { user, error, isLoading } = useUser();\n\n  if (isLoading) return <div>Loading...</div>;\n  if (error) return <div>{error.message}</div>;\n\n  if (user) {\n    return (\n      <div>\n        Welcome {user.name}! <a href=\"/api/auth/logout\">Logout</a>\n      </div>\n    );\n  }\n\n  return <a href=\"/api/auth/login\">Login</a>;\n}\n```\n\n> Next linting rules 可以能建议使用 `Link` 组件来替代 a 标签. `Link` 组件会执行 [client-side transitions between pages](https://nextjs.org/docs/api-reference/next/link). 而链接指向的是 API 路由而不是页面, 因此你需要使用 a 标签.\n\nThere are two additional ways to check for an authenticated user; one for Next.js pages using [withPageAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_page_auth_required.html#withpageauthrequired) and one for Next.js API routes using [withAPIAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_api_auth_required.html#withapiauthrequired).\n\n其它综合示例, 参考 [EXAMPLES.md](https://github.com/authok/nextjs-authok/blob/main/EXAMPLES.md) 文档.\n\n## API 参考\n\n### Server (for Node.js)\n\n`import * from @authok/nextjs-authok`\n\n- [Configuration Options and Environment variables](https://authok.github.io/nextjs-authok/modules/config.html)\n- [initAuthok](https://authok.github.io/nextjs-authok/modules/index.html#initauthok)\n- [handleAuth](https://authok.github.io/nextjs-authok/modules/handlers_auth.html)\n- [handleLogin](https://authok.github.io/nextjs-authok/modules/handlers_login.html#handlelogin)\n- [handleCallback](https://authok.github.io/nextjs-authok/modules/handlers_callback.html)\n- [handleLogout](https://authok.github.io/nextjs-authok/modules/handlers_logout.html)\n- [handleProfile](https://authok.github.io/nextjs-authok/modules/handlers_profile.html)\n- [withApiAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_api_auth_required.html)\n- [withPageAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_page_auth_required.html#withpageauthrequired)\n- [getSession](https://authok.github.io/nextjs-authok/modules/session_get_session.html)\n- [updateSession](https://authok.github.io/nextjs-authok/modules/session_update_session.html)\n- [getAccessToken](https://authok.github.io/nextjs-authok/modules/session_get_access_token.html)\n\n### Edge (for Middleware and the Edge runtime)\n\n`import * from @authok/nextjs-authok/edge`\n\n- [Configuration Options and Environment variables](https://authok.github.io/nextjs-authok/modules/config.html)\n- [initAuthok](https://authok.github.io/nextjs-authok/modules/edge.html#initauthok-1)\n- [withMiddlewareAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_middleware_auth_required.html)\n- [getSession](https://authok.github.io/nextjs-authok/modules/edge.html#getsession-1)\n\n### Client (for the Browser)\n\n`import * from @authok/nextjs-authok/client`\n\n- [UserProvider](https://authok.github.io/nextjs-authok/modules/client_use_user.html#userprovider)\n- [useUser](https://authok.github.io/nextjs-authok/modules/client_use_user.html)\n- [withPageAuthRequired](https://authok.github.io/nextjs-authok/modules/client_with_page_auth_required.html)\n\n### Testing helpers\n\n`import * from @authok/nextjs-authok/testing`\n\n- [generateSessionCookie](https://authok.github.io/nextjs-authok/modules/helpers_testing.html#generatesessioncookie)\n\n参考自动生成的 [API 文档](https://authok.github.io/nextjs-authok/) 获取更多细节\n\n### Cookies and Security\n\nAll cookies will be set to `HttpOnly, SameSite=Lax` and will be set to `Secure` if the application's `AUTHOK_BASE_URL` is `https`.\n\nThe `HttpOnly` setting will make sure that client-side JavaScript is unable to access the cookie to reduce the attack surface of [XSS attacks](https://authok.com/blog/developers-guide-to-common-vulnerabilities-and-how-to-prevent-them/#Cross-Site-Scripting--XSS-).\n\nThe `SameSite=Lax` setting will help mitigate CSRF attacks. Learn more about SameSite by reading the [\"Upcoming Browser Behavior Changes: What Developers Need to Know\"](https://authok.cn/blog/browser-behavior-changes-what-developers-need-to-know/) blog post.\n\n### Caching and Security\n\nMany hosting providers will offer to cache your content at the edge in order to serve data to your users as fast as possible. For example Vercel will [cache your content on the Vercel Edge Network](https://vercel.com/docs/concepts/edge-network/caching) for all static content and Serverless Functions if you provide the necessary caching headers on your response.\n\nIt's generally a bad idea to cache any response that requires authentication, even if the response's content appears safe to cache there may be other data in the response that isn't.\n\nThis SDK offers a rolling session by default, which means that any response that reads the session will have a `Set-Cookie` header to update the cookie's expiry. Vercel and potentially other hosting providers include the `Set-Cookie` header in the cached response, so even if you think the response's content can be cached publicly, the responses `Set-Cookie` header cannot.\n\nCheck your hosting provider's caching rules, but in general you should **never** cache responses that either require authentication or even touch the session to check authentication (eg when using `withApiAuthRequired`, `withPageAuthRequired` or even just `getSession` or `getAccessToken`).\n\n### Error Handling and Security\n\nErrors that come from Authok in the `redirect_uri` callback may contain reflected user input via the OpenID Connect `error` and `error_description` query parameter. Because of this, we do some [basic escaping](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#rule-1-html-encode-before-inserting-untrusted-data-into-html-element-content) on the `message`, `error` and `error_description` properties of the `IdentityProviderError`.\n\nBut, if you write your own error handler, you should **not** render the error `message`, or `error` and `error_description` properties without using a templating engine that will properly escape them for other HTML contexts first.\n\n### Base Path and Internationalized Routing\n\nWith Next.js you can deploy a Next.js application under a sub-path of a domain using [Base Path](https://nextjs.org/docs/api-reference/next.config.js/basepath) and serve internationalized (i18n) routes using [Internationalized Routing](https://nextjs.org/docs/advanced-features/i18n-routing).\n\nIf you use these features the urls of your application will change and so the urls to the nextjs-authok routes will change. To accommodate this there are various places in the SDK that you can customise the url.\n\nFor example, if `basePath: '/foo'` you should prepend this to the `loginUrl` and `profileUrl` specified in your `AuthokProvider`:\n\n```jsx\n// _app.jsx\nfunction App({ Component, pageProps }) {\n  return (\n    <UserProvider loginUrl=\"/foo/api/auth/login\" profileUrl=\"/foo/api/auth/me\">\n      <Component {...pageProps} />\n    </UserProvider>\n  );\n}\n```\n\nAlso, any links to login or logout should include the `basePath`:\n\n```html\n<a href=\"/foo/api/auth/login\">Login</a><br />\n<a href=\"/foo/api/auth/logout\">Logout</a>\n```\n\nYou should configure the [baseUrl](https://authok.github.io/nextjs-authok/interfaces/config.baseconfig.html#baseurl) (or the `AUTHOK_BASE_URL` environment variable). For example:\n\n```shell\n# .env.local\nAUTHOK_BASE_URL=http://localhost:3000/foo\n```\n\nFor any pages that are protected with the Server Side [withPageAuthRequired](https://authok.github.io/nextjs-authok/modules/helpers_with_page_auth_required.html#withpageauthrequired) you should update the `returnTo` parameter depending on the `basePath` and `locale` if necessary.\n\n```js\n// ./pages/my-ssr-page.jsx\nexport default MySsrPage = () => <></>;\n\nconst getFullReturnTo = (ctx) => {\n  // TODO: implement getFullReturnTo based on the ctx.resolvedUrl, ctx.locale\n  // and your next.config.js's basePath and i18n settings.\n  return '/foo/en-US/my-ssr-page';\n};\n\nexport const getServerSideProps = (ctx) => {\n  const returnTo = getFullReturnTo(ctx.req);\n  return withPageAuthRequired({ returnTo })(ctx);\n};\n```\n\n### 对比 Authok React SDK\n\nWe also provide an Authok React SDK, [authok-react](https://github.com/authok/authok-react), which may be suitable for your Next.js application.\n\nThe SPA security model used by `authok-react` is different from the Web Application security model used by this SDK. In short, this SDK protects pages and API routes with a cookie session (see [\"Cookies and Security\"](#cookies-and-security)). A SPA library like `authok-react` will store the user's ID token and access token directly in the browser and use them to access external APIs directly.\n\nYou should be aware of the security implications of both models. However, [authok-react](https://github.com/authok/authok-react) may be more suitable for your needs if you meet any of the following scenarios:\n\n- You are using [Static HTML Export](https://nextjs.org/docs/advanced-features/static-html-export) with Next.js.\n- You do not need to access user data during server-side rendering.\n- You want to get the access token and call external API's directly from the frontend layer rather than using Next.js API routes as a proxy to call external APIs.\n\n### Testing\n\nBy default, the SDK creates and manages a singleton instance to run for the lifetime of the application. When testing your application, you may need to reset this instance, so its state does not leak between tests.\n\nIf you're using Jest, we recommend using `jest.resetModules()` after each test. Alternatively, you can look at [creating your own instance of the SDK](./EXAMPLES.md#create-your-own-instance-of-the-sdk), so it can be recreated between tests.\n\nFor end to end tests, have a look at how we use a [mock OIDC Provider](./scripts/oidc-provider.js).\n\n# 部署\n参考 [如何将示例应用程序部署到 Vercel](./examples/README.md).\n\n## 捐献\n\nWe appreciate feedback and contribution to this repo! Before you get started, please read the following:\n\n- [Authok's general contribution guidelines](https://github.com/authok/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)\n- [Authok's code of conduct guidelines](https://github.com/authok/express-openid-connect/blob/master/CODE-OF-CONDUCT.md)\n- [This repo's contribution guide](./CONTRIBUTING.md)\n\n## Vulnerability Reporting\n\nPlease do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://authok.cn/responsible-disclosure-policy) details the procedure for disclosing security issues.\n\n## Authok 是什么?\n\n<p align=\"center\">\n  <picture>\n    <source media=\"(prefers-color-scheme: dark)\" srcset=\"https://cdn.authok.cn/website/sdks/logos/authok_dark_mode.png\" width=\"150\">\n    <source media=\"(prefers-color-scheme: light)\" srcset=\"https://cdn.authok.cn/website/sdks/logos/authok_light_mode.png\" width=\"150\">\n    <img alt=\"Authok Logo\" src=\"https://cdn.authok.cn/website/sdks/logos/authok_light_mode.png\" width=\"150\">\n  </picture>\n</p>\n<p align=\"center\">\n  Authok is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href=\"https://authok.cn/why-authok\">Why Authok?</a>\n</p>\n<p align=\"center\">\n  This project is licensed under the MIT license. See the <a href=\"https://github.com/authok/express-openid-connect/blob/master/LICENSE\"> LICENSE</a> file for more info.\n</p>\n","readmeFilename":"README.md"}