@frigear-nu/users 3.1.0 → 3.1.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/dist/module.d.mts +7 -0
- package/dist/module.json +2 -2
- package/dist/module.mjs +15 -3
- package/dist/runtime/access.d.ts +1 -1
- package/dist/runtime/access.js +10 -5
- package/dist/runtime/app/components/UserCreateSlideover.d.vue.ts +18 -6
- package/dist/runtime/app/components/UserCreateSlideover.vue +61 -11
- package/dist/runtime/app/components/UserCreateSlideover.vue.d.ts +18 -6
- package/dist/runtime/app/components/UserPicker.d.vue.ts +43 -0
- package/dist/runtime/app/components/UserPicker.vue +158 -0
- package/dist/runtime/app/components/UserPicker.vue.d.ts +43 -0
- package/dist/runtime/app/composables/useUserActions.js +4 -3
- package/dist/runtime/app/plugins/remember-locale.client.d.ts +2 -0
- package/dist/runtime/app/plugins/remember-locale.client.js +23 -0
- package/dist/runtime/app/tabs/Overview.vue +25 -6
- package/dist/runtime/app/utils/admin-user.d.ts +7 -2
- package/dist/runtime/db/schema.postgresql.d.ts +51 -0
- package/dist/runtime/db/schema.postgresql.js +7 -0
- package/dist/runtime/db/schema.sqlite.d.ts +57 -0
- package/dist/runtime/db/schema.sqlite.js +7 -0
- package/dist/runtime/i18n/lang/da.json +23 -5
- package/dist/runtime/i18n/lang/en.json +21 -3
- package/dist/runtime/server/notify-recipients.d.ts +3 -0
- package/dist/runtime/server/notify-recipients.js +10 -0
- package/dist/runtime/server/routes/admin/user.get.js +7 -4
- package/dist/runtime/server/routes/admin/user.patch.js +12 -5
- package/dist/runtime/server/routes/admin/users.get.js +2 -5
- package/dist/runtime/server/routes/admin/users.post.js +21 -9
- package/dist/runtime/server/routes/me/locale.put.d.ts +2 -0
- package/dist/runtime/server/routes/me/locale.put.js +21 -0
- package/dist/runtime/server/routes/me/profile.patch.js +11 -5
- package/dist/runtime/server/schemas.d.ts +10 -0
- package/dist/runtime/server/schemas.js +6 -0
- package/dist/runtime/server/search-index.d.ts +2 -0
- package/dist/runtime/server/search-index.js +7 -0
- package/dist/runtime/server/user-store.js +10 -3
- package/dist/runtime/server/utils/dto.d.ts +10 -3
- package/dist/runtime/server/utils/dto.js +4 -1
- package/dist/runtime/server/utils/user-locale.d.ts +8 -0
- package/dist/runtime/server/utils/user-locale.js +7 -0
- package/dist/runtime/server/utils/user-phone.d.ts +6 -0
- package/dist/runtime/server/utils/user-phone.js +7 -0
- package/dist/runtime/server/utils/user-refs.d.ts +14 -0
- package/dist/runtime/server/utils/user-refs.js +40 -0
- package/dist/runtime/server/utils/user-search.d.ts +9 -0
- package/dist/runtime/server/utils/user-search.js +2 -0
- package/dist/runtime/utils/api.d.ts +9 -3
- package/dist/runtime/utils/api.js +7 -1
- package/dist/runtime/utils/locale.d.ts +10 -0
- package/dist/runtime/utils/locale.js +9 -0
- package/dist/runtime/utils/phone.d.ts +4 -0
- package/dist/runtime/utils/phone.js +2 -0
- package/package.json +10 -10
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
|
-
import { eq, getTableColumns, getTableName, is, sql, Table } from "drizzle-orm";
|
|
2
|
+
import { eq, getTableColumns, getTableName, inArray, is, sql, Table } from "drizzle-orm";
|
|
3
|
+
import { inChunks } from "@frigear-nu/module-kit/server";
|
|
3
4
|
import { getTableConfig as getSQLiteTableConfig } from "drizzle-orm/sqlite-core";
|
|
4
5
|
import { getTableConfig as getPgTableConfig, PgTable } from "drizzle-orm/pg-core";
|
|
5
6
|
const asId = (id) => {
|
|
@@ -39,16 +40,22 @@ const store = {
|
|
|
39
40
|
if (numeric === null) return null;
|
|
40
41
|
return await db.query.users.findFirst({ where: (t, { eq: eq2 }) => eq2(t.id, numeric) }) ?? null;
|
|
41
42
|
},
|
|
43
|
+
// Many at once (auth's `listRoleHolders`), in D1-sized chunks; ids that are not a user are left out.
|
|
44
|
+
async findByIds(ids) {
|
|
45
|
+
const numeric = [...new Set(ids.map(asId).filter((id) => id !== null))];
|
|
46
|
+
if (!numeric.length) return [];
|
|
47
|
+
return await inChunks(numeric, (part) => db.select().from(schema.users).where(inArray(schema.users.id, part)));
|
|
48
|
+
},
|
|
42
49
|
// Case-insensitive, index first (see `findUserByEmail`).
|
|
43
50
|
async findByEmail(email) {
|
|
44
51
|
return await findUserByEmail(email);
|
|
45
52
|
},
|
|
46
53
|
async create(values) {
|
|
47
|
-
const [user] = await db.insert(schema.users).values(withNormalizedEmail(values)).returning();
|
|
54
|
+
const [user] = await db.insert(schema.users).values(withUserSearchText(withNormalizedEmail(values))).returning();
|
|
48
55
|
return user;
|
|
49
56
|
},
|
|
50
57
|
async update(id, values) {
|
|
51
|
-
const [user] = await db.update(schema.users).set(withNormalizedEmail(values)).where(eq(schema.users.id, id)).returning();
|
|
58
|
+
const [user] = await db.update(schema.users).set(withUserSearchText(withNormalizedEmail(values))).where(eq(schema.users.id, id)).returning();
|
|
52
59
|
return user ?? null;
|
|
53
60
|
},
|
|
54
61
|
async remove(id) {
|
|
@@ -5,14 +5,21 @@ type UserRowLike = {
|
|
|
5
5
|
name: string;
|
|
6
6
|
email: string;
|
|
7
7
|
avatar?: string | null;
|
|
8
|
+
phone?: string | null;
|
|
9
|
+
locale?: string | null;
|
|
8
10
|
emailVerifiedAt?: Date | number | string | null;
|
|
9
11
|
lastLoginAt?: Date | number | string | null;
|
|
10
12
|
createdAt: Date | number | string;
|
|
11
13
|
};
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* A stored user as the users admin returns it: an allowlist (never the password hash or other modules' columns). `pii`
|
|
16
|
+
* (the caller holds `users:pii`) adds the phone number.
|
|
17
|
+
*/
|
|
18
|
+
export declare function toAdminUserDto(user: UserRowLike, options?: {
|
|
19
|
+
pii?: boolean;
|
|
20
|
+
}): AdminUserDto;
|
|
14
21
|
/** The signed-in user's own profile, as the self-service routes return it. */
|
|
15
|
-
export declare function toProfileDto(user: Pick<UserRowLike, 'name' | 'email' | 'avatar' | 'emailVerifiedAt'> & {
|
|
22
|
+
export declare function toProfileDto(user: Pick<UserRowLike, 'name' | 'email' | 'avatar' | 'phone' | 'locale' | 'emailVerifiedAt'> & {
|
|
16
23
|
id: number | string;
|
|
17
24
|
}): ProfileDto;
|
|
18
25
|
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { isoDate } from "@frigear-nu/module-kit/server";
|
|
2
|
-
export function toAdminUserDto(user) {
|
|
2
|
+
export function toAdminUserDto(user, options = {}) {
|
|
3
3
|
return {
|
|
4
4
|
id: user.id,
|
|
5
5
|
name: user.name,
|
|
6
6
|
email: user.email,
|
|
7
7
|
avatar: user.avatar ?? null,
|
|
8
|
+
...options.pii ? { phone: user.phone ?? null } : {},
|
|
8
9
|
emailVerifiedAt: isoDate(user.emailVerifiedAt),
|
|
9
10
|
lastLoginAt: isoDate(user.lastLoginAt),
|
|
10
11
|
createdAt: isoDate(user.createdAt)
|
|
@@ -16,6 +17,8 @@ export function toProfileDto(user) {
|
|
|
16
17
|
name: user.name,
|
|
17
18
|
email: user.email,
|
|
18
19
|
avatar: user.avatar ?? null,
|
|
20
|
+
phone: user.phone ?? null,
|
|
21
|
+
locale: user.locale ?? null,
|
|
19
22
|
emailVerifiedAt: isoDate(user.emailVerifiedAt)
|
|
20
23
|
};
|
|
21
24
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The app's i18n locale codes, the languages `users.locale` accepts; empty without i18n (then any short BCP-47 tag). */
|
|
2
|
+
export declare function usersLocales(): string[];
|
|
3
|
+
/**
|
|
4
|
+
* A language as `users.locale` stores it (the rule of the users routes: one of the app's i18n locales, else a short
|
|
5
|
+
* BCP-47 tag), or `null` when `value` is not a string, is empty or breaks the rule. For modules that store a user's
|
|
6
|
+
* language from their own input.
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseUserLocale(value: unknown): string | null;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A phone number as `users.phone` stores it (the rule of the users routes: trimmed, at most 40 digits, spaces and
|
|
3
|
+
* `+-().`), or `null` when `value` is not a string, is empty or breaks the rule. For modules that write `users.phone`
|
|
4
|
+
* from their own input, such as an application form.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseUserPhone(value: unknown): string | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from "../../utils/phone.js";
|
|
2
|
+
export function parseUserPhone(value) {
|
|
3
|
+
if (typeof value !== "string") return null;
|
|
4
|
+
const phone = value.trim();
|
|
5
|
+
if (!phone || phone.length > USER_PHONE_MAX_LENGTH || !USER_PHONE_PATTERN.test(phone)) return null;
|
|
6
|
+
return phone;
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { UserRef } from '../../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Users by id: `id`, `name`, `email`, `avatar`, `phone` and `locale` (the language to write to them in). Ids that are not whole numbers are skipped, and an unknown id
|
|
4
|
+
* is missing from the map. Any number of ids works: they are looked up in D1-sized chunks.
|
|
5
|
+
*/
|
|
6
|
+
export declare function loadUserRefs(ids: Iterable<number | string | null | undefined>): Promise<Map<number, UserRef>>;
|
|
7
|
+
/**
|
|
8
|
+
* Users whose name contains `q` (case-insensitively, `Ø`/`Æ`/`Å` too: the folded `users.searchText`), by name, at most `limit` (20, at most 100). With `pii` the email
|
|
9
|
+
* matches too and the refs carry `email` and `phone`; without it both are `null`, so the search cannot probe addresses.
|
|
10
|
+
*/
|
|
11
|
+
export declare function searchUserRefs(q: string, options?: {
|
|
12
|
+
pii?: boolean;
|
|
13
|
+
limit?: number;
|
|
14
|
+
}): Promise<UserRef[]>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { asc, inArray, or } from "drizzle-orm";
|
|
3
|
+
import { inChunks, likeContains, searchContains } from "@frigear-nu/module-kit/server";
|
|
4
|
+
const userRefColumns = () => ({
|
|
5
|
+
id: schema.users.id,
|
|
6
|
+
name: schema.users.name,
|
|
7
|
+
email: schema.users.email,
|
|
8
|
+
avatar: schema.users.avatar,
|
|
9
|
+
phone: schema.users.phone,
|
|
10
|
+
locale: schema.users.locale
|
|
11
|
+
});
|
|
12
|
+
const toUserRef = (row) => ({
|
|
13
|
+
id: Number(row.id),
|
|
14
|
+
name: row.name ?? null,
|
|
15
|
+
email: row.email ?? null,
|
|
16
|
+
avatar: row.avatar ?? null,
|
|
17
|
+
phone: row.phone || null,
|
|
18
|
+
locale: row.locale || null
|
|
19
|
+
});
|
|
20
|
+
function userIdsOf(ids) {
|
|
21
|
+
const unique = /* @__PURE__ */ new Set();
|
|
22
|
+
for (const id of ids) {
|
|
23
|
+
const numeric = typeof id === "string" && id.trim() ? Number(id) : id;
|
|
24
|
+
if (typeof numeric === "number" && Number.isSafeInteger(numeric)) unique.add(numeric);
|
|
25
|
+
}
|
|
26
|
+
return [...unique];
|
|
27
|
+
}
|
|
28
|
+
export async function loadUserRefs(ids) {
|
|
29
|
+
const unique = userIdsOf(ids);
|
|
30
|
+
if (!unique.length) return /* @__PURE__ */ new Map();
|
|
31
|
+
const rows = await inChunks(unique, (part) => db.select(userRefColumns()).from(schema.users).where(inArray(schema.users.id, part)));
|
|
32
|
+
return new Map(rows.map((row) => [Number(row.id), toUserRef(row)]));
|
|
33
|
+
}
|
|
34
|
+
export async function searchUserRefs(q, options = {}) {
|
|
35
|
+
const term = q.trim();
|
|
36
|
+
if (!term) return [];
|
|
37
|
+
const limit = Math.min(100, Math.max(1, Math.floor(options.limit ?? 20)));
|
|
38
|
+
const rows = await db.select(userRefColumns()).from(schema.users).where(options.pii ? or(searchContains(schema.users.searchText, term), likeContains(schema.users.email, term)) : searchContains(schema.users.searchText, term)).orderBy(asc(schema.users.name), asc(schema.users.id)).limit(limit);
|
|
39
|
+
return rows.map((row) => options.pii ? toUserRef(row) : { ...toUserRef(row), email: null, phone: null });
|
|
40
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `values` with the user's search column (`users.searchText`, the name folded for search) set whenever `name` is
|
|
3
|
+
* written. Every write of a user's name goes through it, so `searchUserRefs` finds `Øyvind` for `øyvind` on sqlite.
|
|
4
|
+
*/
|
|
5
|
+
export declare const withUserSearchText: <T extends {
|
|
6
|
+
name?: string | null;
|
|
7
|
+
}>(values: T) => T & {
|
|
8
|
+
searchText?: string;
|
|
9
|
+
};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import type { AdminUserDto, ProfileDto } from '../../types.js';
|
|
2
|
-
export type { AdminUserDto, ProfileDto };
|
|
1
|
+
import type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption } from '../../types.js';
|
|
2
|
+
export type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption };
|
|
3
|
+
export { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from './phone.js';
|
|
3
4
|
/** The users API: self-service under `/api/users`, staff under `/api/admin/users`. */
|
|
4
5
|
export declare const usersApi: {
|
|
5
6
|
readonly me: {
|
|
6
|
-
/** `PATCH { name
|
|
7
|
+
/** `PATCH { name?, avatar?, phone?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
7
8
|
readonly profile: "/api/users/me";
|
|
9
|
+
/**
|
|
10
|
+
* `PUT { locale, ifUnset? }`: sets the caller's language, answers the profile DTO. With `ifUnset: true` it only fills
|
|
11
|
+
* an empty one (what the app does at sign-in and on a language switch). `profile:write` (implicit).
|
|
12
|
+
*/
|
|
13
|
+
readonly locale: "/api/users/me/locale";
|
|
8
14
|
/** `PUT { email }`: changes the caller's email (or emails a confirmation link with `verifyEmailChange`). Session only. */
|
|
9
15
|
readonly email: "/api/users/me/email";
|
|
10
16
|
/** `PUT { currentPassword, newPassword, confirmNewPassword }`: changes the caller's password. Session only. */
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
export { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from "./phone.js";
|
|
1
2
|
const base = "/api/users";
|
|
2
3
|
const adminBase = "/api/admin/users";
|
|
3
4
|
export const usersApi = {
|
|
4
5
|
me: {
|
|
5
|
-
/** `PATCH { name
|
|
6
|
+
/** `PATCH { name?, avatar?, phone?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
6
7
|
profile: `${base}/me`,
|
|
8
|
+
/**
|
|
9
|
+
* `PUT { locale, ifUnset? }`: sets the caller's language, answers the profile DTO. With `ifUnset: true` it only fills
|
|
10
|
+
* an empty one (what the app does at sign-in and on a language switch). `profile:write` (implicit).
|
|
11
|
+
*/
|
|
12
|
+
locale: `${base}/me/locale`,
|
|
7
13
|
/** `PUT { email }`: changes the caller's email (or emails a confirmation link with `verifyEmailChange`). Session only. */
|
|
8
14
|
email: `${base}/me/email`,
|
|
9
15
|
/** `PUT { currentPassword, newPassword, confirmNewPassword }`: changes the caller's password. Session only. */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The longest language tag stored on a user. */
|
|
2
|
+
export declare const USER_LOCALE_MAX_LENGTH = 20;
|
|
3
|
+
/** A short BCP-47 language tag: a 2-3 letter language and up to two subtags (`da`, `en-GB`, `zh-Hant-TW`). */
|
|
4
|
+
export declare const USER_LOCALE_PATTERN: RegExp;
|
|
5
|
+
/**
|
|
6
|
+
* `value` as `users.locale` stores it, or `null` when it is not a language the app accepts. With `known` (the app's
|
|
7
|
+
* i18n locale codes) only those count, matched without regard to case and returned as the app spells them. Without
|
|
8
|
+
* them, any short BCP-47 tag counts (`USER_LOCALE_PATTERN`), as given.
|
|
9
|
+
*/
|
|
10
|
+
export declare function normalizeUserLocale(value: unknown, known?: readonly string[]): string | null;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const USER_LOCALE_MAX_LENGTH = 20;
|
|
2
|
+
export const USER_LOCALE_PATTERN = /^[a-z]{2,3}(?:-[a-z0-9]{2,8}){0,2}$/i;
|
|
3
|
+
export function normalizeUserLocale(value, known = []) {
|
|
4
|
+
if (typeof value !== "string") return null;
|
|
5
|
+
const tag = value.trim();
|
|
6
|
+
if (!tag || tag.length > USER_LOCALE_MAX_LENGTH) return null;
|
|
7
|
+
if (known.length) return known.find((code) => code.toLowerCase() === tag.toLowerCase()) ?? null;
|
|
8
|
+
return USER_LOCALE_PATTERN.test(tag) ? tag : null;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigear-nu/users",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "The user entity for the main frigear app: the users table, admin user management and self-service profile, email and password changes. Sign-in lives in @frigear-nu/auth.",
|
|
5
5
|
"homepage": "https://docs.frigear.dev/modules/identity/users",
|
|
6
6
|
"repository": {
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
"playground"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@frigear-nu/auth": "^0.6.
|
|
37
|
+
"@frigear-nu/auth": "^0.6.3",
|
|
38
38
|
"@frigear-nu/mcp": "^0.5.0",
|
|
39
|
-
"@frigear-nu/module-kit": "^1.3.
|
|
40
|
-
"@frigear-nu/notify": "^0.2.
|
|
41
|
-
"@frigear-nu/ui": "^0.2.
|
|
39
|
+
"@frigear-nu/module-kit": "^1.3.6",
|
|
40
|
+
"@frigear-nu/notify": "^0.2.6",
|
|
41
|
+
"@frigear-nu/ui": "^0.2.7",
|
|
42
42
|
"@libsql/client": "^0.18.0",
|
|
43
43
|
"@nuxt/kit": "^4.6.4",
|
|
44
44
|
"drizzle-kit": "^0.31.11",
|
|
@@ -72,21 +72,21 @@
|
|
|
72
72
|
}
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@electric-sql/pglite": "^0.5.
|
|
75
|
+
"@electric-sql/pglite": "^0.5.16",
|
|
76
76
|
"@frigear-nu/admin": "^0.5.0",
|
|
77
77
|
"@frigear-nu/email-templates": "^0.1.1",
|
|
78
78
|
"@frigear-nu/menu": "^0.4.0",
|
|
79
79
|
"@frigear-nu/widget": "^0.4.0",
|
|
80
|
-
"@nuxt/devtools": "^4.
|
|
80
|
+
"@nuxt/devtools": "^4.2.0",
|
|
81
81
|
"@nuxt/eslint-config": "^1.17.0",
|
|
82
|
-
"@nuxt/module-builder": "^1.0.
|
|
82
|
+
"@nuxt/module-builder": "^1.0.4",
|
|
83
83
|
"@nuxt/schema": "^4.6.4",
|
|
84
|
-
"@nuxt/test-utils": "^4.
|
|
84
|
+
"@nuxt/test-utils": "^4.4.0",
|
|
85
85
|
"@nuxt/ui": "^4.12.0",
|
|
86
86
|
"@nuxthub/core": "^0.10.12",
|
|
87
87
|
"@types/node": "^26.6.6",
|
|
88
88
|
"changelogen": "^0.6.2",
|
|
89
|
-
"eslint": "^10.
|
|
89
|
+
"eslint": "^10.15.0",
|
|
90
90
|
"nuxt": "^4.6.4",
|
|
91
91
|
"typescript": "^6.0.3",
|
|
92
92
|
"vitest": "^5.1.0",
|