@linyjs/plugin-docs 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -0
- package/docs/api-reference/index.md +361 -0
- package/docs/best-practices/error-handling.md +462 -0
- package/docs/best-practices/naming-conventions.md +496 -0
- package/docs/best-practices/performance.md +532 -0
- package/docs/best-practices/security.md +620 -0
- package/docs/best-practices/service-definition.md +332 -0
- package/docs/faq.md +322 -0
- package/docs/getting-started/first-plugin.md +358 -0
- package/docs/getting-started/installation.md +244 -0
- package/docs/getting-started/quick-start.md +235 -0
- package/docs/guides/authentication.md +428 -0
- package/docs/guides/client-module.md +604 -0
- package/docs/guides/data-persistence.md +640 -0
- package/docs/guides/permissions.md +440 -0
- package/docs/guides/server-module.md +377 -0
- package/docs/guides/ui-slots.md +475 -0
- package/docs/migration/v0-to-v1.md +354 -0
- package/examples/analytics-plugin/README.md +416 -0
- package/examples/analytics-plugin/dist/client/index.d.ts +36 -0
- package/examples/analytics-plugin/dist/client/index.js +173 -0
- package/examples/analytics-plugin/dist/index.d.ts +64 -0
- package/examples/analytics-plugin/dist/index.js +72 -0
- package/examples/analytics-plugin/dist/server/index.d.ts +119 -0
- package/examples/analytics-plugin/dist/server/index.js +313 -0
- package/examples/analytics-plugin/package.json +28 -0
- package/examples/analytics-plugin/src/client/index.tsx +414 -0
- package/examples/analytics-plugin/src/index.ts +71 -0
- package/examples/analytics-plugin/src/server/index.ts +349 -0
- package/examples/analytics-plugin/tsconfig.json +21 -0
- package/examples/blog-plugin/README.md +319 -0
- package/examples/blog-plugin/dist/client/index.d.ts +22 -0
- package/examples/blog-plugin/dist/client/index.js +197 -0
- package/examples/blog-plugin/dist/server/index.d.ts +5 -0
- package/examples/blog-plugin/dist/server/index.js +220 -0
- package/examples/blog-plugin/package.json +24 -0
- package/examples/blog-plugin/src/client/index.tsx +409 -0
- package/examples/blog-plugin/src/server/index.ts +240 -0
- package/examples/blog-plugin/tsconfig.json +16 -0
- package/examples/hello-world/README.md +101 -0
- package/examples/hello-world/dist/client/index.d.ts +5 -0
- package/examples/hello-world/dist/client/index.js +80 -0
- package/examples/hello-world/dist/server/index.d.ts +5 -0
- package/examples/hello-world/dist/server/index.js +38 -0
- package/examples/hello-world/node_modules/.package-lock.json +61 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/HOOKS_USAGE.md +281 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/IMPLEMENTATION_SUMMARY.md +166 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/QUICK_REFERENCE.md +130 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/README.md +214 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/package.json +26 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/application.ts +50 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/comparison-example.tsx +229 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/di.ts +127 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/domain.ts +52 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks-example.tsx +275 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks.ts +175 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/index.ts +87 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/interfaces.ts +117 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/router.ts +49 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/tsconfig.json +10 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/package.json +21 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/application.ts +21 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/di.ts +127 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/domain.ts +75 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/index.ts +55 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/interfaces.ts +58 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/tsconfig.json +8 -0
- package/examples/hello-world/node_modules/@types/react/LICENSE +21 -0
- package/examples/hello-world/node_modules/@types/react/README.md +15 -0
- package/examples/hello-world/node_modules/@types/react/canary.d.ts +129 -0
- package/examples/hello-world/node_modules/@types/react/compiler-runtime.d.ts +4 -0
- package/examples/hello-world/node_modules/@types/react/experimental.d.ts +184 -0
- package/examples/hello-world/node_modules/@types/react/global.d.ts +166 -0
- package/examples/hello-world/node_modules/@types/react/index.d.ts +4369 -0
- package/examples/hello-world/node_modules/@types/react/jsx-dev-runtime.d.ts +33 -0
- package/examples/hello-world/node_modules/@types/react/jsx-runtime.d.ts +24 -0
- package/examples/hello-world/node_modules/@types/react/package.json +210 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/canary.d.ts +129 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/experimental.d.ts +184 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/global.d.ts +166 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/index.d.ts +4356 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-dev-runtime.d.ts +33 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-runtime.d.ts +24 -0
- package/examples/hello-world/node_modules/csstype/LICENSE +19 -0
- package/examples/hello-world/node_modules/csstype/README.md +291 -0
- package/examples/hello-world/node_modules/csstype/index.d.ts +22569 -0
- package/examples/hello-world/node_modules/csstype/index.js.flow +6863 -0
- package/examples/hello-world/node_modules/csstype/package.json +70 -0
- package/examples/hello-world/node_modules/react/LICENSE +21 -0
- package/examples/hello-world/node_modules/react/README.md +37 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.development.js +24 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.production.js +16 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.profiling.js +16 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.development.js +338 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.production.js +14 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.profiling.js +14 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.development.js +370 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.production.js +40 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.development.js +352 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.production.js +34 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.profiling.js +34 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.development.js +370 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.production.js +40 -0
- package/examples/hello-world/node_modules/react/cjs/react.development.js +1284 -0
- package/examples/hello-world/node_modules/react/cjs/react.production.js +542 -0
- package/examples/hello-world/node_modules/react/cjs/react.react-server.development.js +848 -0
- package/examples/hello-world/node_modules/react/cjs/react.react-server.production.js +423 -0
- package/examples/hello-world/node_modules/react/compiler-runtime.js +14 -0
- package/examples/hello-world/node_modules/react/index.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-dev-runtime.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-dev-runtime.react-server.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-runtime.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-runtime.react-server.js +7 -0
- package/examples/hello-world/node_modules/react/package.json +51 -0
- package/examples/hello-world/node_modules/react/react.react-server.js +7 -0
- package/examples/hello-world/node_modules/typescript/LICENSE.txt +55 -0
- package/examples/hello-world/node_modules/typescript/README.md +50 -0
- package/examples/hello-world/node_modules/typescript/SECURITY.md +41 -0
- package/examples/hello-world/node_modules/typescript/ThirdPartyNoticeText.txt +193 -0
- package/examples/hello-world/node_modules/typescript/bin/tsc +2 -0
- package/examples/hello-world/node_modules/typescript/bin/tsserver +2 -0
- package/examples/hello-world/node_modules/typescript/lib/_tsc.js +133818 -0
- package/examples/hello-world/node_modules/typescript/lib/_tsserver.js +659 -0
- package/examples/hello-world/node_modules/typescript/lib/_typingsInstaller.js +222 -0
- package/examples/hello-world/node_modules/typescript/lib/cs/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/de/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/es/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/fr/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/it/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ja/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ko/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.decorators.d.ts +384 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.decorators.legacy.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.asynciterable.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.d.ts +39429 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.iterable.d.ts +571 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.collection.d.ts +147 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.core.d.ts +597 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.generator.d.ts +77 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.iterable.d.ts +605 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.promise.d.ts +81 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.proxy.d.ts +128 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.reflect.d.ts +144 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.d.ts +46 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts +326 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.array.include.d.ts +116 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.full.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.intl.d.ts +31 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.date.d.ts +31 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.full.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.intl.d.ts +44 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.object.d.ts +49 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts +135 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.string.d.ts +45 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts +53 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts +77 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts +53 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.intl.d.ts +83 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.promise.d.ts +30 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.regexp.d.ts +37 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.array.d.ts +79 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.intl.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.object.d.ts +33 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.string.d.ts +37 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.symbol.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.bigint.d.ts +765 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.d.ts +27 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.date.d.ts +42 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.intl.d.ts +474 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.number.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.promise.d.ts +47 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts +99 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.string.d.ts +44 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.intl.d.ts +166 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.promise.d.ts +48 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.string.d.ts +33 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.weakref.d.ts +78 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.array.d.ts +121 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.error.d.ts +75 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.intl.d.ts +145 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.object.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.regexp.d.ts +39 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.string.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.array.d.ts +924 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.collection.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.intl.d.ts +56 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts +65 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.collection.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.object.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.promise.d.ts +35 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.regexp.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts +68 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.string.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es5.d.ts +4601 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es6.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.array.d.ts +35 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.collection.d.ts +96 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.decorators.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.disposable.d.ts +193 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.error.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.float16.d.ts +445 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.intl.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.iterator.d.ts +148 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.promise.d.ts +34 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.sharedmemory.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.scripthost.d.ts +322 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.asynciterable.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.d.ts +13150 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.importscripts.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.iterable.d.ts +340 -0
- package/examples/hello-world/node_modules/typescript/lib/pl/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/pt-br/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ru/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/tr/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/tsc.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserver.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.d.ts +17 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.js +21 -0
- package/examples/hello-world/node_modules/typescript/lib/typesMap.json +497 -0
- package/examples/hello-world/node_modules/typescript/lib/typescript.d.ts +11437 -0
- package/examples/hello-world/node_modules/typescript/lib/typescript.js +200276 -0
- package/examples/hello-world/node_modules/typescript/lib/typingsInstaller.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/watchGuard.js +53 -0
- package/examples/hello-world/node_modules/typescript/lib/zh-cn/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/zh-tw/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/package.json +120 -0
- package/examples/hello-world/package-lock.json +74 -0
- package/examples/hello-world/package.json +23 -0
- package/examples/hello-world/src/client/index.tsx +110 -0
- package/examples/hello-world/src/server/index.ts +42 -0
- package/examples/hello-world/tsconfig.json +16 -0
- package/package.json +46 -0
- package/src/cli.ts +321 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# 快速开始
|
|
2
|
+
|
|
3
|
+
## 📖 导航目录
|
|
4
|
+
|
|
5
|
+
- [快速开始](./quick-start.md) - 本文档
|
|
6
|
+
- [安装指南](./installation.md) - 详细安装说明
|
|
7
|
+
- [第一个插件教程](./first-plugin.md) - 深入学习插件开发
|
|
8
|
+
|
|
9
|
+
## 相关文档
|
|
10
|
+
|
|
11
|
+
- [服务端模块开发](../guides/server-module.md)
|
|
12
|
+
- [客户端模块开发](../guides/client-module.md)
|
|
13
|
+
- [创建示例项目](../../examples/hello-world/)
|
|
14
|
+
|
|
15
|
+
## 5分钟创建你的第一个 linyjs 插件
|
|
16
|
+
|
|
17
|
+
本教程将带你从零开始,在5分钟内创建一个简单的 "Hello World" 插件。
|
|
18
|
+
|
|
19
|
+
### 前置要求
|
|
20
|
+
|
|
21
|
+
- Node.js >= 18.0
|
|
22
|
+
- npm 或 yarn
|
|
23
|
+
- 基本的 TypeScript 和 React 知识
|
|
24
|
+
|
|
25
|
+
### 步骤 1: 初始化项目
|
|
26
|
+
|
|
27
|
+
**推荐方式:使用 CLI 工具自动创建项目**
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# 使用 @linyjs/cli 创建项目目录和基本文件
|
|
31
|
+
npx @linyjs/cli init hello-plugin
|
|
32
|
+
cd hello-plugin
|
|
33
|
+
|
|
34
|
+
# 安装依赖
|
|
35
|
+
npm install
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
CLI 工具会自动创建:
|
|
39
|
+
- package.json (已配置依赖和脚本)
|
|
40
|
+
- tsconfig.json (TypeScript 配置)
|
|
41
|
+
- src/server/index.ts (服务端代码模板)
|
|
42
|
+
- src/client/index.tsx (客户端代码模板)
|
|
43
|
+
|
|
44
|
+
**传统方式:手动创建项目**
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 创建项目目录
|
|
48
|
+
mkdir hello-plugin && cd hello-plugin
|
|
49
|
+
|
|
50
|
+
# 初始化 npm 项目
|
|
51
|
+
npm init -y
|
|
52
|
+
|
|
53
|
+
# 安装依赖
|
|
54
|
+
npm install @linyjs/server-module-interface @linyjs/client-module-interface react
|
|
55
|
+
npm install -D typescript @types/react
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 步骤 2: 项目结构
|
|
59
|
+
|
|
60
|
+
使用 CLI 工具创建的项目已经包含了完整的文件结构:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
hello-plugin/
|
|
64
|
+
├── package.json # 已预配置的依赖和脚本
|
|
65
|
+
├── tsconfig.json # 已配置 TypeScript
|
|
66
|
+
└── src/
|
|
67
|
+
├── server/
|
|
68
|
+
│ └── index.ts # 服务端入口 (已包含示例代码)
|
|
69
|
+
└── client/
|
|
70
|
+
└── index.tsx # 客户端入口 (已包含示例代码)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**注意**:使用 CLI 创建的项目已经包含了预配置的 package.json 和 tsconfig.json,无需手动创建。
|
|
74
|
+
|
|
75
|
+
如果你使用了传统方式,需要**手动创建 tsconfig.json:**
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"compilerOptions": {
|
|
80
|
+
"target": "ES2020",
|
|
81
|
+
"module": "ESNext",
|
|
82
|
+
"lib": ["ES2020", "DOM"],
|
|
83
|
+
"jsx": "react-jsx",
|
|
84
|
+
"declaration": true,
|
|
85
|
+
"outDir": "./dist",
|
|
86
|
+
"rootDir": "./src",
|
|
87
|
+
"strict": true,
|
|
88
|
+
"esModuleInterop": true,
|
|
89
|
+
"skipLibCheck": true,
|
|
90
|
+
"moduleResolution": "node"
|
|
91
|
+
},
|
|
92
|
+
"include": ["src/**/*"]
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 步骤 3: 编写服务端代码
|
|
97
|
+
|
|
98
|
+
**使用 CLI 创建的项目已经包含了示例代码**,你已经可以开始开发或修改现有代码。
|
|
99
|
+
|
|
100
|
+
如果你需要自定义或创建新的服务端代码,编辑 `src/server/index.ts`:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import type { IModule, IServiceDef } from '@linyjs/server-module-interface';
|
|
104
|
+
|
|
105
|
+
class HelloController {
|
|
106
|
+
getRoutes() {
|
|
107
|
+
return [{
|
|
108
|
+
method: 'GET' as const,
|
|
109
|
+
path: '/api/hello',
|
|
110
|
+
middlewares: [],
|
|
111
|
+
responseType: 'json' as const,
|
|
112
|
+
handler: async () => {
|
|
113
|
+
return { message: 'Hello from plugin!' };
|
|
114
|
+
}
|
|
115
|
+
}];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const helloControllerDef: IServiceDef = {
|
|
120
|
+
serviceTag: 'helloController',
|
|
121
|
+
serviceImpl: HelloController,
|
|
122
|
+
meta: { usageType: 'controller' }
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const helloServerModule: IModule = {
|
|
126
|
+
name: 'helloServer',
|
|
127
|
+
serviceDefs: [helloControllerDef]
|
|
128
|
+
};
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 步骤 4: 编写客户端代码
|
|
132
|
+
|
|
133
|
+
**使用 CLI 创建的项目已经包含了示例代码**,你已经可以开始开发或修改现有代码。
|
|
134
|
+
|
|
135
|
+
如果你需要自定义或创建新的客户端代码,编辑 `src/client/index.tsx`:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import type { IModule, IServiceDef } from '@linyjs/client-module-interface';
|
|
139
|
+
import React from 'react';
|
|
140
|
+
|
|
141
|
+
function HelloPage() {
|
|
142
|
+
return (
|
|
143
|
+
<div style={{ padding: '20px' }}>
|
|
144
|
+
<h1>Hello from Plugin!</h1>
|
|
145
|
+
<p>This is your first linyjs plugin.</p>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
class HelloRouteService {
|
|
151
|
+
basePath = '/hello';
|
|
152
|
+
getRoutes() {
|
|
153
|
+
return [{
|
|
154
|
+
path: '/',
|
|
155
|
+
component: HelloPage,
|
|
156
|
+
key: 'hello-page',
|
|
157
|
+
meta: { title: 'Hello' }
|
|
158
|
+
}];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const helloRouteDef: IServiceDef = {
|
|
163
|
+
serviceTag: 'helloRoute',
|
|
164
|
+
serviceImpl: HelloRouteService,
|
|
165
|
+
meta: { usageType: 'route' }
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export const helloClientModule: IModule = {
|
|
169
|
+
name: 'helloClient',
|
|
170
|
+
serviceDefs: [helloRouteDef]
|
|
171
|
+
};
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 步骤 5: 构建和打包
|
|
175
|
+
|
|
176
|
+
**推荐方式:使用开发模式 (dev)**
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# 一键打包、安装到开发环境
|
|
180
|
+
npx @linyjs/cli dev
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
这会:
|
|
184
|
+
1. 自动编译 TypeScript
|
|
185
|
+
2. 打包为 .mpk 文件
|
|
186
|
+
3. 安装到 SSR 的 extensions 目录
|
|
187
|
+
4. 注册到 packages.json
|
|
188
|
+
5. 准备就绪后重启 SSR 服务器即可测试
|
|
189
|
+
|
|
190
|
+
**传统方式:手动打包**
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
# 编译 TypeScript
|
|
194
|
+
npm run build
|
|
195
|
+
|
|
196
|
+
# 使用 CLI 打包为 .mpk 文件
|
|
197
|
+
npx @linyjs/cli pack
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
你会得到 `hello-plugin.mpk` 文件!
|
|
201
|
+
|
|
202
|
+
### 步骤 6: 测试插件
|
|
203
|
+
|
|
204
|
+
**使用 CLI dev 模式**(已自动完成安装和注册):
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
# 启动 SSR 服务器
|
|
208
|
+
cd /path/to/linyjs/packages/ssr
|
|
209
|
+
npx tsx src/demo/dev-server-with-pkg.tsx
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
访问 `http://localhost:3000/hello` 查看你的插件!
|
|
213
|
+
|
|
214
|
+
**手动部署**:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
# 将插件复制到 extensions 目录
|
|
218
|
+
cp hello-plugin.mpk /path/to/linyjs/packages/ssr/extensions/
|
|
219
|
+
|
|
220
|
+
# 启动 SSR 服务器
|
|
221
|
+
cd /path/to/linyjs/packages/ssr
|
|
222
|
+
npx tsx src/demo/dev-server-with-pkg.tsx
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
访问 `http://localhost:3000/hello` 查看你的插件!
|
|
226
|
+
|
|
227
|
+
### 下一步
|
|
228
|
+
|
|
229
|
+
- 📖 阅读 [安装指南](./installation.md) 了解详细的安装步骤
|
|
230
|
+
- 🎯 查看 [第一个插件教程](./first-plugin.md) 深入学习插件开发
|
|
231
|
+
- 💡 浏览 [示例项目](../../examples/) 获取完整代码参考
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
**上一篇**: [返回目录](../README.md) | **下一篇**: [安装指南](./installation.md)
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
# 用户认证
|
|
2
|
+
|
|
3
|
+
本指南介绍如何在 linyjs 插件中使用认证系统实现用户登录、注册和权限验证。
|
|
4
|
+
|
|
5
|
+
## 概述
|
|
6
|
+
|
|
7
|
+
linyjs 提供完整的用户认证系统,支持:
|
|
8
|
+
|
|
9
|
+
- 🔐 用户注册和登录
|
|
10
|
+
- 🪙 JWT Token 认证
|
|
11
|
+
- 🛡️ 路由级别的认证保护
|
|
12
|
+
- 👤 用户信息管理
|
|
13
|
+
|
|
14
|
+
## 配置认证服务
|
|
15
|
+
|
|
16
|
+
### 在服务端模块中注入依赖
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import type { IModule, IServiceDef } from '@linyjs/server-module-interface';
|
|
20
|
+
import { login, register, verifyToken } from '@linyjs/auth';
|
|
21
|
+
|
|
22
|
+
class AuthController {
|
|
23
|
+
// ... 控制器实现
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const authControllerDef: IServiceDef = {
|
|
27
|
+
serviceTag: 'authController',
|
|
28
|
+
serviceImpl: AuthController,
|
|
29
|
+
dependencies: [],
|
|
30
|
+
meta: { usageType: 'controller' }
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const myServerModule: IModule = {
|
|
34
|
+
name: 'myServer',
|
|
35
|
+
moduleDependencies: ['auth'],
|
|
36
|
+
serviceDefs: [authControllerDef]
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 基本认证操作
|
|
41
|
+
|
|
42
|
+
### 用户注册
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { register } from '@linyjs/auth';
|
|
46
|
+
|
|
47
|
+
class AuthController {
|
|
48
|
+
getRoutes() {
|
|
49
|
+
return [{
|
|
50
|
+
method: 'POST' as const,
|
|
51
|
+
path: '/api/auth/register',
|
|
52
|
+
middlewares: [],
|
|
53
|
+
responseType: 'json' as const,
|
|
54
|
+
handler: async (req: any) => {
|
|
55
|
+
const { username, email, password } = req.body;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = await register(username, email, password);
|
|
59
|
+
return {
|
|
60
|
+
success: true,
|
|
61
|
+
message: 'Registration successful',
|
|
62
|
+
data: result
|
|
63
|
+
};
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return {
|
|
66
|
+
success: false,
|
|
67
|
+
message: error instanceof Error ? error.message : 'Registration failed'
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 用户登录
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { login } from '@linyjs/auth';
|
|
80
|
+
|
|
81
|
+
class AuthController {
|
|
82
|
+
getRoutes() {
|
|
83
|
+
return [{
|
|
84
|
+
method: 'POST' as const,
|
|
85
|
+
path: '/api/auth/login',
|
|
86
|
+
middlewares: [],
|
|
87
|
+
responseType: 'json' as const,
|
|
88
|
+
handler: async (req: any) => {
|
|
89
|
+
const { identifier, password } = req.body;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const result = await login(identifier, password);
|
|
93
|
+
return {
|
|
94
|
+
success: true,
|
|
95
|
+
message: 'Login successful',
|
|
96
|
+
data: result
|
|
97
|
+
};
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
message: error instanceof Error ? error.message : 'Login failed'
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Token 验证
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { verifyToken, getUserById } from '@linyjs/auth';
|
|
114
|
+
|
|
115
|
+
class AuthController {
|
|
116
|
+
getRoutes() {
|
|
117
|
+
return [{
|
|
118
|
+
method: 'GET' as const,
|
|
119
|
+
path: '/api/auth/me',
|
|
120
|
+
middlewares: [],
|
|
121
|
+
responseType: 'json' as const,
|
|
122
|
+
handler: async (req: any) => {
|
|
123
|
+
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
124
|
+
|
|
125
|
+
if (!token) {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
message: 'No token provided'
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
const payload = await verifyToken(token);
|
|
134
|
+
const user = await getUserById(payload.userId);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
success: true,
|
|
138
|
+
data: user
|
|
139
|
+
};
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return {
|
|
142
|
+
success: false,
|
|
143
|
+
message: 'Invalid token'
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## 认证中间件
|
|
153
|
+
|
|
154
|
+
### 创建认证中间件
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { verifyToken, getUserById } from '@linyjs/auth';
|
|
158
|
+
|
|
159
|
+
async function authMiddleware(req: any, res: any, next: any) {
|
|
160
|
+
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
161
|
+
|
|
162
|
+
if (!token) {
|
|
163
|
+
return res.status(401).json({
|
|
164
|
+
success: false,
|
|
165
|
+
message: 'Authentication token required'
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const payload = await verifyToken(token);
|
|
171
|
+
const user = await getUserById(payload.userId);
|
|
172
|
+
|
|
173
|
+
if (!user) {
|
|
174
|
+
return res.status(401).json({
|
|
175
|
+
success: false,
|
|
176
|
+
message: 'User not found'
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// 将用户信息添加到请求对象
|
|
181
|
+
req.user = user;
|
|
182
|
+
req.userId = user._id;
|
|
183
|
+
|
|
184
|
+
next();
|
|
185
|
+
} catch (error) {
|
|
186
|
+
return res.status(401).json({
|
|
187
|
+
success: false,
|
|
188
|
+
message: 'Invalid authentication token'
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### 使用认证中间件
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
class ProtectedController {
|
|
198
|
+
getRoutes() {
|
|
199
|
+
return [{
|
|
200
|
+
method: 'GET' as const,
|
|
201
|
+
path: '/api/protected',
|
|
202
|
+
middlewares: [authMiddleware], // 使用认证中间件
|
|
203
|
+
responseType: 'json' as const,
|
|
204
|
+
handler: async (req: any) => {
|
|
205
|
+
return {
|
|
206
|
+
success: true,
|
|
207
|
+
message: `Hello ${req.user.username}! You have access to this protected route.`
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## 客户端认证
|
|
216
|
+
|
|
217
|
+
### 登录表单示例
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import type { IModule } from '@linyjs/client-module-interface';
|
|
221
|
+
import React, { useState } from 'react';
|
|
222
|
+
|
|
223
|
+
function LoginForm() {
|
|
224
|
+
const [formData, setFormData] = useState({
|
|
225
|
+
identifier: '',
|
|
226
|
+
password: ''
|
|
227
|
+
});
|
|
228
|
+
const [error, setError] = useState('');
|
|
229
|
+
|
|
230
|
+
const handleSubmit = async (e: React.FormEvent) => {
|
|
231
|
+
e.preventDefault();
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const response = await fetch('/api/auth/login', {
|
|
235
|
+
method: 'POST',
|
|
236
|
+
headers: {
|
|
237
|
+
'Content-Type': 'application/json'
|
|
238
|
+
},
|
|
239
|
+
body: JSON.stringify(formData)
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
const result = await response.json();
|
|
243
|
+
|
|
244
|
+
if (result.success) {
|
|
245
|
+
// 保存 token 到 localStorage 或 cookie
|
|
246
|
+
localStorage.setItem('token', result.data.token);
|
|
247
|
+
// 跳转或刷新页面
|
|
248
|
+
window.location.reload();
|
|
249
|
+
} else {
|
|
250
|
+
setError(result.message);
|
|
251
|
+
}
|
|
252
|
+
} catch (err) {
|
|
253
|
+
setError('Network error');
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
return (
|
|
258
|
+
<form onSubmit={handleSubmit}>
|
|
259
|
+
<div>
|
|
260
|
+
<label>用户名或邮箱:</label>
|
|
261
|
+
<input
|
|
262
|
+
type="text"
|
|
263
|
+
value={formData.identifier}
|
|
264
|
+
onChange={(e) => setFormData({ ...formData, identifier: e.target.value })}
|
|
265
|
+
required
|
|
266
|
+
/>
|
|
267
|
+
</div>
|
|
268
|
+
<div>
|
|
269
|
+
<label>密码:</label>
|
|
270
|
+
<input
|
|
271
|
+
type="password"
|
|
272
|
+
value={formData.password}
|
|
273
|
+
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
|
274
|
+
required
|
|
275
|
+
/>
|
|
276
|
+
</div>
|
|
277
|
+
{error && <div className="error">{error}</div>}
|
|
278
|
+
<button type="submit">登录</button>
|
|
279
|
+
</form>
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
class LoginFormService {
|
|
284
|
+
getComponents() {
|
|
285
|
+
return {
|
|
286
|
+
LoginForm: {
|
|
287
|
+
component: LoginForm,
|
|
288
|
+
meta: { usageType: 'normal_component' }
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const loginFormDef: IServiceDef = {
|
|
295
|
+
serviceTag: 'loginForm',
|
|
296
|
+
serviceImpl: LoginFormService,
|
|
297
|
+
meta: { usageType: 'normal_service' }
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
export const authClientModule: IModule = {
|
|
301
|
+
name: 'authClient',
|
|
302
|
+
serviceDefs: [loginFormDef]
|
|
303
|
+
};
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## 认证状态管理
|
|
307
|
+
|
|
308
|
+
### 创建认证 Hook
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
import { useState, useEffect } from 'react';
|
|
312
|
+
|
|
313
|
+
interface User {
|
|
314
|
+
_id: string;
|
|
315
|
+
username: string;
|
|
316
|
+
email: string;
|
|
317
|
+
createdAt: Date;
|
|
318
|
+
roles?: string[];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function useAuth() {
|
|
322
|
+
const [user, setUser] = useState<User | null>(null);
|
|
323
|
+
const [loading, setLoading] = useState(true);
|
|
324
|
+
|
|
325
|
+
useEffect(() => {
|
|
326
|
+
const token = localStorage.getItem('token');
|
|
327
|
+
|
|
328
|
+
if (!token) {
|
|
329
|
+
setLoading(false);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
fetch('/api/auth/me', {
|
|
334
|
+
headers: {
|
|
335
|
+
'Authorization': `Bearer ${token}`
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
.then(response => response.json())
|
|
339
|
+
.then(result => {
|
|
340
|
+
if (result.success) {
|
|
341
|
+
setUser(result.data);
|
|
342
|
+
} else {
|
|
343
|
+
localStorage.removeItem('token');
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
.finally(() => {
|
|
347
|
+
setLoading(false);
|
|
348
|
+
});
|
|
349
|
+
}, []);
|
|
350
|
+
|
|
351
|
+
const login = async (identifier: string, password: string) => {
|
|
352
|
+
const response = await fetch('/api/auth/login', {
|
|
353
|
+
method: 'POST',
|
|
354
|
+
headers: {
|
|
355
|
+
'Content-Type': 'application/json'
|
|
356
|
+
},
|
|
357
|
+
body: JSON.stringify({ identifier, password })
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
const result = await response.json();
|
|
361
|
+
|
|
362
|
+
if (result.success) {
|
|
363
|
+
localStorage.setItem('token', result.data.token);
|
|
364
|
+
setUser(result.data.user);
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return false;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const logout = () => {
|
|
372
|
+
localStorage.removeItem('token');
|
|
373
|
+
setUser(null);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const isAuthenticated = !!user;
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
user,
|
|
380
|
+
loading,
|
|
381
|
+
isAuthenticated,
|
|
382
|
+
login,
|
|
383
|
+
logout
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
## 最佳实践
|
|
389
|
+
|
|
390
|
+
### 1. Token 存储
|
|
391
|
+
- 使用 `localStorage` 或 `cookie` 存储 token
|
|
392
|
+
- 考虑安全性时优先使用 `httpOnly` cookie
|
|
393
|
+
- 实现 token 自动刷新机制
|
|
394
|
+
|
|
395
|
+
### 2. 错误处理
|
|
396
|
+
- 捕获并处理认证相关的错误
|
|
397
|
+
- 提供用户友好的错误消息
|
|
398
|
+
- 记录详细的错误日志用于调试
|
|
399
|
+
|
|
400
|
+
### 3. 安全性
|
|
401
|
+
- 始终在服务端验证 token
|
|
402
|
+
- 不要在客户端存储敏感信息
|
|
403
|
+
- 使用强密码策略
|
|
404
|
+
- 考虑实现两步验证增强安全性
|
|
405
|
+
|
|
406
|
+
### 4. 用户体验
|
|
407
|
+
- 记住用户的登录状态
|
|
408
|
+
- 自动续期 token 避免频繁登录
|
|
409
|
+
- 在 token 过期时友好提示用户
|
|
410
|
+
|
|
411
|
+
## 故障排除
|
|
412
|
+
|
|
413
|
+
### 常见问题
|
|
414
|
+
|
|
415
|
+
1. **Token 无法验证**
|
|
416
|
+
- 检查 JWT_SECRET 是否正确配置
|
|
417
|
+
- 确认 token 未过期
|
|
418
|
+
- 验证 token 格式是否正确
|
|
419
|
+
|
|
420
|
+
2. **用户无法登录**
|
|
421
|
+
- 检查用户名/邮箱和密码是否正确
|
|
422
|
+
- 确认用户账户已激活
|
|
423
|
+
- 检查数据库连接是否正常
|
|
424
|
+
|
|
425
|
+
3. **中间件无法获取用户信息**
|
|
426
|
+
- 确认 token 在请求头中正确传递
|
|
427
|
+
- 检查 verifyToken 函数是否正常工作
|
|
428
|
+
- 验证 getUserById 是否能找到对应用户
|