create-lupine 1.2.0 → 1.3.1
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 +13 -7
- package/index.js +236 -109
- package/package.json +1 -1
- package/templates/common/.agents/skills/lupine-framework/SKILL.md +3 -3
- package/templates/common/.agents/skills/lupine-framework/references/backend-api.md +16 -1
- package/templates/common/.agents/skills/lupine-framework/references/icon-handling.md +1 -1
- package/templates/common/.agents/skills/lupine-framework/references/mobile-interactions.md +17 -88
- package/templates/common/.agents/skills/lupine-framework/references/native-apps.md +47 -24
- package/templates/common/.agents/skills/lupine-framework/references/navigation-and-routing.md +1 -0
- package/templates/common/.agents/skills/lupine-framework/references/reactivity-and-state.md +2 -2
- package/templates/common/.agents/skills/lupine-framework/references/styling-and-themes.md +3 -1
- package/templates/common/.env.development +2 -0
- package/templates/common/.env.mobile +4 -3
- package/templates/common/_env +10 -5
- package/templates/common/apps/server/README.md +10 -0
- package/templates/common/dev/cp-folder.js +9 -4
- package/templates/lupine-template-cv-starter/README.md +33 -1
- package/templates/lupine-template-cv-starter/app/package.json +20 -0
- package/templates/lupine-template-cv-starter/app/src/app.tsx +6 -0
- package/templates/lupine-template-cv-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-cv-starter/app/src/index.test.tsx +71 -0
- package/templates/lupine-template-cv-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-cv-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-cv-starter/lupine.json +8 -7
- package/templates/lupine-template-cv-starter/web/src/index.html +9 -2
- package/templates/lupine-template-cv-starter/web/src/index.tsx +9 -14
- package/templates/lupine-template-cv-starter/web/src/lupine-template-cv-starter/index.html +9 -2
- package/templates/lupine-template-cv-starter/web/src/lupine-template-cv-starter/index.tsx +4 -20
- package/templates/lupine-template-doc-starter/README.md +33 -1
- package/templates/lupine-template-doc-starter/app/package.json +20 -0
- package/templates/lupine-template-doc-starter/app/src/app.tsx +6 -0
- package/templates/lupine-template-doc-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-doc-starter/app/src/index.test.tsx +78 -0
- package/templates/lupine-template-doc-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-doc-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-doc-starter/lupine.json +8 -7
- package/templates/lupine-template-doc-starter/web/src/index.html +9 -2
- package/templates/lupine-template-doc-starter/web/src/index.tsx +9 -14
- package/templates/lupine-template-doc-starter/web/src/lupine-template-doc-starter/index.html +9 -2
- package/templates/lupine-template-doc-starter/web/src/lupine-template-doc-starter/index.tsx +4 -20
- package/templates/lupine-template-fullstack-note/README.md +83 -0
- package/templates/{lupine-template-cv-starter → lupine-template-fullstack-note}/api/package.json +2 -2
- package/templates/lupine-template-fullstack-note/api/resources/install.mysql.sql +32 -0
- package/templates/lupine-template-fullstack-note/api/resources/install.sqlite.sql +27 -0
- package/templates/lupine-template-fullstack-note/api/src/service/answer.ts +39 -0
- package/templates/lupine-template-fullstack-note/api/src/service/api.ts +52 -0
- package/templates/lupine-template-fullstack-note/api/src/service/db.ts +78 -0
- package/templates/lupine-template-fullstack-note/api/src/service/notes-api.ts +86 -0
- package/templates/lupine-template-fullstack-note/api/src/service/root-api.ts +19 -0
- package/templates/lupine-template-fullstack-note/api/src/service/user-api.ts +182 -0
- package/templates/lupine-template-fullstack-note/app/package.json +19 -0
- package/templates/lupine-template-fullstack-note/app/src/app.tsx +75 -0
- package/templates/lupine-template-fullstack-note/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-fullstack-note/app/src/index.test.tsx +373 -0
- package/templates/lupine-template-fullstack-note/app/src/index.web.tsx +20 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/components.tsx +65 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/details.tsx +38 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/discover.tsx +25 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/home.tsx +76 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/inbox.tsx +49 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/login.tsx +54 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/me.tsx +32 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/notes.tsx +128 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/settings.tsx +45 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/AppMenu.tsx +44 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/api.ts +159 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/data.ts +20 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/locales/ja.ts +80 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/locales/zh-CN.ts +80 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/texts.ts +14 -0
- package/templates/lupine-template-fullstack-note/app/tsconfig.json +17 -0
- package/templates/lupine-template-fullstack-note/lupine.json +25 -0
- package/templates/lupine-template-fullstack-note/web/assets/favicon.ico +0 -0
- package/templates/{lupine-template-doc-starter/api → lupine-template-fullstack-note/web}/package.json +2 -2
- package/templates/lupine-template-fullstack-note/web/src/index.html +23 -0
- package/templates/lupine-template-fullstack-note/web/src/index.tsx +15 -0
- package/templates/lupine-template-fullstack-starter/README.md +64 -14
- package/templates/lupine-template-fullstack-starter/api/resources/install.mysql.sql +2 -2
- package/templates/lupine-template-fullstack-starter/api/resources/install.sqlite.sql +2 -2
- package/templates/lupine-template-fullstack-starter/api/src/service/contact-api.ts +2 -2
- package/templates/lupine-template-fullstack-starter/api/src/service/user-api.ts +72 -47
- package/templates/lupine-template-fullstack-starter/app/package.json +20 -0
- package/templates/lupine-template-fullstack-starter/app/src/app.tsx +76 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.test.tsx +276 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/about.tsx +27 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/contact.tsx +41 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/favorite.tsx +27 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/home.tsx +63 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/login.tsx +63 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/mine.tsx +44 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/profile.tsx +58 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/register.tsx +48 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/reset-password.tsx +41 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/settings.tsx +33 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/story.tsx +46 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/api.ts +127 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/parts.tsx +100 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/stories.ts +65 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/texts.ts +79 -0
- package/templates/lupine-template-fullstack-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-fullstack-starter/lupine.json +6 -7
- package/templates/lupine-template-fullstack-starter/web/src/admin/admin-patch.tsx +34 -0
- package/templates/lupine-template-fullstack-starter/web/src/{components → admin/components}/home-search-component.tsx +18 -25
- package/templates/lupine-template-fullstack-starter/web/src/{components → admin/components}/story-card.tsx +17 -8
- package/templates/lupine-template-fullstack-starter/web/src/{components → admin/components}/story-list.tsx +1 -2
- package/templates/lupine-template-fullstack-starter/web/src/{admin_dev → admin}/index.html +1 -0
- package/templates/lupine-template-fullstack-starter/web/src/{admin_dev → admin}/index.tsx +13 -28
- package/templates/lupine-template-fullstack-starter/web/src/{pages → admin/pages}/admin-cfg.tsx +23 -19
- package/templates/lupine-template-fullstack-starter/web/src/{pages → admin/pages}/admin-item-edit.tsx +3 -4
- package/templates/lupine-template-fullstack-starter/web/src/{pages → admin/pages}/admin-item-list.tsx +2 -3
- package/templates/lupine-template-fullstack-starter/web/src/{pages → admin/pages}/admin-user.tsx +15 -20
- package/templates/lupine-template-fullstack-starter/web/src/index.html +8 -2
- package/templates/lupine-template-fullstack-starter/web/src/index.tsx +11 -150
- package/templates/lupine-template-fullstack-starter/web/src/services/fetch-data.ts +7 -25
- package/templates/lupine-template-fullstack-starter/web/src/styles/base-css.ts +0 -3
- package/templates/lupine-template-fullstack-starter/web/src/styles/theme.ts +1 -2
- package/templates/lupine-template-responsive-starter/README.md +46 -2
- package/templates/lupine-template-responsive-starter/app/package.json +19 -0
- package/templates/lupine-template-responsive-starter/app/src/app.tsx +80 -0
- package/templates/lupine-template-responsive-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-responsive-starter/app/src/index.test.tsx +283 -0
- package/templates/lupine-template-responsive-starter/app/src/index.web.tsx +31 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/about.tsx +23 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/finance.tsx +48 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/mine.tsx +60 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/not-found.tsx +9 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-detail.tsx +43 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-edit.tsx +45 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-search.tsx +62 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/notes.tsx +71 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/settings.tsx +55 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/tools.tsx +112 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/icons.ts +7 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/layout.ts +18 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/locales/zh-CN.ts +89 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/menu.tsx +81 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/nav.ts +12 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/store.ts +72 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/texts.ts +11 -0
- package/templates/lupine-template-responsive-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-responsive-starter/lupine.json +7 -6
- package/templates/lupine-template-responsive-starter/web/src/index.html +9 -2
- package/templates/lupine-template-responsive-starter/web/src/index.tsx +9 -25
- package/templates/lupine-template-responsive-starter/web/src/lupine-template-responsive-starter/index.html +12 -6
- package/templates/lupine-template-responsive-starter/web/src/lupine-template-responsive-starter/index.tsx +12 -22
- package/templates/common/apps/server/src/app-loader.ts +0 -4
- package/templates/lupine-template-cv-starter/api/resources/config_default.json +0 -6
- package/templates/lupine-template-cv-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-cv-starter/api/src/index.ts +0 -15
- package/templates/lupine-template-cv-starter/web/src/client-env-keys.ts +0 -5
- package/templates/lupine-template-cv-starter/web/src/styles/base-css.ts +0 -15
- package/templates/lupine-template-doc-starter/api/resources/config_default.json +0 -6
- package/templates/lupine-template-doc-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-doc-starter/api/src/index.ts +0 -15
- package/templates/lupine-template-doc-starter/api/src/resources/config_default.json +0 -6
- package/templates/lupine-template-doc-starter/api/src/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-doc-starter/web/src/client-env-keys.ts +0 -5
- package/templates/lupine-template-doc-starter/web/src/styles/base-css.ts +0 -15
- package/templates/lupine-template-fullstack-starter/web/src/admin_dev/admin-about.tsx +0 -16
- package/templates/lupine-template-fullstack-starter/web/src/admin_dev/admin-patch.ts +0 -22
- package/templates/lupine-template-fullstack-starter/web/src/components/footer-menu.tsx +0 -52
- package/templates/lupine-template-fullstack-starter/web/src/components/footer.tsx +0 -15
- package/templates/lupine-template-fullstack-starter/web/src/components/header.tsx +0 -75
- package/templates/lupine-template-fullstack-starter/web/src/components/login-code-zh.tsx +0 -162
- package/templates/lupine-template-fullstack-starter/web/src/components/login-code.tsx +0 -165
- package/templates/lupine-template-fullstack-starter/web/src/components/login-zh.tsx +0 -118
- package/templates/lupine-template-fullstack-starter/web/src/components/login.tsx +0 -122
- package/templates/lupine-template-fullstack-starter/web/src/components/not-found-page.tsx +0 -12
- package/templates/lupine-template-fullstack-starter/web/src/components/register-zh.tsx +0 -161
- package/templates/lupine-template-fullstack-starter/web/src/components/register.tsx +0 -163
- package/templates/lupine-template-fullstack-starter/web/src/components/reset-password-zh.tsx +0 -166
- package/templates/lupine-template-fullstack-starter/web/src/components/reset-password.tsx +0 -177
- package/templates/lupine-template-fullstack-starter/web/src/components/user-info.tsx +0 -84
- package/templates/lupine-template-fullstack-starter/web/src/frames/admin-page-frame.tsx +0 -48
- package/templates/lupine-template-fullstack-starter/web/src/frames/frame1-page-frame.tsx +0 -61
- package/templates/lupine-template-fullstack-starter/web/src/frames/frame2-page-frame.tsx +0 -61
- package/templates/lupine-template-fullstack-starter/web/src/frames/top-frame.tsx +0 -23
- package/templates/lupine-template-fullstack-starter/web/src/frames/user-page-frame.tsx +0 -90
- package/templates/lupine-template-fullstack-starter/web/src/models/index.ts +0 -15
- package/templates/lupine-template-fullstack-starter/web/src/pages/contact.tsx +0 -105
- package/templates/lupine-template-fullstack-starter/web/src/pages/page1-page.tsx +0 -38
- package/templates/lupine-template-fullstack-starter/web/src/pages/page2-page.tsx +0 -38
- package/templates/lupine-template-fullstack-starter/web/src/pages/user-favorite-page.tsx +0 -58
- package/templates/lupine-template-fullstack-starter/web/src/pages/user-home-page.tsx +0 -27
- package/templates/lupine-template-fullstack-starter/web/src/pages/user-mine-page.tsx +0 -54
- package/templates/lupine-template-fullstack-starter/web/src/pages/user-profile-page-zh.tsx +0 -112
- package/templates/lupine-template-fullstack-starter/web/src/pages/user-profile-page.tsx +0 -112
- package/templates/lupine-template-fullstack-starter/web/src/pages/user1-page.tsx +0 -57
- package/templates/lupine-template-fullstack-starter/web/src/services/shared-data.ts +0 -19
- package/templates/lupine-template-fullstack-starter/web/src/styles/app.css +0 -0
- package/templates/lupine-template-responsive-starter/api/package.json +0 -6
- package/templates/lupine-template-responsive-starter/api/resources/config_default.json +0 -6
- package/templates/lupine-template-responsive-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-responsive-starter/api/src/service/root-api.ts +0 -18
- package/templates/lupine-template-responsive-starter/web/src/app-icons.ts +0 -76
- package/templates/lupine-template-responsive-starter/web/src/components/input-history-component.tsx +0 -93
- package/templates/lupine-template-responsive-starter/web/src/components/mine-about-page.tsx +0 -87
- package/templates/lupine-template-responsive-starter/web/src/components/mine-premium-page.tsx +0 -81
- package/templates/lupine-template-responsive-starter/web/src/components/mine-settings-page.tsx +0 -211
- package/templates/lupine-template-responsive-starter/web/src/components/mobile-top-search-menu.tsx +0 -107
- package/templates/lupine-template-responsive-starter/web/src/components/note-detail.tsx +0 -110
- package/templates/lupine-template-responsive-starter/web/src/components/note-edit.tsx +0 -144
- package/templates/lupine-template-responsive-starter/web/src/components/note-search-page.tsx +0 -188
- package/templates/lupine-template-responsive-starter/web/src/components/search-input.tsx +0 -95
- package/templates/lupine-template-responsive-starter/web/src/components/side-menu-content.tsx +0 -169
- package/templates/lupine-template-responsive-starter/web/src/frames/app-responsive-frame.tsx +0 -53
- package/templates/lupine-template-responsive-starter/web/src/pages/about-page.tsx +0 -32
- package/templates/lupine-template-responsive-starter/web/src/pages/finance-page.tsx +0 -35
- package/templates/lupine-template-responsive-starter/web/src/pages/home-page.tsx +0 -444
- package/templates/lupine-template-responsive-starter/web/src/pages/mine-page.tsx +0 -318
- package/templates/lupine-template-responsive-starter/web/src/pages/tools-page.tsx +0 -35
- package/templates/lupine-template-responsive-starter/web/src/services/local-notes-service.ts +0 -87
- package/templates/lupine-template-responsive-starter/web/src/services/mine-service.ts +0 -45
- package/templates/lupine-template-responsive-starter/web/src/styles/app.css +0 -0
- package/templates/lupine-template-responsive-starter/web/src/styles/base-css.ts +0 -65
- package/templates/lupine-template-responsive-starter/web/src/styles/global.css +0 -2130
- package/templates/lupine-template-responsive-starter/web/src/styles/theme.ts +0 -17
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/01-experience/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/02-projects/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/03-skills/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/04-education/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/01-experience/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/02-projects/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/03-skills/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/04-education/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/01-experience/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/02-projects/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/03-skills/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/04-education/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/markdown-config.ts +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/01-experience/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/02-projects/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/03-skills/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/04-education/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/essentials/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/essentials/list.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/guide/install.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/guide/started.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/essentials/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/essentials/list.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/guide/install.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/guide/started.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/essentials/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/essentials/list.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/guide/install.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/guide/started.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/markdown-config.ts +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/essentials/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/essentials/list.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/guide/install.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/guide/started.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/index.html +0 -0
- /package/templates/{lupine-template-responsive-starter → lupine-template-fullstack-note}/api/src/index.ts +0 -0
- /package/templates/lupine-template-fullstack-starter/{web/assets/images → app/assets}/sample.png +0 -0
- /package/templates/lupine-template-fullstack-starter/web/src/{components → admin/components}/story-props.ts +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#-- The tables of the app. api/src/service/db.ts makes them itself when they are first needed, so this file is only
|
|
2
|
+
#-- for a database made by hand. $__ is the prefix of the tables (DB_TABLE_PREFIX of .env, tbl_ by default).
|
|
3
|
+
|
|
4
|
+
#-- the accounts: password is a salted scrypt hash (scrypt$<salt>$<hash>), username is in lower case
|
|
5
|
+
CREATE TABLE IF NOT EXISTS $__account (
|
|
6
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
7
|
+
username VARCHAR(50) NOT NULL UNIQUE,
|
|
8
|
+
password VARCHAR(255) NOT NULL,
|
|
9
|
+
created_at VARCHAR(30) NOT NULL
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
#-- the notes: account_id is the id of $__account; the times are ISO (UTC)
|
|
13
|
+
CREATE TABLE IF NOT EXISTS $__user_note (
|
|
14
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
15
|
+
account_id INTEGER NOT NULL,
|
|
16
|
+
text TEXT NOT NULL,
|
|
17
|
+
created_at VARCHAR(30) NOT NULL,
|
|
18
|
+
updated_at VARCHAR(30) NOT NULL
|
|
19
|
+
);
|
|
20
|
+
CREATE INDEX IF NOT EXISTS $__user_note_account ON $__user_note (account_id, id);
|
|
21
|
+
|
|
22
|
+
#-- failed sign-ins of a user name (the brake), for any name: last_at is in milliseconds
|
|
23
|
+
CREATE TABLE IF NOT EXISTS $__login_failure (
|
|
24
|
+
username VARCHAR(50) NOT NULL PRIMARY KEY,
|
|
25
|
+
failures INTEGER NOT NULL DEFAULT 0,
|
|
26
|
+
last_at BIGINT NOT NULL DEFAULT 0
|
|
27
|
+
);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import { ApiHelper, Logger, ServerRequest } from 'lupine.api';
|
|
3
|
+
|
|
4
|
+
// Every answer of the API: { status: 'ok' | 'error', code, message, ...data }. The message is English, for logs and
|
|
5
|
+
// curl; the app shows its own text for each code (app/src/shell/api.ts), in the language of the user.
|
|
6
|
+
const logger = new Logger('note-api');
|
|
7
|
+
|
|
8
|
+
export type Answer = { status: 'ok' | 'error'; code: string; message: string; [key: string]: unknown };
|
|
9
|
+
|
|
10
|
+
/** answers of one user are never cached */
|
|
11
|
+
export const send = (req: ServerRequest, res: ServerResponse, answer: Answer, httpStatus = 200) => {
|
|
12
|
+
ApiHelper.sendJson(req, res, answer as any, httpStatus, { 'Cache-Control': 'no-store' });
|
|
13
|
+
return true;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const ok = (req: ServerRequest, res: ServerResponse, code: string, message: string, data: { [key: string]: unknown } = {}) =>
|
|
17
|
+
send(req, res, { status: 'ok', code, message, ...data });
|
|
18
|
+
|
|
19
|
+
export const fail = (req: ServerRequest, res: ServerResponse, httpStatus: number, code: string, message: string, data: { [key: string]: unknown } = {}) =>
|
|
20
|
+
send(req, res, { status: 'error', code, message, ...data }, httpStatus);
|
|
21
|
+
|
|
22
|
+
/** the JSON object of a POST, or undefined (no body, not JSON, an array) */
|
|
23
|
+
export const body = (req: ServerRequest): { [key: string]: unknown } | undefined => {
|
|
24
|
+
const data = req.locals.json();
|
|
25
|
+
return data && typeof data === 'object' && !Array.isArray(data) ? (data as { [key: string]: unknown }) : undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** A route: an error (the database, a bug) is logged and answered with code server_error, without its details. */
|
|
29
|
+
export const route =
|
|
30
|
+
(handler: (req: ServerRequest, res: ServerResponse) => Promise<boolean>) =>
|
|
31
|
+
async (req: ServerRequest, res: ServerResponse): Promise<boolean> => {
|
|
32
|
+
try {
|
|
33
|
+
return await handler(req, res);
|
|
34
|
+
} catch (e: any) {
|
|
35
|
+
logger.error(`${req.method} ${req.locals.urlWithoutQuery}: ${e && e.message}`);
|
|
36
|
+
if (!res.headersSent) fail(req, res, 500, 'server_error', 'Internal server error.');
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import { ApiRouter, IApiBase, ServerRequest } from 'lupine.api';
|
|
3
|
+
import { fail, ok, route } from './answer';
|
|
4
|
+
import { addNote, deleteNote, listNotes, updateNote } from './notes-api';
|
|
5
|
+
import { login, logout, me, register } from './user-api';
|
|
6
|
+
|
|
7
|
+
// The routes under /api. What writes is a POST, and the notes need a session.
|
|
8
|
+
export class Api implements IApiBase {
|
|
9
|
+
protected router = new ApiRouter();
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
// CSRF: a POST from a page of another site is refused. Browsers send Origin with every POST; the apps (iOS, Android,
|
|
13
|
+
// macOS, Windows) send none, and they pass. The session cookie is SameSite=lax as well.
|
|
14
|
+
this.router.setFilter(async (req: ServerRequest, res: ServerResponse) => {
|
|
15
|
+
const method = (req.method || 'GET').toUpperCase();
|
|
16
|
+
const origin = req.headers.origin;
|
|
17
|
+
if (method === 'GET' || method === 'HEAD' || method === 'OPTIONS' || !origin) return false;
|
|
18
|
+
try {
|
|
19
|
+
const originHost = new URL(origin).host.toLowerCase();
|
|
20
|
+
const originHostname = originHost.split(':')[0];
|
|
21
|
+
const requestHost = (req.headers.host || '').toLowerCase();
|
|
22
|
+
// the host names of the app (DOMAINS:<app>= of .env)
|
|
23
|
+
const trustedHosts: string[] = (req.locals?.hostPath?.hosts || []).map((h: string) => h.toLowerCase());
|
|
24
|
+
if (originHost === requestHost || trustedHosts.some((h) => originHostname === h || originHost === h || originHostname.endsWith('.' + h))) return false;
|
|
25
|
+
} catch {
|
|
26
|
+
// not a URL (Origin: null)
|
|
27
|
+
}
|
|
28
|
+
return fail(req, res, 403, 'forbidden_origin', 'Forbidden: invalid request origin.');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
this.router.get('/health', route(async (req, res) => ok(req, res, 'ok', 'OK.')));
|
|
32
|
+
|
|
33
|
+
// accounts
|
|
34
|
+
this.router.post('/register', route(register));
|
|
35
|
+
this.router.post('/login', route(login));
|
|
36
|
+
this.router.post('/logout', route(logout));
|
|
37
|
+
this.router.get('/me', route(me));
|
|
38
|
+
|
|
39
|
+
// the notes of the signed-in user
|
|
40
|
+
this.router.get('/notes', route(listNotes));
|
|
41
|
+
this.router.post('/notes/add', route(addNote));
|
|
42
|
+
this.router.post('/notes/update', route(updateNote));
|
|
43
|
+
this.router.post('/notes/delete', route(deleteNote));
|
|
44
|
+
|
|
45
|
+
// anything else under /api
|
|
46
|
+
this.router.use('*', route(async (req, res) => fail(req, res, 404, 'not_found', 'No such route.')));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getRouter(): ApiRouter {
|
|
50
|
+
return this.router;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { apiCache, Db } from 'lupine.api';
|
|
2
|
+
|
|
3
|
+
// The tables of the app, made when they are first needed: in the database of the app (sqlite by default:
|
|
4
|
+
// <app>_data/sqlite3.db; DB_TYPE of .env). The same tables, for a database made by hand: api/resources/install.*.sql.
|
|
5
|
+
// $__ is the prefix of the tables (DB_TABLE_PREFIX, tbl_ by default).
|
|
6
|
+
|
|
7
|
+
const SQLITE = [
|
|
8
|
+
`CREATE TABLE IF NOT EXISTS $__account (
|
|
9
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
10
|
+
username VARCHAR(50) NOT NULL UNIQUE,
|
|
11
|
+
password VARCHAR(255) NOT NULL,
|
|
12
|
+
created_at VARCHAR(30) NOT NULL
|
|
13
|
+
)`,
|
|
14
|
+
`CREATE TABLE IF NOT EXISTS $__user_note (
|
|
15
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
16
|
+
account_id INTEGER NOT NULL,
|
|
17
|
+
text TEXT NOT NULL,
|
|
18
|
+
created_at VARCHAR(30) NOT NULL,
|
|
19
|
+
updated_at VARCHAR(30) NOT NULL
|
|
20
|
+
)`,
|
|
21
|
+
'CREATE INDEX IF NOT EXISTS $__user_note_account ON $__user_note (account_id, id)',
|
|
22
|
+
`CREATE TABLE IF NOT EXISTS $__login_failure (
|
|
23
|
+
username VARCHAR(50) NOT NULL PRIMARY KEY,
|
|
24
|
+
failures INTEGER NOT NULL DEFAULT 0,
|
|
25
|
+
last_at BIGINT NOT NULL DEFAULT 0
|
|
26
|
+
)`,
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const MYSQL = [
|
|
30
|
+
`CREATE TABLE IF NOT EXISTS $__account (
|
|
31
|
+
id INTEGER NOT NULL AUTO_INCREMENT,
|
|
32
|
+
username VARCHAR(50) NOT NULL,
|
|
33
|
+
password VARCHAR(255) NOT NULL,
|
|
34
|
+
created_at VARCHAR(30) NOT NULL,
|
|
35
|
+
PRIMARY KEY (id),
|
|
36
|
+
UNIQUE KEY $__account_username (username)
|
|
37
|
+
) DEFAULT CHARSET=utf8mb4`,
|
|
38
|
+
`CREATE TABLE IF NOT EXISTS $__user_note (
|
|
39
|
+
id INTEGER NOT NULL AUTO_INCREMENT,
|
|
40
|
+
account_id INTEGER NOT NULL,
|
|
41
|
+
text TEXT NOT NULL,
|
|
42
|
+
created_at VARCHAR(30) NOT NULL,
|
|
43
|
+
updated_at VARCHAR(30) NOT NULL,
|
|
44
|
+
PRIMARY KEY (id),
|
|
45
|
+
KEY $__user_note_account (account_id, id)
|
|
46
|
+
) DEFAULT CHARSET=utf8mb4`,
|
|
47
|
+
`CREATE TABLE IF NOT EXISTS $__login_failure (
|
|
48
|
+
username VARCHAR(50) NOT NULL,
|
|
49
|
+
failures INTEGER NOT NULL DEFAULT 0,
|
|
50
|
+
last_at BIGINT NOT NULL DEFAULT 0,
|
|
51
|
+
PRIMARY KEY (username)
|
|
52
|
+
) DEFAULT CHARSET=utf8mb4`,
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
let ready: Promise<void> | undefined;
|
|
56
|
+
|
|
57
|
+
/** The database, with the tables. Other databases (sqlserver, oracle): make the tables yourself. */
|
|
58
|
+
export const db = async (): Promise<Db> => {
|
|
59
|
+
const d = apiCache.getDb();
|
|
60
|
+
if (!ready) {
|
|
61
|
+
const list = d.type === 'sqlite' ? SQLITE : d.type === 'mysql' || d.type === 'mariadb' ? MYSQL : [];
|
|
62
|
+
ready = (async () => {
|
|
63
|
+
for (const sql of list) await d.execute(sql);
|
|
64
|
+
})();
|
|
65
|
+
// try again with the next request (the database wasn't there yet ...)
|
|
66
|
+
ready.catch(() => (ready = undefined));
|
|
67
|
+
}
|
|
68
|
+
await ready;
|
|
69
|
+
return d;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** the id of a new row: insertId of sqlite and mysql */
|
|
73
|
+
export const insertedId = (result: any): number => Number((Array.isArray(result) ? result[0] : result)?.insertId);
|
|
74
|
+
/** how many rows an UPDATE or DELETE changed */
|
|
75
|
+
export const changedRows = (result: any): number => Number((Array.isArray(result) ? result[0] : result)?.affectedRows) || 0;
|
|
76
|
+
|
|
77
|
+
/** what is kept as the time: ISO, UTC (the app shows it in its own time zone) */
|
|
78
|
+
export const now = () => new Date().toISOString();
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import { ServerRequest } from 'lupine.api';
|
|
3
|
+
import { body, fail, ok } from './answer';
|
|
4
|
+
import { changedRows, db, insertedId, now } from './db';
|
|
5
|
+
import { needSession } from './user-api';
|
|
6
|
+
|
|
7
|
+
// The notes of the signed-in user: every query has the user's id in its WHERE, so nobody reaches the notes of another
|
|
8
|
+
// user, not even with the id of one.
|
|
9
|
+
const MAX_TEXT = 2000; // characters (an emoji is one)
|
|
10
|
+
const MAX_NOTES = 500; // of a user
|
|
11
|
+
const PAGE = 100; // notes of one answer, newest first
|
|
12
|
+
|
|
13
|
+
type Note = { id: number; text: string; created_at: string; updated_at: string };
|
|
14
|
+
const FIELDS = 'id, text, created_at, updated_at';
|
|
15
|
+
const plain = (n: any): Note => ({ id: Number(n.id), text: String(n.text), created_at: String(n.created_at), updated_at: String(n.updated_at) });
|
|
16
|
+
|
|
17
|
+
/** the text of a note: 1 to MAX_TEXT characters, or null */
|
|
18
|
+
const noteText = (value: unknown) => {
|
|
19
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
20
|
+
const length = Array.from(text).length;
|
|
21
|
+
return length >= 1 && length <= MAX_TEXT ? text : null;
|
|
22
|
+
};
|
|
23
|
+
const noteId = (value: unknown) => (typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : null);
|
|
24
|
+
const badText = (req: ServerRequest, res: ServerResponse) => fail(req, res, 400, 'bad_note', `A note has 1 to ${MAX_TEXT} characters.`);
|
|
25
|
+
const notFound = (req: ServerRequest, res: ServerResponse) => fail(req, res, 404, 'note_not_found', 'No such note.');
|
|
26
|
+
|
|
27
|
+
/** GET /api/notes?before=<id>: the newest notes (before that id: the next page); more: there are older ones */
|
|
28
|
+
export const listNotes = async (req: ServerRequest, res: ServerResponse) => {
|
|
29
|
+
const user = await needSession(req, res);
|
|
30
|
+
if (!user) return true;
|
|
31
|
+
const before = Number(req.locals.query.get('before') || 0);
|
|
32
|
+
const params: number[] = [user.id];
|
|
33
|
+
let sql = `SELECT ${FIELDS} FROM $__user_note WHERE account_id = ?`;
|
|
34
|
+
if (Number.isInteger(before) && before > 0) {
|
|
35
|
+
sql += ' AND id < ?';
|
|
36
|
+
params.push(before);
|
|
37
|
+
}
|
|
38
|
+
// one more than a page: are there more?
|
|
39
|
+
const rows = await (await db()).select(`${sql} ORDER BY id DESC LIMIT ${PAGE + 1}`, params);
|
|
40
|
+
return ok(req, res, 'ok', 'Notes.', { notes: rows.slice(0, PAGE).map(plain), more: rows.length > PAGE });
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** POST /api/notes/add { text } */
|
|
44
|
+
export const addNote = async (req: ServerRequest, res: ServerResponse) => {
|
|
45
|
+
const user = await needSession(req, res);
|
|
46
|
+
if (!user) return true;
|
|
47
|
+
const data = body(req);
|
|
48
|
+
if (!data) return fail(req, res, 400, 'bad_request', 'Wrong data.');
|
|
49
|
+
const text = noteText(data.text);
|
|
50
|
+
if (text === null) return badText(req, res);
|
|
51
|
+
const d = await db();
|
|
52
|
+
const [{ count }] = await d.select('SELECT COUNT(*) AS count FROM $__user_note WHERE account_id = ?', [user.id]);
|
|
53
|
+
if (Number(count) >= MAX_NOTES) return fail(req, res, 400, 'too_many_notes', `At most ${MAX_NOTES} notes: delete some first.`, { max: MAX_NOTES });
|
|
54
|
+
const at = now();
|
|
55
|
+
const id = insertedId(await d.execute('INSERT INTO $__user_note (account_id, text, created_at, updated_at) VALUES (?, ?, ?, ?)', [user.id, text, at, at]));
|
|
56
|
+
return ok(req, res, 'note_added', 'Note added.', { note: { id, text, created_at: at, updated_at: at } });
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** POST /api/notes/update { id, text } */
|
|
60
|
+
export const updateNote = async (req: ServerRequest, res: ServerResponse) => {
|
|
61
|
+
const user = await needSession(req, res);
|
|
62
|
+
if (!user) return true;
|
|
63
|
+
const data = body(req);
|
|
64
|
+
if (!data) return fail(req, res, 400, 'bad_request', 'Wrong data.');
|
|
65
|
+
const id = noteId(data.id);
|
|
66
|
+
if (id === null) return fail(req, res, 400, 'bad_request', 'Wrong id.');
|
|
67
|
+
const text = noteText(data.text);
|
|
68
|
+
if (text === null) return badText(req, res);
|
|
69
|
+
const d = await db();
|
|
70
|
+
const result = await d.execute('UPDATE $__user_note SET text = ?, updated_at = ? WHERE id = ? AND account_id = ?', [text, now(), id, user.id]);
|
|
71
|
+
if (!changedRows(result)) return notFound(req, res);
|
|
72
|
+
const [note] = await d.select(`SELECT ${FIELDS} FROM $__user_note WHERE id = ? AND account_id = ?`, [id, user.id]);
|
|
73
|
+
return note ? ok(req, res, 'note_updated', 'Note updated.', { note: plain(note) }) : notFound(req, res);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** POST /api/notes/delete { id } */
|
|
77
|
+
export const deleteNote = async (req: ServerRequest, res: ServerResponse) => {
|
|
78
|
+
const user = await needSession(req, res);
|
|
79
|
+
if (!user) return true;
|
|
80
|
+
const data = body(req);
|
|
81
|
+
if (!data) return fail(req, res, 400, 'bad_request', 'Wrong data.');
|
|
82
|
+
const id = noteId(data.id);
|
|
83
|
+
if (id === null) return fail(req, res, 400, 'bad_request', 'Wrong id.');
|
|
84
|
+
const result = await (await db()).execute('DELETE FROM $__user_note WHERE id = ? AND account_id = ?', [id, user.id]);
|
|
85
|
+
return changedRows(result) ? ok(req, res, 'note_deleted', 'Note deleted.', { id }) : notFound(req, res);
|
|
86
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ApiRouter, IApiBase, StaticServer } from 'lupine.api';
|
|
2
|
+
import { Api } from './api';
|
|
3
|
+
|
|
4
|
+
// /api: the API of the app (api.ts: accounts, sessions, notes); everything else is the web files (the page, the app)
|
|
5
|
+
// and the pages rendered on the server.
|
|
6
|
+
export class RootApi implements IApiBase {
|
|
7
|
+
protected router = new ApiRouter();
|
|
8
|
+
|
|
9
|
+
constructor() {
|
|
10
|
+
this.router.use('/api', new Api().getRouter());
|
|
11
|
+
|
|
12
|
+
const staticServer = new StaticServer();
|
|
13
|
+
this.router.use('*', staticServer.processRequest.bind(staticServer));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public getRouter(): ApiRouter {
|
|
17
|
+
return this.router;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import { CryptoUtils, getSessionExpireDays, isHttpsRequest, isSessionExpired, Logger, ServerRequest } from 'lupine.api';
|
|
3
|
+
import { body, fail, ok } from './answer';
|
|
4
|
+
import { db, insertedId, now } from './db';
|
|
5
|
+
|
|
6
|
+
// Accounts and sessions.
|
|
7
|
+
//
|
|
8
|
+
// The password is kept as a salted scrypt hash (CryptoUtils.hashPassword: "scrypt$<salt>$<hash>") and compared in
|
|
9
|
+
// constant time (CryptoUtils.verifyPassword). A sign-in answers the same for an unknown user name and a wrong password,
|
|
10
|
+
// and takes as long (an unknown name is checked against a hash of nothing).
|
|
11
|
+
//
|
|
12
|
+
// The session is a token encrypted with CRYPTO_KEY of .env (AES-256 and an HMAC: it can't be read or changed without
|
|
13
|
+
// the key). It holds the id and the name of the user, the time of the sign-in, and a hash of the stored password hash,
|
|
14
|
+
// so a new password ends every session of before. A browser gets it as the httpOnly cookie _token; an app (iOS, Android,
|
|
15
|
+
// macOS, Windows: no cookie store) in the answer, and sends it back as the cookie itself. Browsers send Origin with
|
|
16
|
+
// every POST and the apps don't, so the token is in the answer only without Origin: a script in a page never sees it.
|
|
17
|
+
|
|
18
|
+
const logger = new Logger('user-api');
|
|
19
|
+
|
|
20
|
+
export const COOKIE = '_token';
|
|
21
|
+
const NAME_RE = /^[a-z0-9_.@-]{3,50}$/;
|
|
22
|
+
const PASSWORD_MIN = 6;
|
|
23
|
+
const PASSWORD_MAX = 256;
|
|
24
|
+
// failed sign-ins of a user name: after MAX_FAILURES in a row, none for LOCK_MINUTES after the last one. Kept in the
|
|
25
|
+
// database, so every worker process and a restart know them; for every name, known or not, so the brake doesn't tell
|
|
26
|
+
// which names have an account. Limit the requests in front of the server, too (nginx: limit_req).
|
|
27
|
+
const MAX_FAILURES = 5;
|
|
28
|
+
const LOCK_MINUTES = 15;
|
|
29
|
+
const LOCK_MS = LOCK_MINUTES * 60 * 1000;
|
|
30
|
+
// SESSION_EXPIRE_DAYS of .env, 90 days by default
|
|
31
|
+
const expireDays = () => getSessionExpireDays('SESSION_EXPIRE_DAYS', 90);
|
|
32
|
+
|
|
33
|
+
/** a user name as it is kept: without spaces around it, in lower case ("Ada" and "ada" are one account) */
|
|
34
|
+
const normalName = (value: unknown) => (typeof value === 'string' ? value.trim().toLowerCase() : '');
|
|
35
|
+
const validPassword = (value: unknown): value is string => typeof value === 'string' && value.length >= PASSWORD_MIN && value.length <= PASSWORD_MAX;
|
|
36
|
+
|
|
37
|
+
// an unknown name is checked against this, so that it takes as long as a wrong password
|
|
38
|
+
const NOBODY = CryptoUtils.hashPassword(CryptoUtils.randomString(16));
|
|
39
|
+
|
|
40
|
+
export type SessionUser = { id: number; username: string };
|
|
41
|
+
type Account = { id: number; username: string; password: string };
|
|
42
|
+
type Token = { i: number; u: string; t: number; h: string };
|
|
43
|
+
|
|
44
|
+
const cryptoKey = () => process.env['CRYPTO_KEY'] || '';
|
|
45
|
+
|
|
46
|
+
/** No CRYPTO_KEY in .env: sessions can't be made or read. Answers that and returns true. */
|
|
47
|
+
export const noCryptoKey = (req: ServerRequest, res: ServerResponse) => {
|
|
48
|
+
if (cryptoKey()) return false;
|
|
49
|
+
logger.error('CRYPTO_KEY is not set in .env: no sign-in is possible');
|
|
50
|
+
return fail(req, res, 500, 'no_crypto_key', 'The server has no CRYPTO_KEY in its .env.');
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** a hash of the stored password hash: a new password changes it */
|
|
54
|
+
const passwordStamp = (a: Account) => CryptoUtils.sha256(`${a.id}:${a.username}:${a.password}`);
|
|
55
|
+
|
|
56
|
+
const startSession = (req: ServerRequest, res: ServerResponse, a: Account, code: string, message: string) => {
|
|
57
|
+
const token: Token = { i: a.id, u: a.username, t: Date.now(), h: passwordStamp(a) };
|
|
58
|
+
const encrypted = CryptoUtils.encrypt(JSON.stringify(token), cryptoKey());
|
|
59
|
+
req.locals.setCookie(COOKIE, encrypted, { expireDays: expireDays(), path: '/', httpOnly: true, secure: isHttpsRequest(req), sameSite: 'lax' });
|
|
60
|
+
return ok(req, res, code, message, { user: { id: a.id, username: a.username }, token: req.headers.origin ? undefined : encrypted });
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The user of the session (the cookie _token), or null: none, not ours, too old, an account that is gone, a new password. */
|
|
64
|
+
export const sessionUser = async (req: ServerRequest): Promise<SessionUser | null> => {
|
|
65
|
+
const encrypted = req.locals.cookies().get(COOKIE, '');
|
|
66
|
+
if (!encrypted || !cryptoKey()) return null;
|
|
67
|
+
let token: Token;
|
|
68
|
+
try {
|
|
69
|
+
token = JSON.parse(CryptoUtils.decrypt(encrypted, cryptoKey()));
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
if (!token || !Number.isInteger(token.i) || typeof token.u !== 'string' || typeof token.h !== 'string' || isSessionExpired(token.t, expireDays())) return null;
|
|
74
|
+
const [a] = await (await db()).select<Account>('SELECT id, username, password FROM $__account WHERE id = ?', [token.i]);
|
|
75
|
+
if (!a || a.username !== token.u || !CryptoUtils.timingSafeEqual(passwordStamp(a), token.h)) return null;
|
|
76
|
+
return { id: Number(a.id), username: a.username };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** The user of the session, or answers not_signed_in (or no_crypto_key) and returns null. */
|
|
80
|
+
export const needSession = async (req: ServerRequest, res: ServerResponse): Promise<SessionUser | null> => {
|
|
81
|
+
if (noCryptoKey(req, res)) return null;
|
|
82
|
+
const user = await sessionUser(req);
|
|
83
|
+
if (!user) fail(req, res, 401, 'not_signed_in', 'Not signed in.');
|
|
84
|
+
return user;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// ---- the brake on failed sign-ins
|
|
88
|
+
|
|
89
|
+
let cleaned = 0;
|
|
90
|
+
/** minutes until the name may try again, or 0 */
|
|
91
|
+
const lockedMinutes = async (username: string) => {
|
|
92
|
+
const [row] = await (await db()).select('SELECT failures, last_at FROM $__login_failure WHERE username = ?', [username]);
|
|
93
|
+
const left = row && Number(row.failures) >= MAX_FAILURES ? Number(row.last_at) + LOCK_MS - Date.now() : 0;
|
|
94
|
+
return left > 0 ? Math.ceil(left / 60000) : 0;
|
|
95
|
+
};
|
|
96
|
+
const recordFailure = async (username: string) => {
|
|
97
|
+
const d = await db();
|
|
98
|
+
const at = Date.now();
|
|
99
|
+
const [row] = await d.select('SELECT failures, last_at FROM $__login_failure WHERE username = ?', [username]);
|
|
100
|
+
if (row) {
|
|
101
|
+
// a failure long after the last one starts counting again
|
|
102
|
+
const failures = at - Number(row.last_at) > LOCK_MS ? 1 : Number(row.failures) + 1;
|
|
103
|
+
await d.execute('UPDATE $__login_failure SET failures = ?, last_at = ? WHERE username = ?', [failures, at, username]);
|
|
104
|
+
} else {
|
|
105
|
+
try {
|
|
106
|
+
await d.execute('INSERT INTO $__login_failure (username, failures, last_at) VALUES (?, ?, ?)', [username, 1, at]);
|
|
107
|
+
} catch {
|
|
108
|
+
// another request inserted it just now
|
|
109
|
+
await d.execute('UPDATE $__login_failure SET failures = failures + 1, last_at = ? WHERE username = ?', [at, username]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// the names nobody tried for a while (now and then)
|
|
113
|
+
if (at - cleaned > LOCK_MS) {
|
|
114
|
+
cleaned = at;
|
|
115
|
+
await d.execute('DELETE FROM $__login_failure WHERE last_at < ?', [at - LOCK_MS]);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// ---- the routes
|
|
120
|
+
|
|
121
|
+
/** POST /api/register { username, password }: a new account, signed in at once */
|
|
122
|
+
export const register = async (req: ServerRequest, res: ServerResponse) => {
|
|
123
|
+
if (noCryptoKey(req, res)) return true;
|
|
124
|
+
const data = body(req);
|
|
125
|
+
if (!data) return fail(req, res, 400, 'bad_request', 'Wrong data.');
|
|
126
|
+
const username = normalName(data.username);
|
|
127
|
+
if (!NAME_RE.test(username)) return fail(req, res, 400, 'bad_username', 'User name: 3 to 50 characters (letters, digits, _ . @ -).');
|
|
128
|
+
if (!validPassword(data.password)) return fail(req, res, 400, 'bad_password', `Password: ${PASSWORD_MIN} to ${PASSWORD_MAX} characters.`);
|
|
129
|
+
const d = await db();
|
|
130
|
+
const taken = () => fail(req, res, 409, 'username_taken', 'This user name is taken.');
|
|
131
|
+
if ((await d.select('SELECT id FROM $__account WHERE username = ?', [username])).length) return taken();
|
|
132
|
+
const password = await CryptoUtils.hashPasswordAsync(data.password);
|
|
133
|
+
let id: number;
|
|
134
|
+
try {
|
|
135
|
+
id = insertedId(await d.execute('INSERT INTO $__account (username, password, created_at) VALUES (?, ?, ?)', [username, password, now()]));
|
|
136
|
+
} catch (e) {
|
|
137
|
+
// UNIQUE: registered by another request at the same moment
|
|
138
|
+
if ((await d.select('SELECT id FROM $__account WHERE username = ?', [username])).length) return taken();
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
142
|
+
const [row] = await d.select('SELECT id FROM $__account WHERE username = ?', [username]);
|
|
143
|
+
id = Number(row.id);
|
|
144
|
+
}
|
|
145
|
+
logger.info(`new account ${id}`);
|
|
146
|
+
return startSession(req, res, { id, username, password }, 'registered', 'Account created.');
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/** POST /api/login { username, password } */
|
|
150
|
+
export const login = async (req: ServerRequest, res: ServerResponse) => {
|
|
151
|
+
if (noCryptoKey(req, res)) return true;
|
|
152
|
+
const data = body(req);
|
|
153
|
+
if (!data) return fail(req, res, 400, 'bad_request', 'Wrong data.');
|
|
154
|
+
const username = normalName(data.username);
|
|
155
|
+
const password = data.password;
|
|
156
|
+
const failed = () => fail(req, res, 401, 'login_failed', 'Wrong user name or password.');
|
|
157
|
+
// such a name has no account, and a password that long is never checked (scrypt of megabytes)
|
|
158
|
+
if (!NAME_RE.test(username) || typeof password !== 'string' || !password || password.length > PASSWORD_MAX) return failed();
|
|
159
|
+
const minutes = await lockedMinutes(username);
|
|
160
|
+
if (minutes) return fail(req, res, 429, 'too_many_attempts', `Too many failed sign-ins. Try again in ${minutes} minutes.`, { minutes });
|
|
161
|
+
const d = await db();
|
|
162
|
+
const [a] = await d.select<Account>('SELECT id, username, password FROM $__account WHERE username = ?', [username]);
|
|
163
|
+
const valid = await CryptoUtils.verifyPasswordAsync(password, a ? String(a.password) : NOBODY);
|
|
164
|
+
if (!a || !valid) {
|
|
165
|
+
await recordFailure(username);
|
|
166
|
+
return failed();
|
|
167
|
+
}
|
|
168
|
+
await d.execute('DELETE FROM $__login_failure WHERE username = ?', [username]);
|
|
169
|
+
return startSession(req, res, { id: Number(a.id), username: a.username, password: String(a.password) }, 'signed_in', 'Signed in.');
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** POST /api/logout: the browser forgets the cookie; an app forgets its token itself */
|
|
173
|
+
export const logout = async (req: ServerRequest, res: ServerResponse) => {
|
|
174
|
+
req.locals.setCookie(COOKIE, '', { expireDays: -1, path: '/', httpOnly: true, secure: isHttpsRequest(req), sameSite: 'lax' });
|
|
175
|
+
return ok(req, res, 'signed_out', 'Signed out.');
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** GET /api/me: who is signed in */
|
|
179
|
+
export const me = async (req: ServerRequest, res: ServerResponse) => {
|
|
180
|
+
const user = await needSession(req, res);
|
|
181
|
+
return user ? ok(req, res, 'ok', 'Signed in.', { user }) : true;
|
|
182
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lupine-template-fullstack-note-app",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Notes kept on the server, with accounts: a lupine.native app (tabs, pages, themes, languages, sign-in) for iOS, Android, macOS, Windows and the web.",
|
|
6
|
+
"lupineNative": {
|
|
7
|
+
"name": "LupineNotes",
|
|
8
|
+
"id": "com.uuware.lupine.notes",
|
|
9
|
+
"entry": {
|
|
10
|
+
"native": "src/index.native.tsx",
|
|
11
|
+
"test": "src/index.test.tsx",
|
|
12
|
+
"web": "src/index.web.tsx"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"lupine.native.components": "^1.1.0",
|
|
17
|
+
"lupine.native.core": "^1.1.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import './shell/texts';
|
|
2
|
+
import { createEffect, onBack, onMount, Text, untrack, View } from 'lupine.native.core';
|
|
3
|
+
import { col, Icon, NavBar, NavItem, OverlayHost, Pages, router, syncAppearance, t, Toast, tr, val } from 'lupine.native.components';
|
|
4
|
+
import { Components } from './pages/components';
|
|
5
|
+
import { About, Item, NotFound } from './pages/details';
|
|
6
|
+
import { Discover } from './pages/discover';
|
|
7
|
+
import { Home } from './pages/home';
|
|
8
|
+
import { Inbox, unread } from './pages/inbox';
|
|
9
|
+
import { Login } from './pages/login';
|
|
10
|
+
import { Me } from './pages/me';
|
|
11
|
+
import { Notes } from './pages/notes';
|
|
12
|
+
import { Settings } from './pages/settings';
|
|
13
|
+
import * as icons from 'lupine.core/icons';
|
|
14
|
+
import { api } from './shell/api';
|
|
15
|
+
|
|
16
|
+
const items: NavItem[] = [
|
|
17
|
+
{ key: '/', label: tr('nav.home', 'Home'), icon: icons.home, testID: 'tab-home' },
|
|
18
|
+
{ key: '/components', label: tr('nav.components', 'Components'), icon: icons.grid, testID: 'tab-components' },
|
|
19
|
+
{ key: '/discover', label: tr('nav.discover', 'Create'), icon: icons.plus, raised: true, testID: 'tab-discover' },
|
|
20
|
+
{ key: '/inbox', label: tr('nav.inbox', 'Inbox'), icon: icons.bell, badge: unread, testID: 'tab-inbox' },
|
|
21
|
+
{ key: '/me', label: tr('nav.me', 'Me'), icon: icons.user, testID: 'tab-me' },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
// the title of the browser's tab (the window on desktops) while a tab is shown; pages over the tabs: the name of the app
|
|
25
|
+
const appName = () => t('app.name', 'Lupine Notes');
|
|
26
|
+
const tabTitle = (key: string) => () => (key === '/' ? appName() : `${val(items.find((i) => i.key === key)?.label ?? '')} | ${appName()}`);
|
|
27
|
+
|
|
28
|
+
/** Wide windows: the menu stays on the left. Phones: a tab bar at the bottom, pushed pages cover it. */
|
|
29
|
+
export const App = () => {
|
|
30
|
+
untrack(syncAppearance); // before the first control is created
|
|
31
|
+
createEffect(syncAppearance);
|
|
32
|
+
// who is signed in; not while a server renders the page
|
|
33
|
+
onMount(() => void api.refresh());
|
|
34
|
+
let last = 0;
|
|
35
|
+
onBack(() => {
|
|
36
|
+
if (Date.now() - last < 2000) return false;
|
|
37
|
+
last = Date.now();
|
|
38
|
+
Toast.info(t('ln.pressAgainToExit', 'Press back again to exit'));
|
|
39
|
+
});
|
|
40
|
+
return (
|
|
41
|
+
<View style={{ flex: 1, backgroundColor: col('bg') }}>
|
|
42
|
+
<Pages
|
|
43
|
+
side={240}
|
|
44
|
+
title={appName}
|
|
45
|
+
tabs={[
|
|
46
|
+
{ path: '/', page: Home, title: tabTitle('/') },
|
|
47
|
+
{ path: '/components', page: Components, title: tabTitle('/components') },
|
|
48
|
+
{ path: '/discover', page: Discover, title: tabTitle('/discover') },
|
|
49
|
+
{ path: '/inbox', page: Inbox, title: tabTitle('/inbox') },
|
|
50
|
+
{ path: '/me', page: Me, title: tabTitle('/me') },
|
|
51
|
+
]}
|
|
52
|
+
routes={[
|
|
53
|
+
{ path: '/item/:id', page: ({ id }) => <Item id={id} /> },
|
|
54
|
+
{ path: '/about', page: () => <About /> },
|
|
55
|
+
{ path: '/settings', page: () => <Settings /> },
|
|
56
|
+
{ path: '/notes', page: () => <Notes /> },
|
|
57
|
+
{ path: '/login', page: () => <Login /> },
|
|
58
|
+
]}
|
|
59
|
+
notFound={() => <NotFound />}
|
|
60
|
+
>
|
|
61
|
+
{/* one menu: a tab bar at the bottom on phones, a column on the left on wide windows (css, also on the server) */}
|
|
62
|
+
<NavBar
|
|
63
|
+
items={items} active={router.tab} onPress={router.navigate} width={240}
|
|
64
|
+
header={
|
|
65
|
+
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 10, padding: 18 }}>
|
|
66
|
+
<Icon name={icons.logo} size={26} color={col('primary')} />
|
|
67
|
+
<Text style={{ fontSize: 17, fontWeight: '700', color: col('text') }}>{tr('app.name', 'Lupine Notes')}</Text>
|
|
68
|
+
</View>
|
|
69
|
+
}
|
|
70
|
+
/>
|
|
71
|
+
</Pages>
|
|
72
|
+
<OverlayHost />
|
|
73
|
+
</View>
|
|
74
|
+
);
|
|
75
|
+
};
|