@frigear-nu/users 1.0.8 → 1.0.10

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.
Files changed (103) hide show
  1. package/README.md +19 -136
  2. package/dist/module.d.mts +35 -48
  3. package/dist/module.json +1 -1
  4. package/dist/module.mjs +104 -169
  5. package/dist/runtime/admin-menus.js +16 -0
  6. package/dist/runtime/app/pages/admin-user.vue +52 -0
  7. package/dist/runtime/app/pages/admin.vue +252 -0
  8. package/dist/runtime/app/tabs/Overview.d.vue.ts +11 -0
  9. package/dist/runtime/app/tabs/Overview.vue +206 -0
  10. package/dist/runtime/app/tabs/Overview.vue.d.ts +11 -0
  11. package/dist/runtime/app/utils/admin-user.d.ts +22 -0
  12. package/dist/runtime/app/utils/admin-user.js +0 -0
  13. package/dist/runtime/app/widgets/NewMembers.d.vue.ts +3 -0
  14. package/dist/runtime/app/widgets/NewMembers.vue +45 -0
  15. package/dist/runtime/app/widgets/NewMembers.vue.d.ts +3 -0
  16. package/dist/runtime/app/widgets/NewMembersStat.d.vue.ts +3 -0
  17. package/dist/runtime/app/widgets/NewMembersStat.vue +20 -0
  18. package/dist/runtime/app/widgets/NewMembersStat.vue.d.ts +3 -0
  19. package/dist/runtime/db/email-changes.sqlite.d.ts +202 -0
  20. package/dist/runtime/db/email-changes.sqlite.js +16 -0
  21. package/dist/runtime/db/schema.sqlite.d.ts +5 -212
  22. package/dist/runtime/db/schema.sqlite.js +2 -21
  23. package/dist/runtime/i18n/lang/da.json +65 -87
  24. package/dist/runtime/i18n/lang/en.json +65 -87
  25. package/dist/runtime/server/mcp/tools/get-my-profile.js +6 -0
  26. package/dist/runtime/server/routes/admin/new-members.get.js +16 -0
  27. package/dist/runtime/server/routes/admin/roles.get.js +4 -0
  28. package/dist/runtime/server/routes/admin/stats.get.js +12 -0
  29. package/dist/runtime/server/routes/admin/user-resend.post.js +19 -0
  30. package/dist/runtime/server/routes/admin/user.delete.js +21 -0
  31. package/dist/runtime/server/routes/admin/user.get.js +32 -0
  32. package/dist/runtime/server/routes/admin/user.patch.js +45 -0
  33. package/dist/runtime/server/routes/admin/users.get.js +59 -0
  34. package/dist/runtime/server/routes/admin/users.post.js +33 -0
  35. package/dist/runtime/server/routes/me/change-email.post.js +35 -0
  36. package/dist/runtime/server/routes/me/change-password.post.d.ts +2 -0
  37. package/dist/runtime/server/routes/me/change-password.post.js +27 -0
  38. package/dist/runtime/server/routes/me/profile.post.d.ts +2 -0
  39. package/dist/runtime/server/routes/me/profile.post.js +17 -0
  40. package/dist/runtime/server/routes/me/revert-email-change.get.d.ts +2 -0
  41. package/dist/runtime/server/routes/me/revert-email-change.get.js +24 -0
  42. package/dist/runtime/server/routes/me/verify-email-change.get.d.ts +2 -0
  43. package/dist/runtime/server/routes/me/verify-email-change.get.js +37 -0
  44. package/dist/runtime/server/user-store.d.ts +3 -0
  45. package/dist/runtime/server/user-store.js +70 -0
  46. package/dist/runtime/server/utils/admin.d.ts +59 -0
  47. package/dist/runtime/server/utils/admin.js +53 -0
  48. package/dist/runtime/server/utils/notifications.d.ts +16 -0
  49. package/dist/runtime/server/utils/notifications.js +32 -0
  50. package/dist/runtime/server/utils/require-user.d.ts +12 -0
  51. package/dist/runtime/server/utils/require-user.js +9 -0
  52. package/dist/runtime/utils/index.d.ts +19 -0
  53. package/dist/runtime/utils/index.js +11 -0
  54. package/dist/types.d.mts +2 -2
  55. package/package.json +28 -16
  56. package/dist/runtime/app/composables/useAuthManifest.d.ts +0 -5
  57. package/dist/runtime/app/composables/useAuthManifest.js +0 -7
  58. package/dist/runtime/app/composables/useUserLogin.d.ts +0 -12
  59. package/dist/runtime/app/composables/useUserLogin.js +0 -26
  60. package/dist/runtime/app/composables/useUserPasskeys.d.ts +0 -7
  61. package/dist/runtime/app/composables/useUserPasskeys.js +0 -22
  62. package/dist/runtime/app/composables/useUserPasswordReset.d.ts +0 -13
  63. package/dist/runtime/app/composables/useUserPasswordReset.js +0 -32
  64. package/dist/runtime/app/composables/useUserRegistration.d.ts +0 -10
  65. package/dist/runtime/app/composables/useUserRegistration.js +0 -20
  66. package/dist/runtime/app/pages/login.vue +0 -68
  67. package/dist/runtime/app/pages/register.vue +0 -40
  68. package/dist/runtime/app/utils/oauth-providers.d.ts +0 -13
  69. package/dist/runtime/app/utils/oauth-providers.js +0 -4
  70. package/dist/runtime/menus.js +0 -14
  71. package/dist/runtime/server/routes/auth/forgot-password.post.js +0 -19
  72. package/dist/runtime/server/routes/auth/github.get.js +0 -7
  73. package/dist/runtime/server/routes/auth/google.get.js +0 -6
  74. package/dist/runtime/server/routes/auth/login.post.js +0 -15
  75. package/dist/runtime/server/routes/auth/logout.get.js +0 -4
  76. package/dist/runtime/server/routes/auth/register.post.js +0 -18
  77. package/dist/runtime/server/routes/auth/reset-password.post.js +0 -29
  78. package/dist/runtime/server/routes/auth/webauthn/authenticate.post.js +0 -34
  79. package/dist/runtime/server/routes/auth/webauthn/credentials/[id].delete.js +0 -9
  80. package/dist/runtime/server/routes/auth/webauthn/credentials/index.get.js +0 -8
  81. package/dist/runtime/server/routes/auth/webauthn/register.post.js +0 -42
  82. package/dist/runtime/server/utils/oauth.d.ts +0 -8
  83. package/dist/runtime/server/utils/oauth.js +0 -18
  84. package/dist/runtime/server/utils/password-reset.d.ts +0 -22
  85. package/dist/runtime/server/utils/password-reset.js +0 -14
  86. package/dist/runtime/server/utils/session.d.ts +0 -11
  87. package/dist/runtime/server/utils/session.js +0 -5
  88. /package/dist/runtime/{menus.d.ts → admin-menus.d.ts} +0 -0
  89. /package/dist/runtime/app/pages/{login.d.vue.ts → admin-user.d.vue.ts} +0 -0
  90. /package/dist/runtime/app/pages/{login.vue.d.ts → admin-user.vue.d.ts} +0 -0
  91. /package/dist/runtime/app/pages/{register.d.vue.ts → admin.d.vue.ts} +0 -0
  92. /package/dist/runtime/app/pages/{register.vue.d.ts → admin.vue.d.ts} +0 -0
  93. /package/dist/runtime/server/{routes/auth/forgot-password.post.d.ts → mcp/tools/get-my-profile.d.ts} +0 -0
  94. /package/dist/runtime/server/routes/{auth/github.get.d.ts → admin/new-members.get.d.ts} +0 -0
  95. /package/dist/runtime/server/routes/{auth/google.get.d.ts → admin/roles.get.d.ts} +0 -0
  96. /package/dist/runtime/server/routes/{auth/login.post.d.ts → admin/stats.get.d.ts} +0 -0
  97. /package/dist/runtime/server/routes/{auth/logout.get.d.ts → admin/user-resend.post.d.ts} +0 -0
  98. /package/dist/runtime/server/routes/{auth/register.post.d.ts → admin/user.delete.d.ts} +0 -0
  99. /package/dist/runtime/server/routes/{auth/reset-password.post.d.ts → admin/user.get.d.ts} +0 -0
  100. /package/dist/runtime/server/routes/{auth/webauthn/authenticate.post.d.ts → admin/user.patch.d.ts} +0 -0
  101. /package/dist/runtime/server/routes/{auth/webauthn/credentials/[id].delete.d.ts → admin/users.get.d.ts} +0 -0
  102. /package/dist/runtime/server/routes/{auth/webauthn/credentials/index.get.d.ts → admin/users.post.d.ts} +0 -0
  103. /package/dist/runtime/server/routes/{auth/webauthn/register.post.d.ts → me/change-email.post.d.ts} +0 -0
