From f5ad51a35e5b21122ca40b7dd6723bff33d51edb Mon Sep 17 00:00:00 2001 From: code-october <148516338+code-october@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:29:42 +0000 Subject: [PATCH 1/6] fix quoteEnd extract regex --- app/components/markdown.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 6e340d065..7d27f1d60 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -252,7 +252,7 @@ function tryWrapHtmlCode(text: string) { }, ) .replace( - /(<\/body>)([\r\n\s]*?)(<\/html>)([\n\r]*?)([`]*?)([\n\r]*?)/g, + /(<\/body>)([\r\n\s]*?)(<\/html>)([\n\r]*)([`]*)([\n\r]*?)/g, (match, bodyEnd, space, htmlEnd, newLine, quoteEnd) => { return !quoteEnd ? bodyEnd + space + htmlEnd + "\n```\n" : match; }, From 909834d4b45443e1b818811b1e4ed06272a1f24f Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 30 Sep 2024 11:58:08 +0800 Subject: [PATCH 2/6] change nginx --- chat.conf | 88 ------------------------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 chat.conf diff --git a/chat.conf b/chat.conf deleted file mode 100644 index 1a5c8d26e..000000000 --- a/chat.conf +++ /dev/null @@ -1,88 +0,0 @@ -map $http_upgrade $connection_upgrade { - default upgrade; - '' close; -} - - # 匹配以 www. 开头的请求并重定向到 HTTPS 的不带 www. - server { - listen 80; - listen 443 ssl; - server_name ~^www\.(?.+)$; - - ssl_protocols TLSv1.2 TLSv1.3; - ssl_certificate "/root/cert.pem"; - ssl_certificate_key "/root/cert.key"; - ssl_session_cache shared:SSL:1m; - ssl_session_timeout 10m; - ssl_ciphers PROFILE=SYSTEM; - ssl_prefer_server_ciphers on; - add_header Strict-Transport-Security "max-age=31536000"; - - return 301 https://$domain$request_uri; - } - - # 匹配任意 HTTP 重定向到 HTTPS - server { - listen 80 default_server; - server_name _; - - # 将 HTTP 请求重定向到 HTTPS - return 301 https://$host$request_uri; - } - - - -server { - listen 443 ssl http2 default_server; - server_name _; - - ssl_protocols TLSv1.2 TLSv1.3; - ssl_certificate "/root/cert.pem"; - ssl_certificate_key "/root/cert.key"; - ssl_session_cache shared:SSL:1m; - ssl_session_timeout 10m; - ssl_ciphers PROFILE=SYSTEM; - ssl_prefer_server_ciphers on; - add_header Strict-Transport-Security "max-age=31536000"; - - access_log /var/log/nginx/access.log; - error_log /var/log/nginx/error.log; - - -#REWRITE-START -if ($host ~* ^www\.(.+)$) { - set $new_host \$1; - return 301 https://$new_host$request_uri; -} -#REWRITE-END - -#PROXY-START/ - -location ^~ / -{ - proxy_pass http://127.0.0.1:23000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header REMOTE-HOST $remote_addr; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade; - proxy_http_version 1.1; - - # 禁止缓存的相关设置 - proxy_buffering off; - proxy_no_cache 1; - proxy_cache_bypass 1; - add_header X-Accel-Buffering "no"; - # 设置代理超时 - proxy_read_timeout 24h; - proxy_connect_timeout 3m; - proxy_send_timeout 24h; - proxy_max_temp_file_size 0; -} - -#PROXY-END/ - - -} - From deb215ccd1adaf14bb55d1f26de4ef4713fd4780 Mon Sep 17 00:00:00 2001 From: lyf <1910527151@qq.com> Date: Mon, 30 Sep 2024 13:23:24 +0800 Subject: [PATCH 3/6] fix readme --- README.md | 21 +++++++++++++-------- README_CN.md | 2 +- README_JA.md | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index be5e91d65..e10d3bce8 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 [![MacOS][MacOS-image]][download-url] [![Linux][Linux-image]][download-url] -[NextChatAI](https://nextchat.dev/chat) / [Web App](https://app.nextchat.dev) / [Desktop App](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [Discord](https://discord.gg/YCkeafCafC) / [Enterprise Edition](#enterprise-edition) / [Twitter](https://twitter.com/NextChatDev) +[NextChatAI](https://nextchat.dev/chat?utm_source=readme) / [Web App](https://app.nextchat.dev) / [Desktop App](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [Discord](https://discord.gg/YCkeafCafC) / [Enterprise Edition](#enterprise-edition) / [Twitter](https://twitter.com/NextChatDev) [NextChatAI](https://nextchat.dev/chat) / [网页版](https://app.nextchat.dev) / [客户端](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) / [反馈](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) -[saas-url]: https://nextchat.dev/chat +[saas-url]: https://nextchat.dev/chat?utm_source=readme [saas-image]: https://img.shields.io/badge/NextChat-Saas-green?logo=microsoftedge [web-url]: https://app.nextchat.dev/ [download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases @@ -31,7 +31,7 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 [MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple [Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu -[Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) +[Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) [](https://monica.im/?utm=nxcrp) @@ -40,6 +40,7 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 ## Enterprise Edition Meeting Your Company's Privatization and Customization Deployment Requirements: + - **Brand Customization**: Tailored VI/UI to seamlessly align with your corporate brand image. - **Resource Integration**: Unified configuration and management of dozens of AI resources by company administrators, ready for use by team members. - **Permission Control**: Clearly defined member permissions, resource permissions, and knowledge base permissions, all controlled via a corporate-grade Admin Panel. @@ -53,6 +54,7 @@ For enterprise inquiries, please contact: **business@nextchat.dev** ## 企业版 满足企业用户私有化部署和个性化定制需求: + - **品牌定制**:企业量身定制 VI/UI,与企业品牌形象无缝契合 - **资源集成**:由企业管理人员统一配置和管理数十种 AI 资源,团队成员开箱即用 - **权限管理**:成员权限、资源权限、知识库权限层级分明,企业级 Admin Panel 统一控制 @@ -101,7 +103,7 @@ For enterprise inquiries, please contact: **business@nextchat.dev** ## What's New - 🚀 v2.15.0 Now supports Plugins! Read this: [NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins) -- 🚀 v2.14.0 Now supports Artifacts & SD +- 🚀 v2.14.0 Now supports Artifacts & SD - 🚀 v2.10.1 support Google Gemini Pro model. - 🚀 v2.9.11 you can use azure endpoint now. - 🚀 v2.8 now we have a client that runs across all platforms! @@ -132,8 +134,8 @@ For enterprise inquiries, please contact: **business@nextchat.dev** - [x] 支持自部署的大语言模型:开箱即用 [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) ,服务端部署 [LocalAI 项目](https://github.com/go-skynet/LocalAI) llama / gpt4all / rwkv / vicuna / koala / gpt4all-j / cerebras / falcon / dolly 等等,或者使用 [api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm) - [x] Artifacts: 通过独立窗口,轻松预览、复制和分享生成的内容/可交互网页 [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092) - [x] 插件机制,支持`联网搜索`、`计算器`、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) - - [x] 支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) - - [ ] 本地知识库 + - [x] 支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) +- [ ] 本地知识库 ## 最新动态 @@ -333,10 +335,12 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model User `-all` to disable all default models, `+all` to enable all default models. For Azure: use `modelName@azure=deploymentName` to customize model name and deployment name. + > Example: `+gpt-3.5-turbo@azure=gpt35` will show option `gpt35(Azure)` in model list. > If you only can use Azure model, `-all,+gpt-3.5-turbo@azure=gpt35` will `gpt35(Azure)` the only option in model list. For ByteDance: use `modelName@bytedance=deploymentName` to customize model name and deployment name. + > Example: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` will show option `Doubao-lite-4k(ByteDance)` in model list. ### `DEFAULT_MODEL` (optional) @@ -346,8 +350,9 @@ Change default model ### `WHITE_WEBDAV_ENDPOINTS` (optional) You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format: -- Each address must be a complete endpoint -> `https://xxxx/yyy` + +- Each address must be a complete endpoint + > `https://xxxx/yyy` - Multiple addresses are connected by ', ' ### `DEFAULT_INPUT_TEMPLATE` (optional) diff --git a/README_CN.md b/README_CN.md index 640fe3933..73fbc3f51 100644 --- a/README_CN.md +++ b/README_CN.md @@ -8,7 +8,7 @@ 一键免费部署你的私人 ChatGPT 网页应用,支持 GPT3, GPT4 & Gemini Pro 模型。 -[NextChatAI](https://nextchat.dev/chat) / [企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) / [演示 Demo](https://chat-gpt-next-web.vercel.app/) / [反馈 Issues](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [加入 Discord](https://discord.gg/zrhvHCr79N) +[NextChatAI](https://nextchat.dev/chat?utm_source=readme) / [企业版](#%E4%BC%81%E4%B8%9A%E7%89%88) / [演示 Demo](https://chat-gpt-next-web.vercel.app/) / [反馈 Issues](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [加入 Discord](https://discord.gg/zrhvHCr79N) [Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) diff --git a/README_JA.md b/README_JA.md index ba3c514dc..416928c26 100644 --- a/README_JA.md +++ b/README_JA.md @@ -5,7 +5,7 @@ ワンクリックで無料であなた専用の ChatGPT ウェブアプリをデプロイ。GPT3、GPT4 & Gemini Pro モデルをサポート。 -[NextChatAI](https://nextchat.dev/chat) / [企業版](#企業版) / [デモ](https://chat-gpt-next-web.vercel.app/) / [フィードバック](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [Discordに参加](https://discord.gg/zrhvHCr79N) +[NextChatAI](https://nextchat.dev/chat?utm_source=readme) / [企業版](#企業版) / [デモ](https://chat-gpt-next-web.vercel.app/) / [フィードバック](https://github.com/Yidadaa/ChatGPT-Next-Web/issues) / [Discordに参加](https://discord.gg/zrhvHCr79N) [Zeaburでデプロイ](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Zeaburでデプロイ](https://zeabur.com/templates/ZBUEFA) [Gitpodで開く](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) From d2984db6e7deb0fc51ea76abde2b6748067baa28 Mon Sep 17 00:00:00 2001 From: lyf <1910527151@qq.com> Date: Mon, 30 Sep 2024 13:28:14 +0800 Subject: [PATCH 4/6] fix readme --- README.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e10d3bce8..bf7c30594 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 [MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple [Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu -[Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) +[Deploy on Zeabur](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [Deploy on Zeabur](https://zeabur.com/templates/ZBUEFA) [Open in Gitpod](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) [](https://monica.im/?utm=nxcrp) @@ -40,7 +40,6 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 ## Enterprise Edition Meeting Your Company's Privatization and Customization Deployment Requirements: - - **Brand Customization**: Tailored VI/UI to seamlessly align with your corporate brand image. - **Resource Integration**: Unified configuration and management of dozens of AI resources by company administrators, ready for use by team members. - **Permission Control**: Clearly defined member permissions, resource permissions, and knowledge base permissions, all controlled via a corporate-grade Admin Panel. @@ -54,7 +53,6 @@ For enterprise inquiries, please contact: **business@nextchat.dev** ## 企业版 满足企业用户私有化部署和个性化定制需求: - - **品牌定制**:企业量身定制 VI/UI,与企业品牌形象无缝契合 - **资源集成**:由企业管理人员统一配置和管理数十种 AI 资源,团队成员开箱即用 - **权限管理**:成员权限、资源权限、知识库权限层级分明,企业级 Admin Panel 统一控制 @@ -103,7 +101,7 @@ For enterprise inquiries, please contact: **business@nextchat.dev** ## What's New - 🚀 v2.15.0 Now supports Plugins! Read this: [NextChat-Awesome-Plugins](https://github.com/ChatGPTNextWeb/NextChat-Awesome-Plugins) -- 🚀 v2.14.0 Now supports Artifacts & SD +- 🚀 v2.14.0 Now supports Artifacts & SD - 🚀 v2.10.1 support Google Gemini Pro model. - 🚀 v2.9.11 you can use azure endpoint now. - 🚀 v2.8 now we have a client that runs across all platforms! @@ -134,8 +132,8 @@ For enterprise inquiries, please contact: **business@nextchat.dev** - [x] 支持自部署的大语言模型:开箱即用 [RWKV-Runner](https://github.com/josStorer/RWKV-Runner) ,服务端部署 [LocalAI 项目](https://github.com/go-skynet/LocalAI) llama / gpt4all / rwkv / vicuna / koala / gpt4all-j / cerebras / falcon / dolly 等等,或者使用 [api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm) - [x] Artifacts: 通过独立窗口,轻松预览、复制和分享生成的内容/可交互网页 [#5092](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/pull/5092) - [x] 插件机制,支持`联网搜索`、`计算器`、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) - - [x] 支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) -- [ ] 本地知识库 + - [x] 支持联网搜索、计算器、调用其他平台 api [#165](https://github.com/Yidadaa/ChatGPT-Next-Web/issues/165) [#5353](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/issues/5353) + - [ ] 本地知识库 ## 最新动态 @@ -335,12 +333,10 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model User `-all` to disable all default models, `+all` to enable all default models. For Azure: use `modelName@azure=deploymentName` to customize model name and deployment name. - > Example: `+gpt-3.5-turbo@azure=gpt35` will show option `gpt35(Azure)` in model list. > If you only can use Azure model, `-all,+gpt-3.5-turbo@azure=gpt35` will `gpt35(Azure)` the only option in model list. For ByteDance: use `modelName@bytedance=deploymentName` to customize model name and deployment name. - > Example: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` will show option `Doubao-lite-4k(ByteDance)` in model list. ### `DEFAULT_MODEL` (optional) @@ -350,9 +346,8 @@ Change default model ### `WHITE_WEBDAV_ENDPOINTS` (optional) You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format: - -- Each address must be a complete endpoint - > `https://xxxx/yyy` +- Each address must be a complete endpoint +> `https://xxxx/yyy` - Multiple addresses are connected by ', ' ### `DEFAULT_INPUT_TEMPLATE` (optional) From bf457c6853760cafe22c730b50049de0d537ade4 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 30 Sep 2024 14:31:15 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E5=8A=A8=E7=94=BBbug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=8C=E5=8F=8A=E4=B8=9D=E6=BB=91=E4=B8=80?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/chat.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 29091aced..c50905d2e 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -438,14 +438,23 @@ function useScrollToBottom( if (dom) { requestAnimationFrame(() => { setAutoScroll(true); - dom.scrollTo(0, dom.scrollHeight); + // dom.scrollTo(0, dom.scrollHeight); + // 丝滑一点 + dom.scrollTo({ + top: dom.scrollHeight, + behavior: "smooth", + }); }); } } // auto scroll useEffect(() => { - if (autoScroll && !detach) { + // if (autoScroll && !detach) { + // scrollDomToBottom(); + // } + // 自动滚动一直有bug,直接强制修改了 + if (autoScroll) { scrollDomToBottom(); } }); From 7c7cc91c05a99201f58c3c761bbaf00ad8e06b5e Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 30 Sep 2024 17:48:14 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF=EF=BC=8C=E6=97=A0?= =?UTF-8?q?=E5=BD=B1=E5=93=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app/(auth)/login/set-password/page.tsx | 46 ++++++++++------------ app/app/(auth)/login/user-login-core.tsx | 1 - lib/auth.ts | 19 ++++----- lib/auth_client.ts | 8 ++-- lib/auth_type.ts | 6 --- lib/types/next-auth.d.ts | 45 +++++++++++++++++++++ middleware.ts | 10 ++--- tsconfig.json | 3 +- 8 files changed, 86 insertions(+), 52 deletions(-) delete mode 100644 lib/auth_type.ts create mode 100644 lib/types/next-auth.d.ts diff --git a/app/app/(auth)/login/set-password/page.tsx b/app/app/(auth)/login/set-password/page.tsx index 2ee04158c..1e0f60ff9 100644 --- a/app/app/(auth)/login/set-password/page.tsx +++ b/app/app/(auth)/login/set-password/page.tsx @@ -35,7 +35,6 @@ export default function SetPasswordPage() { const onFinish: FormProps["onFinish"] = (values) => { // setLoading(true); // console.log('-------------', values) - // @ts-expect-error fetch(`/api/user/${session?.user?.id}`, { method: "PUT", credentials: "include", @@ -61,31 +60,28 @@ export default function SetPasswordPage() { layout="vertical" onFinish={onFinish} > - { - // @ts-expect-error - status === "authenticated" && session?.user?.hasPassword && ( - { - if (!value) { - return Promise.reject(new Error("请填写该字段")); - } - }, + {status === "authenticated" && session?.user?.hasPassword && ( + { + if (!value) { + return Promise.reject(new Error("请填写该字段")); + } }, - ]} - > - } - type="password" - autoComplete="current-password" - id="user_old_password" - /> - - ) - } + }, + ]} + > + } + type="password" + autoComplete="current-password" + id="user_old_password" + /> + + )} { - // @ts-expect-error if (!value?.user?.hasPassword) { if (result_url === "/") { result_url = "/login/set-password"; diff --git a/lib/auth.ts b/lib/auth.ts index b698f82f4..7562a60a8 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -9,6 +9,9 @@ import { isEmail, isName } from "@/lib/auth_list"; import {createTransport} from "nodemailer"; import { comparePassword } from "@/lib/utils"; import { randomBytes } from "crypto"; +import { type Session } from "next-auth"; +import { type JWT } from "next-auth/jwt"; + const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES; let verificationTokens = new Map(); @@ -153,25 +156,23 @@ export const authOptions: NextAuthOptions = { if (user) { token.user = user; } else { - const updateUser = await prisma.user.findUnique({ where: { id: token.sub }}); - // console.log('========', updateUser) + const updateUser: User | null = await prisma.user.findUnique({ where: { id: token.sub }}); if (!updateUser || !updateUser.allowToLogin) { throw new Error('无法刷新令牌,用户状态不正确'); } - token.user = updateUser; + token.user = updateUser as User; } return token; }, - session: async ({ session, token }) => { + session: async ({ session, token }: { + session: Session, + token: JWT + }) => { session.user = { ...session.user, - // @ts-expect-error - id: token?.sub, - // @ts-expect-error + id: token?.sub ?? "", username: token?.user?.username || token?.user?.gh_username, - // @ts-expect-error hasPassword: !!token?.user?.password, - // @ts-expect-error isAdmin: token?.user?.isAdmin, }; // console.log('555555555,', session, token) diff --git a/lib/auth_client.ts b/lib/auth_client.ts index 143480963..5b65b200f 100644 --- a/lib/auth_client.ts +++ b/lib/auth_client.ts @@ -1,21 +1,21 @@ import { isName } from "@/lib/auth_list"; -import { CUS_JWT } from "@/lib/auth_type"; +import { type JWT } from "next-auth/jwt"; -export async function VerifiedUser(session: CUS_JWT | null) { +export async function VerifiedUser(session: JWT | null) { const userId = session?.sub const name = session?.email || session?.name return !!(name && isName(name) && userId); } -export async function VerifiedAdminUser(session: CUS_JWT | null) { +export async function VerifiedAdminUser(session: JWT | null) { // console.log('-------', session, session?.user?.isAdmin) return !!session?.user?.isAdmin; // const name = session?.email || session?.name // return !!(name && ADMIN_LIST.includes(name)); } -export function VerifiedNeedSetPassword(path: string, session: CUS_JWT | null,) { +export function VerifiedNeedSetPassword(path: string, session: JWT | null,) { const need_set_pwd = !session?.user?.password return path === "/login/set-password" && need_set_pwd; } diff --git a/lib/auth_type.ts b/lib/auth_type.ts deleted file mode 100644 index 28623eb73..000000000 --- a/lib/auth_type.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { JWT } from "next-auth/jwt"; -import { User } from "@prisma/client"; - -export type CUS_JWT = JWT & { - user: User, -} diff --git a/lib/types/next-auth.d.ts b/lib/types/next-auth.d.ts new file mode 100644 index 000000000..f062194b2 --- /dev/null +++ b/lib/types/next-auth.d.ts @@ -0,0 +1,45 @@ +// types/next-auth.d.ts +import { DefaultSession, DefaultUser } from "next-auth"; + + +declare module "next-auth" { + /** + * 扩展 Session 接口,添加自定义的用户属性 + */ + interface Session { + user: { + id: string; + username?: string | null; + hasPassword?: boolean | null; + isAdmin?: boolean | null; + } & DefaultSession["user"]; + } + + /** + * 扩展 User 接口,添加自定义属性 + * 注意:保持属性可选,以与 AdapterUser 兼容 + */ + interface User extends DefaultUser { + id: string; + username?: string; + gh_username?: string; + password?: string; + isAdmin?: boolean; + } + +} + +declare module "next-auth/jwt" { + /** + * 扩展 JWT 接口,添加自定义的用户属性 + */ + interface JWT { + user?: { + id: string; + username?: string | null; + gh_username?: string | null; + password?: string | null; + isAdmin?: boolean | null; + }; + } +} diff --git a/middleware.ts b/middleware.ts index 1a8c7ff79..745b4314f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,9 +1,7 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { getToken } from "next-auth/jwt"; -import { VerifiedUser, VerifiedAdminUser, VerifiedNeedSetPassword } from "@/lib/auth_client"; -import { CUS_JWT } from "@/lib/auth_type"; - +import { VerifiedUser, VerifiedAdminUser } from "@/lib/auth_client"; export default async function middleware(req: NextRequest) { const url = req.nextUrl; @@ -17,8 +15,8 @@ export default async function middleware(req: NextRequest) { } const session = await getToken({ req }); - const isUser = await VerifiedUser(session as CUS_JWT); - const isAdminUser = await VerifiedAdminUser(session as CUS_JWT); + const isUser = await VerifiedUser(session); + const isAdminUser = await VerifiedAdminUser(session); // console.log('----session', session, '---isUser', isUser, '---isAdmin', isAdminUser) // 管理员页面的api接口还是要认证的 if (path.startsWith('/api/admin/')) { @@ -45,7 +43,7 @@ export default async function middleware(req: NextRequest) { ); } - // if (VerifiedNeedSetPassword(path, session as CUS_JWT)) { + // if (VerifiedNeedSetPassword(path, session)) { // console.log('-0-0-- 需要修改密码', ) // // return NextResponse.redirect(new URL("/login/set-password", req.url)) // } diff --git a/tsconfig.json b/tsconfig.json index c73eef3e8..ce1a04b38 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,8 @@ ], "paths": { "@/*": ["./*"] - } + }, + "typeRoots": ["lib/types"] }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"], "exclude": ["node_modules"]