package/README.md CHANGED
@@ -5,167 +5,50 @@
5
5
  [![License][license-src]][license-href]
6
6
  [![Nuxt][nuxt-src]][nuxt-href]
7
7
 
8
- A module for managing the users in the main frigear app. for doing amazing things.
8
+ The user entity of the main frigear app: the `users` table, staff administration of users, and what a signed-in user can
9
+ change about themselves. How people sign in lives in [`@frigear-nu/auth`](../auth), which this module installs.
9
10
 
10
11
  ## Features
11
12
 
12
- - ⛰ Manages users & their schema in teh standard frigear way.
13
- - 🚠 Can be extended using other modules or fields on metadata
14
- - 🌲 Simple to use, supports billing & subscriptions.
15
- - 🔑 Email/password login & registration, with an on/off switch.
16
- - ✉️ Forgot/reset password flow, dispatched through a pluggable email driver (`@frigear-nu/notify`) so the module stays dependency-free.
17
- - 🔐 Passkey (WebAuthn) registration, authentication & credential management.
18
- - 🪪 "Simple" OAuth routes for GitHub & Google, powered by [`nuxt-auth-utils`](https://github.com/atinux/nuxt-auth-utils). Any other provider it supports can be wired up directly in the consuming app.
19
- - 🧩 Overridable composables (`useUserLogin`, `useUserRegistration`, `useUserPasswordReset`, `useUserPasskeys`) and opt-in login/register pages (built with [`@nuxt/ui`](https://ui.nuxt.com)).
20
- - 🧵 Session enrichment: the logged-in user is mapped into the session once, at login/registration time.
13
+ - ⛰ The `users` table and its schema, extended by other modules (credentials by `auth`, `role` by `authorization`, ...).
14
+ - 🛠 Staff administration (`settings.admin`): list, create, edit, delete and re-invite users, a "new members" widget and
15
+ an admin page (with `@frigear-nu/admin`). Policy is the host's, through the `users:admin:authorize` hook.
16
+ - 🧑 Self-service: change profile, email (optionally confirmed from the inbox) and password.
17
+ - 🧩 `requireUser` / `getUser` for server routes, and `extendUsersAdmin` for modules that add to the admin user page.
18
+ - 🔌 Registers the user store `@frigear-nu/auth` reads and writes users through.
21
19
 
22
20
  ## Quick Setup
23
21
 
24
- Install the module to your Nuxt application with one command:
25
-
26
22
  ```bash
27
23
  npx nuxt module add @frigear-nu/users
28
24
  ```
29
25
 
30
- That's it! You can now use @frigear-nu/users in your Nuxt app ✨
31
-
32
- This module requires [`@nuxthub/core`](https://hub.nuxt.com) to be configured in the host app, since it relies on it for
33
- the `users` (and, when WebAuthn is enabled, `user_credentials`) database table.
34
-
35
- ## Configuration
36
-
37
- Configure the module via the `users` key in `nuxt.config.ts`:
26
+ This module requires [`@nuxthub/core`](https://hub.nuxt.com) in the host app. Generate and commit a migration for the
27
+ tables it contributes (see the module-kit migrations guide).
38
28
 
39
29
  ```ts
40
30
  export default defineNuxtConfig({
41
31
  modules: ['@nuxthub/core', '@frigear-nu/users'],
42
32
  users: {
43
- mode: 'server',
44
33
  settings: {
45
- // Enable/disable email + password login & register routes, composables and pages.
46
- // Defaults to `true`.
47
- password: true,
48
- // Enable/disable the "forgot password"/"reset password" routes & composable. Requires
49
- // `password` to be enabled. Defaults to `true` whenever `password` is enabled.
50
- passwordReset: true,
51
- // Enable passkey (WebAuthn) registration/authentication routes, composables and DB table.
52
- // Requires the optional `@simplewebauthn/browser` and `@simplewebauthn/server` peer deps.
53
- // Defaults to `false`.
54
- webAuthn: false,
55
- // Registers the login/register pages, built with `@nuxt/ui` (an optional peer dependency,
56
- // required only when a page is enabled). Off by default; set to `true` for the default
57
- // path (`/login`, `/register`) or a custom path string to opt in.
58
- pages: {
59
- login: true,
60
- register: '/sign-up',
61
- },
62
- session: {
63
- // Path to a function (default export) mapping the raw DB user to the session user shape.
64
- mapper: '~/server/utils/map-session-user',
65
- },
66
- // Registers the "simple" OAuth routes we ship (`/api/auth/github`, `/api/auth/google`).
67
- // Each provider's client id/secret are read by nuxt-auth-utils itself from the standard
68
- // `NUXT_OAUTH_<PROVIDER>_CLIENT_ID` / `NUXT_OAUTH_<PROVIDER>_CLIENT_SECRET` env vars.
69
- // Route registration depends only on these flags. Any other provider supported by
70
- // nuxt-auth-utils can be added directly in the consuming app.
71
- oauth: {
72
- github: true,
73
- google: true,
74
- },
75
- },
76
- },
77
- })
78
- ```
79
-
80
- The password-reset email is dispatched as an `EmailNotification` through [`@frigear-nu/notify`](../notify), which this module declares as a `moduleDependency` (it's installed automatically - no need to add it to `modules` yourself). Which driver actually delivers it is an app/deployment concern, so it's configured separately, under the `notify` key:
81
-
82
- ```ts
83
- export default defineNuxtConfig({
84
- modules: ['@nuxthub/core', '@frigear-nu/users'],
85
- notify: {
86
- email: {
87
- // Name of a built-in driver (`'log'`, the default - logs instead of sending) or a path to
88
- // a function (default export) matching the `EmailDriver` signature, for a real provider.
89
- driver: '~/server/utils/resend-email-driver',
34
+ admin: true, // staff administration; `{ page: false }` = API only
35
+ verifyEmailChange: true, // a new address must be confirmed from its inbox
36
+ profilePath: '/account/profile', // where that confirmation lands
37
+ mcp: false, // the `get-my-profile` MCP tool
90
38
  },
91
39
  },
92
40
  })
93
41
  ```
94
42
 
95
- ```ts
96
- // server/utils/resend-email-driver.ts
97
- import type { EmailDriver } from '@frigear-nu/notify/types'
43
+ See the [documentation](../../apps/docs/content/4.modules/18.users.md) for the routes, hooks and the user page extension
44
+ points, and [`@frigear-nu/auth`](../auth/README.md) for sign-in (`authentication.settings`).
98
45
 
99
- const sendWithResend: EmailDriver = async (notification) => {
100
- // ... call your provider's API with notification.to/subject/body/text
101
- }
46
+ ## Development
102
47
 
103
- export default sendWithResend
48
+ ```bash
49
+ pnpm --filter '@frigear-nu/users' test
104
50
  ```
105
51
 
106
- ### Routes
107
-
108
- | Route | Enabled by | Description |
109
- | --- | --- | --- |
110
- | `POST /api/auth/login` | `settings.password` | Email/password login |
111
- | `POST /api/auth/register` | `settings.password` | Email/password registration |
112
- | `POST /api/auth/forgot-password` | `settings.passwordReset` | Request a password reset email for an `email` |
113
- | `POST /api/auth/reset-password` | `settings.passwordReset` | Reset the password given a reset `code`, `password` & `confirmPassword`, and sign in |
114
- | `POST /api/auth/webauthn/register` | `settings.webAuthn` | Register a new passkey |
115
- | `POST /api/auth/webauthn/authenticate` | `settings.webAuthn` | Authenticate with a passkey |
116
- | `GET /api/auth/webauthn/credentials` | `settings.webAuthn` | List the current user's passkeys |
117
- | `DELETE /api/auth/webauthn/credentials/:id` | `settings.webAuthn` | Remove one of the current user's passkeys |
118
- | `GET /api/auth/github`, `/google` | `settings.oauth.<provider>` | "Simple" OAuth login |
119
-
120
- ### Pages
121
-
122
- | Page | Enabled by | Description |
123
- | --- | --- | --- |
124
- | Login | `settings.pages.login` | Renders a `UAuthForm` with the password fields and/or OAuth provider buttons that are actually enabled |
125
- | Register | `settings.pages.register` | Renders a `UAuthForm` for name/email/password registration |
126
-
127
- ### Composables
128
-
129
- - `useUserLogin()` / `useUserRegistration()` – wrap the password routes above, and expose `isEnabled`/`passwordLoginEnabled`/`passkeyLoginEnabled`/`oauthProviders` computed from the module's public runtime config so the shipped pages (or your own) can render conditionally.
130
- - `useUserPasswordReset()` – wraps the forgot/reset-password routes above (`requestReset`, `resetPassword`), exposing `isEnabled` based on `settings.passwordReset`.
131
- - `useUserPasskeys()` – wraps `useWebAuthn` for registering/authenticating/listing passkeys.
132
-
133
- All composables are registered via Nuxt's import ecosystem, so they can be overridden by re-declaring a composable of
134
- the same name in the consuming app.
135
-
136
-
137
- ## Contribution
138
-
139
- <details>
140
- <summary>Local development</summary>
141
-
142
- ```bash
143
- # Install dependencies
144
- npm install
145
-
146
- # Generate type stubs
147
- npm run dev:prepare
148
-
149
- # Develop with the playground
150
- npm run dev
151
-
152
- # Build the playground
153
- npm run dev:build
154
-
155
- # Run ESLint
156
- npm run lint
157
-
158
- # Run Vitest
159
- npm run test
160
- npm run test:watch
161
-
162
- # Release new version
163
- npm run release
164
- ```
165
-
166
- </details>
167
-
168
-
169
52
  <!-- Badges -->
170
53
  [npm-version-src]: https://img.shields.io/npm/v/@frigear-nu/users/latest.svg?style=flat&colorA=020420&colorB=00DC82
171
54
  [npm-version-href]: https://npmjs.com/package/@frigear-nu/users
package/dist/module.d.mts CHANGED
@@ -1,72 +1,59 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
+ import { UsersAdminContext, UsersAdminRolesContext } from '../dist/runtime/server/utils/admin.js';
2
3
 
3
- type AuthMethod = (string | 'passkeys' | 'emailPassword');
4
- type AuthMethodObject = {
5
- label: string;
6
- value: AuthMethod;
7
- icon?: string;
8
- };
9
- type AuthManifest = {
10
- methods: AuthMethodObject[];
11
- pages: {
12
- login?: string;
13
- register?: string;
14
- };
15
- };
16
- declare const OAUTH_PROVIDERS: readonly ["github", "google"];
17
- type OAuthProvider = typeof OAUTH_PROVIDERS[number];
18
- type OAuthProviderDetails = {
19
- label: string;
20
- value: OAuthProvider;
21
- icon?: string;
22
- };
23
4
  type PublicModuleOptions = {
24
- password: boolean;
25
- passwordReset: boolean;
26
- webAuthn: boolean;
27
- oauth: OAuthProvider[];
5
+ /** Where the page of the signed-in user's profile lives; the email change confirmation link lands there. */
6
+ profilePath?: string;
7
+ /** Whether `settings.mcp` is on. */
8
+ mcp: boolean;
28
9
  pages: {
29
- login?: string;
30
- register?: string;
10
+ admin?: string;
31
11
  };
32
12
  };
33
13
  type ModuleOptions = {
34
- mode: 'server';
35
14
  settings?: {
36
- migrations?: boolean;
37
- password?: boolean;
38
- passwordReset?: boolean;
39
- webAuthn?: boolean;
40
- pages?: {
41
- login?: string | boolean;
42
- register?: string | boolean;
43
- };
44
- session?: {
45
- mapper?: string;
15
+ /**
16
+ * User administration for staff: list/create/edit users (`/api/admin/users`), a "new members" widget and an
17
+ * admin page. Who may do what is decided by the host in the `users:admin:authorize` Nitro hook (denied by
18
+ * default). `page` is the admin page path (`true` = `/admin/users`, `false` = no page).
19
+ */
20
+ admin?: boolean | {
21
+ page?: string | boolean;
46
22
  };
47
- oauth?: Partial<Record<OAuthProvider, boolean>>;
48
- };
49
- } | {
50
- mode: 'client';
51
- settings?: {
52
- mapFromHttp?: string;
53
- migrations?: false;
23
+ /**
24
+ * A new email address only takes effect once its owner confirms it through an emailed link; the old address then
25
+ * gets a notice with a link to revert the change. Defaults to `false` (applied immediately).
26
+ */
27
+ verifyEmailChange?: boolean;
28
+ /** Where the confirmation link of an email change lands (`?email-changed=1` or `?error=email-change`). Defaults to `/`. */
29
+ profilePath?: string;
30
+ /** Expose the MCP tools of this module (`get-my-profile`) on the MCP endpoint. Needs `@frigear-nu/mcp`. Defaults to `false`. */
31
+ mcp?: boolean;
54
32
  };
55
33
  };
56
34
 
35
+ declare module 'nitropack' {
36
+ interface NitroRuntimeHooks {
37
+ /** Staff gate of the users admin (`settings.admin`): set `ctx.allowed = true` to allow `ctx.action`. Denied by default. */
38
+ 'users:admin:authorize': (ctx: UsersAdminContext) => void | Promise<void>;
39
+ /** Roles the signed-in user may assign in the users admin: set `ctx.roles`. Defaults to the roles in use. */
40
+ 'users:admin:roles': (ctx: UsersAdminRolesContext) => void | Promise<void>;
41
+ }
42
+ }
57
43
  declare module 'nuxt/schema' {
58
44
  interface NuxtConfig {
59
45
  users?: ModuleOptions;
60
46
  }
61
47
  interface RuntimeConfig {
62
- users?: ModuleOptions;
48
+ users?: {
49
+ verifyEmailChange: boolean;
50
+ };
63
51
  }
64
52
  interface PublicRuntimeConfig {
65
- authMethods: AuthMethodObject[];
66
53
  users?: PublicModuleOptions;
67
54
  }
68
55
  }
69
56
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
70
57
 
71
- export { OAUTH_PROVIDERS, _default as default };
72
- export type { AuthManifest, AuthMethod, AuthMethodObject, ModuleOptions, OAuthProvider, OAuthProviderDetails, PublicModuleOptions };
58
+ export { _default as default };
59
+ export type { ModuleOptions, PublicModuleOptions };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@frigear-nu/users",
3
3
  "configKey": "users",
4
- "version": "1.0.8",
4
+ "version": "1.0.10",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,207 +1,142 @@
1
- import { defineNuxtModule, createResolver, hasNuxtModule, addServerTemplate, addServerImportsDir, addServerHandler, addImports, addTemplate, extendPages } from '@nuxt/kit';
2
- import { upperFirst } from 'scule';
3
- import { registerLocales, registerDatabaseTable } from '@frigear-nu/module-kit/utils';
1
+ import { defineNuxtModule, createResolver, addServerImportsDir, addServerHandler, hasNuxtModule } from '@nuxt/kit';
2
+ import { registerTailwindSource, registerLocales, registerDatabaseTable, inlineModuleRuntime, moduleApiUrl, resolvePagePath, addModulePages, addOptionalMenuSource } from '@frigear-nu/module-kit/utils';
3
+ import { setAuthUserStore, addAuthScope } from '@frigear-nu/auth/utils';
4
4
 
5
- const OAUTH_PROVIDERS = ["github", "google"];
6
-
7
- function resolvePagePath(setting, defaultPath) {
8
- if (typeof setting === "string") return setting;
9
- if (setting === true) return defaultPath;
10
- return void 0;
11
- }
12
5
  const module$1 = defineNuxtModule({
13
6
  meta: { name: "@frigear-nu/users", configKey: "users" },
14
7
  defaults: {
15
- mode: "server",
16
- settings: {
17
- migrations: true,
18
- password: true,
19
- webAuthn: false,
20
- pages: {}
21
- }
8
+ settings: {}
22
9
  },
23
- moduleDependencies: {
10
+ // `@frigear-nu/mcp` is only installed when `settings.mcp` is on.
11
+ moduleDependencies: (nuxt) => ({
24
12
  "@nuxthub/core": {
25
13
  version: ">=0.0.1"
26
14
  },
27
15
  "@frigear-nu/module-kit": {
28
16
  version: ">=0.0.1"
29
17
  },
30
- "@frigear-nu/notify": {
18
+ // Sign-in, sessions and `requireAuth`: the users module only says who the users are.
19
+ "@frigear-nu/auth": {
31
20
  version: ">=0.0.1"
32
21
  },
33
- "@frigear-nu/menu": {
22
+ // Email and password change notices are dispatched through it.
23
+ "@frigear-nu/notify": {
34
24
  version: ">=0.0.1"
35
25
  },
36
- "nuxt-auth-utils": {
26
+ "@frigear-nu/menu": {
37
27
  version: ">=0.0.1",
38
- defaults: { webAuthn: true }
39
- }
40
- },
28
+ optional: true
29
+ },
30
+ "@frigear-nu/widget": {
31
+ version: ">=0.0.0",
32
+ optional: true
33
+ },
34
+ // Provides the shared page components of the admin pages (`AdminPage`, `AdminTable`, ...).
35
+ "@frigear-nu/admin": {
36
+ version: ">=0.0.0",
37
+ optional: true
38
+ },
39
+ ...nuxt.options.users?.settings?.mcp === true ? { "@frigear-nu/mcp": { version: ">=0.0.1" } } : {}
40
+ }),
41
41
  async setup(options, nuxt) {
42
- if (options.mode === "client") {
43
- console.log("Users module is running in client mode - will be built accordingly to support oauth.");
44
- return;
45
- }
46
42
  const resolver = createResolver(import.meta.url);
43
+ registerTailwindSource(nuxt, resolver.resolve("./runtime/app"));
47
44
  registerLocales(nuxt, resolver.resolve("./runtime/i18n/lang"));
48
45
  const settings = options.settings ?? {};
49
- const withPassword = settings.password !== false;
50
- const withPasswordReset = withPassword && settings.passwordReset !== false;
51
- const withWebAuthn = settings.webAuthn === true;
52
- const withMenus = hasNuxtModule("@frigear-nu/menu");
53
- const withI18n = nuxt.options.modules?.some((mod) => mod === "@nuxtjs/i18n" || Array.isArray(mod) && mod[0] === "@nuxtjs/i18n");
54
- const authMethods = [];
55
- if (!withWebAuthn) {
56
- nuxt.options.auth ||= {};
57
- nuxt.options.auth.webAuthn = false;
58
- }
59
- const tablesToRegister = ["users"];
60
- if (withWebAuthn) {
61
- tablesToRegister.push("userPasskeys");
62
- }
63
- if (withPasswordReset) {
64
- tablesToRegister.push("passwordResets");
65
- }
66
46
  await registerDatabaseTable(nuxt, {
67
- name: tablesToRegister,
47
+ name: ["users"],
68
48
  resolver,
69
49
  schema: (dialect) => `./runtime/db/schema.${dialect}`
70
50
  });
71
- addServerTemplate({
72
- filename: "#users/session-mapper",
73
- getContents: async () => {
74
- if (!settings.session?.mapper) return "export default (user) => user";
75
- const mapperPath = await resolver.resolvePath(settings.session.mapper, { cwd: nuxt.options.rootDir });
76
- return `export { default } from ${JSON.stringify(mapperPath)}`;
77
- }
78
- });
51
+ setAuthUserStore(nuxt, resolver.resolve("./runtime/server/user-store"));
79
52
  addServerImportsDir(resolver.resolve("./runtime/server/utils"));
80
- addServerHandler({
81
- route: "/auth/logout",
82
- method: "get",
83
- handler: resolver.resolve("./runtime/server/routes/auth/logout.get")
84
- });
85
- if (withPassword) {
86
- authMethods.push({
87
- label: withI18n ? "auth.emailPassword.label" : "Email & Password",
88
- value: "emailPassword",
89
- icon: "i-lucide-user-lock"
90
- });
91
- addServerHandler({
92
- route: "/api/auth/login",
93
- handler: resolver.resolve("./runtime/server/routes/auth/login.post")
94
- });
95
- addServerHandler({
96
- route: "/api/auth/register",
97
- handler: resolver.resolve("./runtime/server/routes/auth/register.post")
98
- });
53
+ inlineModuleRuntime(nuxt, resolver);
54
+ for (const [path, file] of [
55
+ ["me/profile", "profile.post"],
56
+ ["me/change-email", "change-email.post"],
57
+ ["me/change-password", "change-password.post"]
58
+ ]) {
59
+ addServerHandler({ route: moduleApiUrl("users", path), method: "post", handler: resolver.resolve(`./runtime/server/routes/me/${file}`) });
99
60
  }
100
- if (withPasswordReset) {
101
- addServerHandler({
102
- route: "/api/auth/forgot-password",
103
- handler: resolver.resolve("./runtime/server/routes/auth/forgot-password.post")
104
- });
105
- addServerHandler({
106
- route: "/api/auth/reset-password",
107
- handler: resolver.resolve("./runtime/server/routes/auth/reset-password.post")
61
+ if (settings.verifyEmailChange) {
62
+ for (const file of ["verify-email-change", "revert-email-change"]) {
63
+ addServerHandler({
64
+ route: `/auth/${file}`,
65
+ method: "get",
66
+ handler: resolver.resolve(`./runtime/server/routes/me/${file}.get`)
67
+ });
68
+ }
69
+ await registerDatabaseTable(nuxt, {
70
+ name: ["accountEmailChanges"],
71
+ resolver,
72
+ schema: (dialect) => `./runtime/db/email-changes.${dialect}`
108
73
  });
109
74
  }
110
- addImports([
111
- { name: "useUserLogin", from: resolver.resolve("./runtime/app/composables/useUserLogin") },
112
- {
113
- name: "useUserRegistration",
114
- from: resolver.resolve("./runtime/app/composables/useUserRegistration")
115
- },
116
- { name: "useUserPasswordReset", from: resolver.resolve("./runtime/app/composables/useUserPasswordReset") }
117
- ]);
118
- if (withWebAuthn) {
119
- authMethods.push({
120
- label: withI18n ? "auth.passkeys.label" : "Passkey",
121
- value: "passkeys",
122
- icon: "i-lucide-key"
123
- });
124
- addServerHandler({
125
- route: "/api/auth/webauthn/register",
126
- handler: resolver.resolve("./runtime/server/routes/auth/webauthn/register.post")
127
- });
128
- addServerHandler({
129
- route: "/api/auth/webauthn/authenticate",
130
- handler: resolver.resolve("./runtime/server/routes/auth/webauthn/authenticate.post")
131
- });
132
- addServerHandler({
133
- route: "/api/auth/webauthn/credentials",
134
- handler: resolver.resolve("./runtime/server/routes/auth/webauthn/credentials/index.get")
135
- });
136
- addServerHandler({
137
- route: "/api/auth/webauthn/credentials/:id",
138
- method: "delete",
139
- handler: resolver.resolve("./runtime/server/routes/auth/webauthn/credentials/[id].delete")
140
- });
75
+ if (settings.mcp === true) {
76
+ const { addMcpDefinitions } = await import('@frigear-nu/mcp/utils');
77
+ addAuthScope(nuxt, { name: "profile:read", description: "Read your own name, email and role (MCP)" });
78
+ addMcpDefinitions(nuxt, resolver.resolve("./runtime/server/mcp"));
141
79
  }
142
- addImports([{
143
- name: "useUserPasskeys",
144
- from: resolver.resolve("./runtime/app/composables/useUserPasskeys")
145
- }]);
146
- const enabledOauthProviders = [];
147
- for (const provider of OAUTH_PROVIDERS) {
148
- if (!settings.oauth?.[provider]) continue;
149
- authMethods.push({
150
- label: withI18n ? `auth.oauth.${provider}.label` : upperFirst(provider),
151
- value: provider
152
- });
153
- addServerHandler({
154
- route: `/api/auth/${provider}`,
155
- handler: resolver.resolve(`./runtime/server/routes/auth/${provider}.get`)
156
- });
157
- enabledOauthProviders.push(provider);
80
+ nuxt.options.runtimeConfig.users = { verifyEmailChange: settings.verifyEmailChange === true };
81
+ const withAdmin = !!settings.admin;
82
+ const adminOptions = typeof settings.admin === "object" ? settings.admin : {};
83
+ const withAdminShell = hasNuxtModule("@frigear-nu/admin");
84
+ if (withAdmin && !withAdminShell) {
85
+ console.warn("[@frigear-nu/users] `settings.admin` is on, but `@frigear-nu/admin` is not installed: the admin API is registered, the admin pages are not.");
158
86
  }
159
- const loginPath = withPassword || enabledOauthProviders.length > 0 ? resolvePagePath(settings.pages?.login, "/login") : void 0;
160
- const registerPath = withPassword ? resolvePagePath(settings.pages?.register, "/register") : void 0;
161
- nuxt.options.runtimeConfig.public.authMethods = authMethods;
162
- const authManifest = {
163
- methods: authMethods,
164
- pages: {
165
- login: loginPath,
166
- register: registerPath
87
+ const adminPagePath = withAdmin && withAdminShell ? resolvePagePath(adminOptions.page ?? true, (await import('@frigear-nu/admin/utils')).adminRoute(nuxt, "users")) : void 0;
88
+ if (withAdmin) {
89
+ for (const [method, path, file] of [
90
+ ["get", "/api/admin/users", "users.get"],
91
+ ["post", "/api/admin/users", "users.post"],
92
+ ["get", "/api/admin/users/new-members", "new-members.get"],
93
+ ["get", "/api/admin/users/stats", "stats.get"],
94
+ ["get", "/api/admin/users/roles", "roles.get"],
95
+ ["get", "/api/admin/users/:id", "user.get"],
96
+ ["patch", "/api/admin/users/:id", "user.patch"],
97
+ ["delete", "/api/admin/users/:id", "user.delete"],
98
+ ["post", "/api/admin/users/:id/resend", "user-resend.post"]
99
+ ]) {
100
+ addServerHandler({ route: path, method, handler: resolver.resolve(`./runtime/server/routes/admin/${file}`) });
101
+ }
102
+ if (adminPagePath) {
103
+ addModulePages("@frigear-nu/users", resolver, [
104
+ { name: "users-admin", path: adminPagePath, file: "./runtime/app/pages/admin.vue" },
105
+ { name: "users-admin-user", path: `${adminPagePath.replace(/\/+$/, "")}/:id(\\d+)`, file: "./runtime/app/pages/admin-user.vue" }
106
+ ]);
107
+ }
108
+ if (hasNuxtModule("@frigear-nu/widget")) {
109
+ const { extendWidgets, extendTabs } = await import('@frigear-nu/widget/utils');
110
+ extendTabs(nuxt, "admin.users", {
111
+ id: "overview",
112
+ component: resolver.resolve("./runtime/app/tabs/Overview.vue"),
113
+ order: 0,
114
+ label: "Overview",
115
+ labelKey: "users.admin.tabs.overview",
116
+ icon: "i-lucide-user"
117
+ });
118
+ extendWidgets(nuxt, "admin.dashboard", {
119
+ id: "users-new-members",
120
+ component: resolver.resolve("./runtime/app/widgets/NewMembers.vue"),
121
+ order: 20,
122
+ span: 2
123
+ });
124
+ extendWidgets(nuxt, "admin.dashboard.top", {
125
+ id: "users-new-members-stat",
126
+ component: resolver.resolve("./runtime/app/widgets/NewMembersStat.vue"),
127
+ order: 10
128
+ });
167
129
  }
168
- };
169
- addTemplate({
170
- filename: "users/auth.manifest.json",
171
- write: true,
172
- getContents: () => JSON.stringify(authManifest, null, 2)
173
- });
174
- if (loginPath || registerPath) {
175
- extendPages((pages) => {
176
- const existingPaths = new Set(pages.map((page) => page.path));
177
- if (loginPath && !existingPaths.has(loginPath)) {
178
- pages.push({
179
- name: "users-login",
180
- path: loginPath,
181
- file: resolver.resolve("./runtime/app/pages/login.vue")
182
- });
183
- }
184
- if (registerPath && !existingPaths.has(registerPath)) {
185
- pages.push({
186
- name: "users-register",
187
- path: registerPath,
188
- file: resolver.resolve("./runtime/app/pages/register.vue")
189
- });
190
- }
191
- });
192
130
  }
193
- if (withMenus) {
194
- const { registerMenuSource } = await import('@frigear-nu/menu/utils');
195
- registerMenuSource(nuxt, { file: resolver.resolve("./runtime/menus"), menu: "account.dropdown" });
131
+ if (adminPagePath) {
132
+ addOptionalMenuSource(nuxt, { file: resolver.resolve("./runtime/admin-menus"), menu: "admin" });
196
133
  }
197
134
  nuxt.options.runtimeConfig.public.users = {
198
- password: withPassword,
199
- passwordReset: withPasswordReset,
200
- webAuthn: withWebAuthn,
201
- oauth: enabledOauthProviders,
202
- pages: { login: loginPath, register: registerPath }
135
+ profilePath: settings.profilePath,
136
+ mcp: settings.mcp === true,
137
+ pages: { admin: adminPagePath }
203
138
  };
204
139
  }
205
140
  });
206
141
 
207
- export { OAUTH_PROVIDERS, module$1 as default };
142
+ export { module$1 as default };
@@ -0,0 +1,16 @@
1
+ import { defineMenuSource, extendMenuItem } from "@frigear-nu/menu/utils";
2
+ export default defineMenuSource(() => {
3
+ const adminPage = useRuntimeConfig().public.users?.pages?.admin;
4
+ if (adminPage) {
5
+ extendMenuItem("admin.nav", {
6
+ id: "users-admin",
7
+ label: "Users",
8
+ labelKey: "users.admin.nav.label",
9
+ description: "Manage the users of the system.",
10
+ descriptionKey: "users.admin.nav.description",
11
+ icon: "i-lucide-users",
12
+ to: adminPage,
13
+ order: 10
14
+ });
15
+ }
16
+ });