@frigear-nu/users 3.1.4 → 3.1.6
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 +117 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +144 -27
- package/dist/runtime/access.js +3 -3
- package/dist/runtime/app/components/UserCreateSlideover.vue +8 -6
- package/dist/runtime/app/components/UserPicker.d.vue.ts +2 -2
- package/dist/runtime/app/components/UserPicker.vue.d.ts +2 -2
- package/dist/runtime/app/components/UsersAdminTerms.d.vue.ts +7 -0
- package/dist/runtime/app/components/UsersAdminTerms.vue +55 -0
- package/dist/runtime/app/components/UsersAdminTerms.vue.d.ts +7 -0
- package/dist/runtime/app/components/UsersTermsGate.d.vue.ts +11 -0
- package/dist/runtime/app/components/UsersTermsGate.vue +82 -0
- package/dist/runtime/app/components/UsersTermsGate.vue.d.ts +11 -0
- package/dist/runtime/app/components/UsersTermsText.d.vue.ts +7 -0
- package/dist/runtime/app/components/UsersTermsText.vue +34 -0
- package/dist/runtime/app/components/UsersTermsText.vue.d.ts +7 -0
- package/dist/runtime/app/composables/useUserActions.d.ts +6 -2
- package/dist/runtime/app/composables/useUserActions.js +32 -3
- package/dist/runtime/app/pages/admin-user.vue +3 -0
- package/dist/runtime/app/pages/admin.vue +23 -5
- package/dist/runtime/app/pages/revert-email-change.vue +2 -1
- package/dist/runtime/app/plugins/offer-locale.client.js +22 -3
- package/dist/runtime/app/plugins/terms-gate.client.d.ts +2 -0
- package/dist/runtime/app/plugins/terms-gate.client.js +55 -0
- package/dist/runtime/app/tabs/Overview.vue +88 -14
- package/dist/runtime/app/utils/admin-user.d.ts +4 -2
- package/dist/runtime/app/utils/terms.d.ts +10 -0
- package/dist/runtime/app/utils/terms.js +7 -0
- package/dist/runtime/app/widgets/NewUsers.d.vue.ts +4 -1
- package/dist/runtime/app/widgets/NewUsers.vue +7 -4
- package/dist/runtime/app/widgets/NewUsers.vue.d.ts +4 -1
- package/dist/runtime/app/widgets/NewUsersStat.d.vue.ts +4 -1
- package/dist/runtime/app/widgets/NewUsersStat.vue +4 -1
- package/dist/runtime/app/widgets/NewUsersStat.vue.d.ts +4 -1
- package/dist/runtime/app/widgets/UsersAccountTerms.d.vue.ts +6 -0
- package/dist/runtime/app/widgets/UsersAccountTerms.vue +63 -0
- package/dist/runtime/app/widgets/UsersAccountTerms.vue.d.ts +6 -0
- package/dist/runtime/app/widgets/UsersRegisterTerms.d.vue.ts +9 -0
- package/dist/runtime/app/widgets/UsersRegisterTerms.vue +37 -0
- package/dist/runtime/app/widgets/UsersRegisterTerms.vue.d.ts +9 -0
- package/dist/runtime/db/schema.postgresql.d.ts +68 -0
- package/dist/runtime/db/schema.postgresql.js +15 -4
- package/dist/runtime/db/schema.sqlite.d.ts +72 -0
- package/dist/runtime/db/schema.sqlite.js +15 -4
- package/dist/runtime/db/terms-users.postgresql.d.ts +24 -0
- package/dist/runtime/db/terms-users.postgresql.js +4 -0
- package/dist/runtime/db/terms-users.sqlite.d.ts +26 -0
- package/dist/runtime/db/terms-users.sqlite.js +4 -0
- package/dist/runtime/db/terms.postgresql.d.ts +162 -0
- package/dist/runtime/db/terms.postgresql.js +21 -0
- package/dist/runtime/db/terms.sqlite.d.ts +173 -0
- package/dist/runtime/db/terms.sqlite.js +21 -0
- package/dist/runtime/i18n/lang/da.json +85 -6
- package/dist/runtime/i18n/lang/en.json +85 -6
- package/dist/runtime/server/plugins/terms-claims.d.ts +13 -0
- package/dist/runtime/server/plugins/terms-claims.js +19 -0
- package/dist/runtime/server/plugins/terms.d.ts +15 -0
- package/dist/runtime/server/plugins/terms.js +43 -0
- package/dist/runtime/server/routes/admin/user-avatar.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/user-avatar.delete.js +11 -0
- package/dist/runtime/server/routes/admin/user-terms.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/user-terms.get.js +12 -0
- package/dist/runtime/server/routes/admin/user.delete.js +2 -0
- package/dist/runtime/server/routes/admin/user.patch.js +14 -3
- package/dist/runtime/server/routes/admin/users.get.js +11 -3
- package/dist/runtime/server/routes/admin/users.post.js +1 -0
- package/dist/runtime/server/routes/avatar.get.d.ts +2 -0
- package/dist/runtime/server/routes/avatar.get.js +12 -0
- package/dist/runtime/server/routes/me/avatar.delete.d.ts +2 -0
- package/dist/runtime/server/routes/me/avatar.delete.js +13 -0
- package/dist/runtime/server/routes/me/avatar.put.d.ts +2 -0
- package/dist/runtime/server/routes/me/avatar.put.js +20 -0
- package/dist/runtime/server/routes/me/profile.patch.js +23 -4
- package/dist/runtime/server/routes/me/terms.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/terms.get.js +4 -0
- package/dist/runtime/server/routes/me/terms.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/terms.post.js +18 -0
- package/dist/runtime/server/schemas.d.ts +12 -1
- package/dist/runtime/server/schemas.js +6 -1
- package/dist/runtime/server/search-index.js +2 -2
- package/dist/runtime/server/user-store.js +2 -1
- package/dist/runtime/server/utils/dto.d.ts +5 -1
- package/dist/runtime/server/utils/dto.js +8 -0
- package/dist/runtime/server/utils/user-refs.d.ts +2 -1
- package/dist/runtime/server/utils/user-refs.js +8 -2
- package/dist/runtime/server/utils/user-search.d.ts +13 -5
- package/dist/runtime/server/utils/user-search.js +13 -1
- package/dist/runtime/server/utils/users-avatar.d.ts +29 -0
- package/dist/runtime/server/utils/users-avatar.js +69 -0
- package/dist/runtime/server/utils/users-terms.d.ts +35 -0
- package/dist/runtime/server/utils/users-terms.js +90 -0
- package/dist/runtime/utils/api.d.ts +25 -4
- package/dist/runtime/utils/api.js +23 -3
- package/dist/runtime/utils/avatar.d.ts +25 -0
- package/dist/runtime/utils/avatar.js +23 -0
- package/dist/runtime/utils/profile.d.ts +14 -0
- package/dist/runtime/utils/profile.js +8 -0
- package/dist/runtime/utils/terms-claim.d.ts +27 -0
- package/dist/runtime/utils/terms-claim.js +15 -0
- package/dist/types.d.mts +1 -1
- package/package.json +18 -13
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { blob } from "#users/avatar-blob";
|
|
2
|
+
import { apiError, badRequest, notConfigured, notFound, validationError } from "@frigear-nu/module-kit/server";
|
|
3
|
+
import { IMAGE_EXTENSIONS, IMAGE_TYPES, imageSize, sniffImageType } from "@frigear-nu/module-kit/image";
|
|
4
|
+
import { USERS_MAX_AVATAR_BYTES, USERS_MAX_AVATAR_DIMENSION, absoluteUserAvatarUrl, parseUploadedUserAvatar } from "../../utils/avatar.js";
|
|
5
|
+
import { usersApi } from "../../utils/api.js";
|
|
6
|
+
const MULTIPART_ALLOWANCE = 16 * 1024;
|
|
7
|
+
const blobPathname = (userId, file) => `avatars/${userId}/${file}`;
|
|
8
|
+
const contentTypeOf = (file) => IMAGE_TYPES.find((type) => file.endsWith(`.${IMAGE_EXTENSIONS[type]}`));
|
|
9
|
+
export function usersMaxAvatarBytes(event) {
|
|
10
|
+
return Number(useRuntimeConfig(event).public.users?.maxAvatarBytes) || USERS_MAX_AVATAR_BYTES;
|
|
11
|
+
}
|
|
12
|
+
export function usersAvatarOrigin(event) {
|
|
13
|
+
return configuredAuthSiteOrigin(event) ?? getRequestURL(event).origin;
|
|
14
|
+
}
|
|
15
|
+
export async function storeUserAvatar(event, userId) {
|
|
16
|
+
if (!blob) throw notConfigured("Avatar uploads are not enabled.");
|
|
17
|
+
if (!(getRequestHeader(event, "content-type") ?? "").includes("multipart/form-data")) throw badRequest("Send the image as multipart/form-data.");
|
|
18
|
+
const maxBytes = usersMaxAvatarBytes(event);
|
|
19
|
+
const tooLarge = () => apiError(413, "file_too_large", `The image is larger than ${Math.round(maxBytes / 1024 / 1024 * 10) / 10} MB.`, { maxBytes });
|
|
20
|
+
const length = Number(getRequestHeader(event, "content-length"));
|
|
21
|
+
if (!Number.isFinite(length) || length <= 0 || length > maxBytes + MULTIPART_ALLOWANCE) throw tooLarge();
|
|
22
|
+
const parts = await readMultipartFormData(event) ?? [];
|
|
23
|
+
const file = parts.find((part) => part.name === "file" && part.filename);
|
|
24
|
+
if (!file) throw validationError([{ path: ["file"], message: "Choose an image." }], "Missing file");
|
|
25
|
+
if (file.data.byteLength > maxBytes) throw tooLarge();
|
|
26
|
+
const bytes = new Uint8Array(file.data.buffer, file.data.byteOffset, file.data.byteLength);
|
|
27
|
+
const type = sniffImageType(bytes);
|
|
28
|
+
if (!type) throw apiError(415, "unsupported_media_type", "The image must be a PNG, JPEG or WebP.");
|
|
29
|
+
const size = imageSize(bytes);
|
|
30
|
+
if (!size || !size.width || !size.height) throw apiError(400, "unreadable_image", "The size of the image could not be read.");
|
|
31
|
+
if (size.width > USERS_MAX_AVATAR_DIMENSION || size.height > USERS_MAX_AVATAR_DIMENSION) {
|
|
32
|
+
throw apiError(400, "image_too_large", `The image may be at most ${USERS_MAX_AVATAR_DIMENSION} pixels wide and high.`);
|
|
33
|
+
}
|
|
34
|
+
const saved = await blob.put(`avatar.${IMAGE_EXTENSIONS[type]}`, file.data, { contentType: type, prefix: `avatars/${userId}`, addRandomSuffix: true });
|
|
35
|
+
const name = saved.pathname.split("/").pop();
|
|
36
|
+
return new URL(usersApi.avatar(userId, name), usersAvatarOrigin(event)).href;
|
|
37
|
+
}
|
|
38
|
+
export async function serveUserAvatar(event, userId, file) {
|
|
39
|
+
const contentType = contentTypeOf(file);
|
|
40
|
+
if (!blob || !contentType) throw notFound("There is no image.");
|
|
41
|
+
const stored = await blob.get(blobPathname(userId, file));
|
|
42
|
+
if (!stored) throw notFound("There is no image.");
|
|
43
|
+
setResponseHeader(event, "content-type", contentType);
|
|
44
|
+
setResponseHeader(event, "content-disposition", "inline");
|
|
45
|
+
setResponseHeader(event, "x-content-type-options", "nosniff");
|
|
46
|
+
setResponseHeader(event, "cache-control", "public, max-age=31536000, immutable");
|
|
47
|
+
return stored;
|
|
48
|
+
}
|
|
49
|
+
export async function deleteUserAvatar(avatar, ownerId) {
|
|
50
|
+
const uploaded = parseUploadedUserAvatar(avatar);
|
|
51
|
+
if (!blob || !uploaded || uploaded.userId !== Number(ownerId)) return;
|
|
52
|
+
await blob.del(blobPathname(uploaded.userId, uploaded.file)).catch((error) => console.error("[users] an avatar image could not be deleted", error));
|
|
53
|
+
}
|
|
54
|
+
export async function deleteUserAvatarFiles(userId) {
|
|
55
|
+
if (!blob) return;
|
|
56
|
+
try {
|
|
57
|
+
let cursor;
|
|
58
|
+
do {
|
|
59
|
+
const page = await blob.list({ prefix: `avatars/${userId}/`, cursor });
|
|
60
|
+
if (page.blobs.length) await blob.del(page.blobs.map((stored) => stored.pathname));
|
|
61
|
+
cursor = page.hasMore ? page.cursor : void 0;
|
|
62
|
+
} while (cursor);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error(`[users] the avatar images of user ${userId} could not be deleted`, error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function usersAvatarAbsoluteUrl(event, avatar) {
|
|
68
|
+
return absoluteUserAvatarUrl(avatar, usersAvatarOrigin(event));
|
|
69
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { H3Event } from 'h3';
|
|
2
|
+
import type { userTermsAcceptances as TermsTable } from '../../db/terms.sqlite.js';
|
|
3
|
+
import type { UserTermsAcceptanceDto, UserTermsStatusDto, UsersTermsAcceptedContext, UsersTermsRuntimeConfig, UsersTermsSource } from '../../../types.js';
|
|
4
|
+
/** The User-Agent is stored cut to this many characters. */
|
|
5
|
+
export declare const USERS_TERMS_USER_AGENT_MAX_LENGTH = 512;
|
|
6
|
+
/** The terms config (`users.terms`), or `null` when the host does not track terms. */
|
|
7
|
+
export declare function usersTermsConfig(event?: H3Event): UsersTermsRuntimeConfig | null;
|
|
8
|
+
/** A locale of the terms files: `locale` when it has a file, else the first file's (the text the site falls back to). */
|
|
9
|
+
export declare function usersTermsLocale(config: UsersTermsRuntimeConfig, locale: string | null | undefined): string;
|
|
10
|
+
/** A keyed hash (HMAC-SHA-256, hex) of the request's IP address, or `null` without an address or a secret. */
|
|
11
|
+
export declare function usersIpHash(event: H3Event): Promise<string | null>;
|
|
12
|
+
/** The signed-in user's terms: the current version, their latest acceptance and whether it is the current one. */
|
|
13
|
+
export declare function usersTermsStatus(event: H3Event | undefined, userId: number): Promise<UserTermsStatusDto>;
|
|
14
|
+
/**
|
|
15
|
+
* Records that `userId` accepted the current terms in `locale` (one of the files, else the fallback): a history row with
|
|
16
|
+
* the file's hash, the time, a keyed hash of the IP address and the User-Agent (without `event`: none), then the latest
|
|
17
|
+
* on the user. Fires
|
|
18
|
+
* `users:terms:accepted`. The server sets everything but the locale.
|
|
19
|
+
*/
|
|
20
|
+
export declare function usersRecordTermsAcceptance(event: H3Event | undefined, userId: number, input: {
|
|
21
|
+
locale?: string | null;
|
|
22
|
+
source: UsersTermsSource;
|
|
23
|
+
acceptedAt?: number;
|
|
24
|
+
}): Promise<UsersTermsAcceptedContext>;
|
|
25
|
+
/**
|
|
26
|
+
* 403 `terms_outdated` unless `userId` accepted the current terms. For routes that must not go on without them (a
|
|
27
|
+
* purchase). Does nothing when the host does not track terms.
|
|
28
|
+
*/
|
|
29
|
+
export declare function usersRequireAcceptedTerms(event: H3Event, userId: number): Promise<void>;
|
|
30
|
+
/** A user's acceptances, newest first (the admin history). */
|
|
31
|
+
export declare function usersTermsHistory(userId: number): Promise<UserTermsAcceptanceDto[]>;
|
|
32
|
+
/** Removes a deleted user's history (`users:deleted`): the table has no foreign key. */
|
|
33
|
+
export declare function usersDeleteTermsHistory(userId: number): Promise<void>;
|
|
34
|
+
/** One history row as the admin route returns it. */
|
|
35
|
+
export declare function toUserTermsAcceptanceDto(row: typeof TermsTable.$inferSelect): UserTermsAcceptanceDto;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { desc, eq } from "drizzle-orm";
|
|
3
|
+
import { apiError, isoDate, notConfigured } from "@frigear-nu/module-kit/server";
|
|
4
|
+
import { resolveSecret } from "@frigear-nu/module-kit/secret";
|
|
5
|
+
import { bytesToHex } from "@frigear-nu/module-kit/crypto";
|
|
6
|
+
const termsSchema = () => schema;
|
|
7
|
+
export const USERS_TERMS_USER_AGENT_MAX_LENGTH = 512;
|
|
8
|
+
export function usersTermsConfig(event) {
|
|
9
|
+
return useRuntimeConfig(event).users?.terms ?? null;
|
|
10
|
+
}
|
|
11
|
+
function publicTerms(config) {
|
|
12
|
+
return { version: config.version, url: config.url, ...config.privacyUrl ? { privacyUrl: config.privacyUrl } : {} };
|
|
13
|
+
}
|
|
14
|
+
export function usersTermsLocale(config, locale) {
|
|
15
|
+
if (locale && Object.hasOwn(config.hashes, locale)) return locale;
|
|
16
|
+
const base = locale?.split("-")[0];
|
|
17
|
+
if (base && Object.hasOwn(config.hashes, base)) return base;
|
|
18
|
+
return Object.keys(config.hashes)[0];
|
|
19
|
+
}
|
|
20
|
+
let ipKey;
|
|
21
|
+
export async function usersIpHash(event) {
|
|
22
|
+
const ip = getRequestIP(event, { xForwardedFor: true });
|
|
23
|
+
if (!ip) return null;
|
|
24
|
+
const secret = await resolveSecret("@frigear-nu/users:terms-ip");
|
|
25
|
+
if (!secret) return null;
|
|
26
|
+
if (ipKey?.secret !== secret) {
|
|
27
|
+
ipKey = { secret, key: crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]) };
|
|
28
|
+
}
|
|
29
|
+
const signature = await crypto.subtle.sign("HMAC", await ipKey.key, new TextEncoder().encode(`users:terms:ip:${ip}`));
|
|
30
|
+
return bytesToHex(signature);
|
|
31
|
+
}
|
|
32
|
+
export async function usersTermsStatus(event, userId) {
|
|
33
|
+
const config = usersTermsConfig(event);
|
|
34
|
+
if (!config) throw notConfigured("Terms are not tracked here (`users.terms`).");
|
|
35
|
+
const { users } = termsSchema();
|
|
36
|
+
const [row] = await db.select({ acceptedAt: users.acceptedTermsAt, version: users.acceptedTermsVersion }).from(users).where(eq(users.id, userId)).limit(1);
|
|
37
|
+
const accepted = row?.version && row.acceptedAt ? { version: row.version, acceptedAt: isoDate(row.acceptedAt) } : null;
|
|
38
|
+
return { current: publicTerms(config), accepted, upToDate: accepted?.version === config.version };
|
|
39
|
+
}
|
|
40
|
+
export async function usersRecordTermsAcceptance(event, userId, input) {
|
|
41
|
+
const config = usersTermsConfig(event);
|
|
42
|
+
if (!config) throw notConfigured("Terms are not tracked here (`users.terms`).");
|
|
43
|
+
const locale = usersTermsLocale(config, input.locale);
|
|
44
|
+
const acceptedAt = input.acceptedAt ?? Date.now();
|
|
45
|
+
const userAgent = event ? getRequestHeader(event, "user-agent")?.slice(0, USERS_TERMS_USER_AGENT_MAX_LENGTH) || null : null;
|
|
46
|
+
const ipHash = event ? await usersIpHash(event) : null;
|
|
47
|
+
const { users, userTermsAcceptances } = termsSchema();
|
|
48
|
+
await db.insert(userTermsAcceptances).values({
|
|
49
|
+
userId,
|
|
50
|
+
version: config.version,
|
|
51
|
+
locale,
|
|
52
|
+
contentHash: config.hashes[locale],
|
|
53
|
+
acceptedAt,
|
|
54
|
+
source: input.source,
|
|
55
|
+
ipHash,
|
|
56
|
+
userAgent
|
|
57
|
+
});
|
|
58
|
+
await db.update(users).set({ acceptedTermsAt: acceptedAt, acceptedTermsVersion: config.version }).where(eq(users.id, userId));
|
|
59
|
+
const context = { userId, version: config.version, acceptedAt, source: input.source };
|
|
60
|
+
await useNitroApp().hooks.callHook("users:terms:accepted", context);
|
|
61
|
+
return context;
|
|
62
|
+
}
|
|
63
|
+
export async function usersRequireAcceptedTerms(event, userId) {
|
|
64
|
+
if (!usersTermsConfig(event)) return;
|
|
65
|
+
const status = await usersTermsStatus(event, userId);
|
|
66
|
+
if (!status.upToDate) {
|
|
67
|
+
throw apiError(403, "terms_outdated", "Accept the current terms first.", { version: status.current.version, url: status.current.url });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export async function usersTermsHistory(userId) {
|
|
71
|
+
const { userTermsAcceptances: table } = termsSchema();
|
|
72
|
+
const rows = await db.select().from(table).where(eq(table.userId, userId)).orderBy(desc(table.acceptedAt), desc(table.id));
|
|
73
|
+
return rows.map(toUserTermsAcceptanceDto);
|
|
74
|
+
}
|
|
75
|
+
export async function usersDeleteTermsHistory(userId) {
|
|
76
|
+
const { userTermsAcceptances: table } = termsSchema();
|
|
77
|
+
await db.delete(table).where(eq(table.userId, userId));
|
|
78
|
+
}
|
|
79
|
+
export function toUserTermsAcceptanceDto(row) {
|
|
80
|
+
return {
|
|
81
|
+
id: row.id,
|
|
82
|
+
version: row.version,
|
|
83
|
+
locale: row.locale,
|
|
84
|
+
contentHash: row.contentHash,
|
|
85
|
+
acceptedAt: isoDate(row.acceptedAt),
|
|
86
|
+
source: row.source,
|
|
87
|
+
ipHash: row.ipHash ?? null,
|
|
88
|
+
userAgent: row.userAgent ?? null
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption } from '../../types.js';
|
|
2
|
-
export type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption };
|
|
1
|
+
import type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption, UserTermsAcceptanceDto, UserTermsStatusDto, UsersPublicTerms } from '../../types.js';
|
|
2
|
+
export type { AdminUserDto, ProfileDto, UserPickerCreateInitial, UserPickerOption, UserTermsAcceptanceDto, UserTermsStatusDto, UsersPublicTerms };
|
|
3
3
|
export { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from './phone.js';
|
|
4
|
+
export { USER_MEDIA_OPT_OUTS, USER_NICKNAME_MAX_LENGTH, USER_PEOPLE_NUMBER_MAX_LENGTH, normalizeUserPeopleNumber, userDisplayName } from './profile.js';
|
|
5
|
+
export type { UserMediaOptOut } from './profile.js';
|
|
6
|
+
export { USERS_AVATAR_FILE_PATTERN, USERS_AVATAR_TYPES, USERS_MAX_AVATAR_BYTES, USERS_MAX_AVATAR_DIMENSION, absoluteUserAvatarUrl, isUploadedUserAvatar, parseUploadedUserAvatar } from './avatar.js';
|
|
4
7
|
/** The users API: self-service under `/api/users`, staff under `/api/admin/users`. */
|
|
5
8
|
export declare const usersApi: {
|
|
6
9
|
readonly me: {
|
|
7
|
-
/** `PATCH { name?, avatar?, phone?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
10
|
+
/** `PATCH { name?, nickname?, avatar?, phone?, peopleNumber?, photoOptOut?, tagOptOut?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
8
11
|
readonly profile: "/api/users/me";
|
|
9
12
|
/**
|
|
10
13
|
* `PUT { locale, ifUnset? }`: sets the caller's language, answers the profile DTO. With `ifUnset: true` it only fills
|
|
@@ -15,7 +18,21 @@ export declare const usersApi: {
|
|
|
15
18
|
readonly email: "/api/users/me/email";
|
|
16
19
|
/** `PUT { currentPassword, newPassword, confirmNewPassword }`: changes the caller's password. Session only. */
|
|
17
20
|
readonly password: "/api/users/me/password";
|
|
21
|
+
/**
|
|
22
|
+
* `GET`: the caller's terms (`UserTermsStatusDto`). `POST { version, locale? }`: accepts the current terms, answers
|
|
23
|
+
* the status (409 `outdated_version` for another version). Session only, with `users.terms`.
|
|
24
|
+
*/
|
|
25
|
+
readonly terms: "/api/users/me/terms";
|
|
26
|
+
/**
|
|
27
|
+
* `PUT` multipart/form-data with the image in field `file` (PNG, JPEG or WebP, at most `users.maxAvatarBytes`):
|
|
28
|
+
* stores it as the caller's avatar and answers the profile DTO, whose `avatar` is the image's absolute URL
|
|
29
|
+
* (`usersApi.avatar`). 413 `file_too_large`, 415 `unsupported_media_type`, 400 without a file. `DELETE` removes the
|
|
30
|
+
* caller's avatar (204). Both `profile:write`; only with `users.avatarUpload`.
|
|
31
|
+
*/
|
|
32
|
+
readonly avatar: "/api/users/me/avatar";
|
|
18
33
|
};
|
|
34
|
+
/** `GET`: an uploaded avatar (public, cached for good: every upload gets a new file name). Only with `users.avatarUpload`. */
|
|
35
|
+
readonly avatar: (userId: number | string, file: string) => string;
|
|
19
36
|
/** `GET ?code=`: read-only preview of an email change link (`verifyEmailChange`). */
|
|
20
37
|
readonly emailChange: "/api/users/email-change";
|
|
21
38
|
/** `POST { code }`: confirms an email change; the signed-in owner only. */
|
|
@@ -23,12 +40,16 @@ export declare const usersApi: {
|
|
|
23
40
|
/** `GET ?code=` (preview) and `POST { code }` (restore the old address). The code is the proof, no sign-in needed. */
|
|
24
41
|
readonly emailChangeRevert: "/api/users/email-change/revert";
|
|
25
42
|
readonly admin: {
|
|
26
|
-
/** `GET` (paginated list, `users:list`; `useTableQuery` keys plus `role`, `verified`) and `POST` (create, `users:create`, 201). */
|
|
43
|
+
/** `GET` (paginated list, `users:list`; `useTableQuery` keys plus `role`, `verified`, `mediaOptOut=photo|tag`) and `POST` (create, `users:create`, 201). */
|
|
27
44
|
readonly users: "/api/admin/users";
|
|
28
45
|
/** `GET` (detail), `PATCH` (edit) and `DELETE` (204). */
|
|
29
46
|
readonly user: (id: number | string) => string;
|
|
30
47
|
/** `POST`: emails the sign-in link (or the verification email) again. */
|
|
31
48
|
readonly resend: (id: number | string) => string;
|
|
49
|
+
/** `GET`: the user's terms acceptances, newest first (`UserTermsAcceptanceDto[]`, `users:pii`, with `users.terms`). */
|
|
50
|
+
readonly terms: (id: number | string) => string;
|
|
51
|
+
/** `DELETE`: removes the user's avatar, uploaded or from a sign-in provider (204). `users:update`. */
|
|
52
|
+
readonly avatar: (id: number | string) => string;
|
|
32
53
|
/** `GET`: the newest sign-ups of the last 30 days (dashboard). */
|
|
33
54
|
readonly new: "/api/admin/users/new";
|
|
34
55
|
/** `GET`: `{ total, newUsers, days }` (dashboard). */
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from "./phone.js";
|
|
2
|
+
export { USER_MEDIA_OPT_OUTS, USER_NICKNAME_MAX_LENGTH, USER_PEOPLE_NUMBER_MAX_LENGTH, normalizeUserPeopleNumber, userDisplayName } from "./profile.js";
|
|
3
|
+
export { USERS_AVATAR_FILE_PATTERN, USERS_AVATAR_TYPES, USERS_MAX_AVATAR_BYTES, USERS_MAX_AVATAR_DIMENSION, absoluteUserAvatarUrl, isUploadedUserAvatar, parseUploadedUserAvatar } from "./avatar.js";
|
|
2
4
|
const base = "/api/users";
|
|
3
5
|
const adminBase = "/api/admin/users";
|
|
4
6
|
export const usersApi = {
|
|
5
7
|
me: {
|
|
6
|
-
/** `PATCH { name?, avatar?, phone?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
8
|
+
/** `PATCH { name?, nickname?, avatar?, phone?, peopleNumber?, photoOptOut?, tagOptOut?, locale? }`: updates the caller's profile, answers the profile DTO. `profile:write` (implicit). */
|
|
7
9
|
profile: `${base}/me`,
|
|
8
10
|
/**
|
|
9
11
|
* `PUT { locale, ifUnset? }`: sets the caller's language, answers the profile DTO. With `ifUnset: true` it only fills
|
|
@@ -13,8 +15,22 @@ export const usersApi = {
|
|
|
13
15
|
/** `PUT { email }`: changes the caller's email (or emails a confirmation link with `verifyEmailChange`). Session only. */
|
|
14
16
|
email: `${base}/me/email`,
|
|
15
17
|
/** `PUT { currentPassword, newPassword, confirmNewPassword }`: changes the caller's password. Session only. */
|
|
16
|
-
password: `${base}/me/password
|
|
18
|
+
password: `${base}/me/password`,
|
|
19
|
+
/**
|
|
20
|
+
* `GET`: the caller's terms (`UserTermsStatusDto`). `POST { version, locale? }`: accepts the current terms, answers
|
|
21
|
+
* the status (409 `outdated_version` for another version). Session only, with `users.terms`.
|
|
22
|
+
*/
|
|
23
|
+
terms: `${base}/me/terms`,
|
|
24
|
+
/**
|
|
25
|
+
* `PUT` multipart/form-data with the image in field `file` (PNG, JPEG or WebP, at most `users.maxAvatarBytes`):
|
|
26
|
+
* stores it as the caller's avatar and answers the profile DTO, whose `avatar` is the image's absolute URL
|
|
27
|
+
* (`usersApi.avatar`). 413 `file_too_large`, 415 `unsupported_media_type`, 400 without a file. `DELETE` removes the
|
|
28
|
+
* caller's avatar (204). Both `profile:write`; only with `users.avatarUpload`.
|
|
29
|
+
*/
|
|
30
|
+
avatar: `${base}/me/avatar`
|
|
17
31
|
},
|
|
32
|
+
/** `GET`: an uploaded avatar (public, cached for good: every upload gets a new file name). Only with `users.avatarUpload`. */
|
|
33
|
+
avatar: (userId, file) => `${base}/avatars/${encodeURIComponent(String(userId))}/${encodeURIComponent(file)}`,
|
|
18
34
|
/** `GET ?code=`: read-only preview of an email change link (`verifyEmailChange`). */
|
|
19
35
|
emailChange: `${base}/email-change`,
|
|
20
36
|
/** `POST { code }`: confirms an email change; the signed-in owner only. */
|
|
@@ -22,12 +38,16 @@ export const usersApi = {
|
|
|
22
38
|
/** `GET ?code=` (preview) and `POST { code }` (restore the old address). The code is the proof, no sign-in needed. */
|
|
23
39
|
emailChangeRevert: `${base}/email-change/revert`,
|
|
24
40
|
admin: {
|
|
25
|
-
/** `GET` (paginated list, `users:list`; `useTableQuery` keys plus `role`, `verified`) and `POST` (create, `users:create`, 201). */
|
|
41
|
+
/** `GET` (paginated list, `users:list`; `useTableQuery` keys plus `role`, `verified`, `mediaOptOut=photo|tag`) and `POST` (create, `users:create`, 201). */
|
|
26
42
|
users: adminBase,
|
|
27
43
|
/** `GET` (detail), `PATCH` (edit) and `DELETE` (204). */
|
|
28
44
|
user: (id) => `${adminBase}/${encodeURIComponent(String(id))}`,
|
|
29
45
|
/** `POST`: emails the sign-in link (or the verification email) again. */
|
|
30
46
|
resend: (id) => `${adminBase}/${encodeURIComponent(String(id))}/resend`,
|
|
47
|
+
/** `GET`: the user's terms acceptances, newest first (`UserTermsAcceptanceDto[]`, `users:pii`, with `users.terms`). */
|
|
48
|
+
terms: (id) => `${adminBase}/${encodeURIComponent(String(id))}/terms`,
|
|
49
|
+
/** `DELETE`: removes the user's avatar, uploaded or from a sign-in provider (204). `users:update`. */
|
|
50
|
+
avatar: (id) => `${adminBase}/${encodeURIComponent(String(id))}/avatar`,
|
|
31
51
|
/** `GET`: the newest sign-ups of the last 30 days (dashboard). */
|
|
32
52
|
new: `${adminBase}/new`,
|
|
33
53
|
/** `GET`: `{ total, newUsers, days }` (dashboard). */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** The image types an uploaded avatar may have (read from its bytes, never from the name or the browser's type). */
|
|
2
|
+
export declare const USERS_AVATAR_TYPES: readonly ["image/png", "image/jpeg", "image/webp"];
|
|
3
|
+
/** The default largest avatar upload: 2 MB (`users.maxAvatarBytes`). */
|
|
4
|
+
export declare const USERS_MAX_AVATAR_BYTES: number;
|
|
5
|
+
/** The widest and tallest avatar upload, in pixels. The account page sends 512 by 512. */
|
|
6
|
+
export declare const USERS_MAX_AVATAR_DIMENSION = 4096;
|
|
7
|
+
/** The file name of a stored avatar: `avatar-<random suffix>.<png|jpg|webp>`, new for every upload. */
|
|
8
|
+
export declare const USERS_AVATAR_FILE_PATTERN: RegExp;
|
|
9
|
+
/**
|
|
10
|
+
* The user id and file name of an avatar uploaded here, or `null` for anything else: a URL from a sign-in provider, a
|
|
11
|
+
* pasted URL, nothing. `users.avatar` holds an uploaded avatar as an absolute URL of the route that serves it
|
|
12
|
+
* (`https://<site>/api/users/avatars/<userId>/<file>`); the bare path is recognised too.
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseUploadedUserAvatar(value: unknown): {
|
|
15
|
+
userId: number;
|
|
16
|
+
file: string;
|
|
17
|
+
} | null;
|
|
18
|
+
/** Whether `value` is an avatar uploaded here (the URL or path of the avatar route), not a picture elsewhere. */
|
|
19
|
+
export declare function isUploadedUserAvatar(value: unknown): value is string;
|
|
20
|
+
/**
|
|
21
|
+
* The avatar as an absolute URL, for anything that leaves the app (OIDC `picture`, exports, other apps). Uploads are
|
|
22
|
+
* stored absolute already; an uploaded avatar stored as a bare path is resolved against `origin` (the app's public
|
|
23
|
+
* origin). Any other avatar is returned as it is; `null` stays `null`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function absoluteUserAvatarUrl(avatar: string | null | undefined, origin: string | null | undefined): string | null;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { IMAGE_TYPES } from "@frigear-nu/module-kit/image";
|
|
2
|
+
export const USERS_AVATAR_TYPES = IMAGE_TYPES;
|
|
3
|
+
export const USERS_MAX_AVATAR_BYTES = 2 * 1024 * 1024;
|
|
4
|
+
export const USERS_MAX_AVATAR_DIMENSION = 4096;
|
|
5
|
+
export const USERS_AVATAR_FILE_PATTERN = /^avatar-[0-9a-f]{4,64}\.(?:png|jpg|webp)$/;
|
|
6
|
+
const UPLOADED_AVATAR = /^(?:https?:\/\/[a-z0-9.-]+(?::\d{1,5})?)?\/api\/users\/avatars\/(\d{1,15})\/(avatar-[0-9a-f]{4,64}\.(?:png|jpg|webp))$/i;
|
|
7
|
+
export function parseUploadedUserAvatar(value) {
|
|
8
|
+
if (typeof value !== "string") return null;
|
|
9
|
+
const match = UPLOADED_AVATAR.exec(value);
|
|
10
|
+
return match ? { userId: Number(match[1]), file: match[2] } : null;
|
|
11
|
+
}
|
|
12
|
+
export function isUploadedUserAvatar(value) {
|
|
13
|
+
return parseUploadedUserAvatar(value) !== null;
|
|
14
|
+
}
|
|
15
|
+
export function absoluteUserAvatarUrl(avatar, origin) {
|
|
16
|
+
if (!avatar) return null;
|
|
17
|
+
if (!origin || !isUploadedUserAvatar(avatar)) return avatar;
|
|
18
|
+
try {
|
|
19
|
+
return new URL(avatar, origin).href;
|
|
20
|
+
} catch {
|
|
21
|
+
return avatar;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The longest nickname stored on a user. */
|
|
2
|
+
export declare const USER_NICKNAME_MAX_LENGTH = 50;
|
|
3
|
+
/** The longest people number (a person's id in an external system) stored on a user. */
|
|
4
|
+
export declare const USER_PEOPLE_NUMBER_MAX_LENGTH = 64;
|
|
5
|
+
/** The media opt-outs a user can choose, as the admin list filters them (`?mediaOptOut=`). */
|
|
6
|
+
export declare const USER_MEDIA_OPT_OUTS: readonly ["photo", "tag"];
|
|
7
|
+
export type UserMediaOptOut = typeof USER_MEDIA_OPT_OUTS[number];
|
|
8
|
+
/** A people number as stored: trimmed, or `null` when empty. */
|
|
9
|
+
export declare const normalizeUserPeopleNumber: (value: string | null | undefined) => string | null;
|
|
10
|
+
/** How staff lists show a person: "Name (nickname)", or the name alone. */
|
|
11
|
+
export declare const userDisplayName: (user: {
|
|
12
|
+
name: string | null | undefined;
|
|
13
|
+
nickname?: string | null;
|
|
14
|
+
}) => string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const USER_NICKNAME_MAX_LENGTH = 50;
|
|
2
|
+
export const USER_PEOPLE_NUMBER_MAX_LENGTH = 64;
|
|
3
|
+
export const USER_MEDIA_OPT_OUTS = ["photo", "tag"];
|
|
4
|
+
export const normalizeUserPeopleNumber = (value) => value?.trim() || null;
|
|
5
|
+
export const userDisplayName = (user) => {
|
|
6
|
+
const name = user.name ?? "";
|
|
7
|
+
return user.nickname && user.nickname !== name ? `${name} (${user.nickname})` : name;
|
|
8
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** The `terms` claim. `accepted_at` is in epoch seconds, like `auth_time`. */
|
|
2
|
+
export type UsersTermsClaim = {
|
|
3
|
+
version: string;
|
|
4
|
+
locale: string;
|
|
5
|
+
/** SHA-256 (hex) of the terms file the user accepted. */
|
|
6
|
+
content_hash: string;
|
|
7
|
+
accepted_at: number;
|
|
8
|
+
};
|
|
9
|
+
/** The claim of a history row (the user's latest acceptance). */
|
|
10
|
+
export declare function toUsersTermsClaim(row: {
|
|
11
|
+
version: string;
|
|
12
|
+
locale: string;
|
|
13
|
+
contentHash: string;
|
|
14
|
+
acceptedAt: number | Date;
|
|
15
|
+
}): UsersTermsClaim;
|
|
16
|
+
/**
|
|
17
|
+
* The acceptance a `terms` claim proves here: when it is for this app's current version and its hash is the hash of one
|
|
18
|
+
* of this app's terms files (the text is the same, byte for byte). The locale is the matching file's; `acceptedAt` in
|
|
19
|
+
* epoch milliseconds. `null` for anything else (other terms, an older version, a malformed claim).
|
|
20
|
+
*/
|
|
21
|
+
export declare function matchUsersTermsClaim(value: unknown, config: {
|
|
22
|
+
version: string;
|
|
23
|
+
hashes: Record<string, string>;
|
|
24
|
+
}, now?: number): {
|
|
25
|
+
locale: string;
|
|
26
|
+
acceptedAt: number;
|
|
27
|
+
} | null;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function toUsersTermsClaim(row) {
|
|
2
|
+
const acceptedAt = row.acceptedAt instanceof Date ? row.acceptedAt.getTime() : row.acceptedAt;
|
|
3
|
+
return { version: row.version, locale: row.locale, content_hash: row.contentHash, accepted_at: Math.floor(acceptedAt / 1e3) };
|
|
4
|
+
}
|
|
5
|
+
export function matchUsersTermsClaim(value, config, now = Date.now()) {
|
|
6
|
+
if (!value || typeof value !== "object") return null;
|
|
7
|
+
const claim = value;
|
|
8
|
+
if (claim.version !== config.version || typeof claim.content_hash !== "string") return null;
|
|
9
|
+
if (typeof claim.accepted_at !== "number" || !Number.isFinite(claim.accepted_at) || claim.accepted_at <= 0) return null;
|
|
10
|
+
const locales = Object.keys(config.hashes);
|
|
11
|
+
const claimed = typeof claim.locale === "string" && config.hashes[claim.locale] === claim.content_hash ? claim.locale : void 0;
|
|
12
|
+
const locale = claimed ?? locales.find((code) => config.hashes[code] === claim.content_hash);
|
|
13
|
+
if (!locale) return null;
|
|
14
|
+
return { locale, acceptedAt: Math.min(claim.accepted_at * 1e3, now) };
|
|
15
|
+
}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default } from './module.mjs'
|
|
2
2
|
|
|
3
|
-
export { type AdminUserDto, type ModuleOptions, type ProfileDto, type PublicModuleOptions } from './module.mjs'
|
|
3
|
+
export { type AdminUserDto, type ModuleOptions, type ProfileDto, type PublicModuleOptions, type UserDeletedContext, type UserTermsAcceptanceDto, type UserTermsStatusDto, type UsersPublicTerms, type UsersTermsAcceptedContext, type UsersTermsSource } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigear-nu/users",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.6",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"frigear-module",
|
|
7
|
+
"nuxt-module"
|
|
8
|
+
],
|
|
5
9
|
"homepage": "https://docs.frigear.dev/modules/identity/users",
|
|
6
10
|
"repository": {
|
|
7
11
|
"type": "git",
|
|
@@ -34,23 +38,24 @@
|
|
|
34
38
|
"playground"
|
|
35
39
|
],
|
|
36
40
|
"dependencies": {
|
|
37
|
-
"@frigear-nu/auth": "^0.6.
|
|
38
|
-
"@frigear-nu/mcp": "^0.5.
|
|
39
|
-
"@frigear-nu/module-kit": "^1.3.
|
|
40
|
-
"@frigear-nu/notify": "^0.2
|
|
41
|
-
"@frigear-nu/ui": "^0.2.
|
|
41
|
+
"@frigear-nu/auth": "^0.6.9",
|
|
42
|
+
"@frigear-nu/mcp": "^0.5.3",
|
|
43
|
+
"@frigear-nu/module-kit": "^1.3.13",
|
|
44
|
+
"@frigear-nu/notify": "^0.3.2",
|
|
45
|
+
"@frigear-nu/ui": "^0.2.12",
|
|
42
46
|
"@libsql/client": "^0.18.0",
|
|
43
47
|
"@nuxt/kit": "^4.6.4",
|
|
44
48
|
"drizzle-kit": "^0.31.11",
|
|
45
49
|
"drizzle-orm": "^0.45.4",
|
|
50
|
+
"h3": "^1.15.11",
|
|
46
51
|
"scule": "^1.3.0",
|
|
47
52
|
"zod": "^4.6.7"
|
|
48
53
|
},
|
|
49
54
|
"peerDependencies": {
|
|
50
55
|
"@frigear-nu/admin": ">=0.0.0",
|
|
51
|
-
"@frigear-nu/email-templates": "^0.1.
|
|
52
|
-
"@frigear-nu/menu": "^0.4.
|
|
53
|
-
"@frigear-nu/widget": "^0.4.
|
|
56
|
+
"@frigear-nu/email-templates": "^0.1.6",
|
|
57
|
+
"@frigear-nu/menu": "^0.4.2",
|
|
58
|
+
"@frigear-nu/widget": "^0.4.4",
|
|
54
59
|
"@nuxt/ui": "^4.12.0",
|
|
55
60
|
"@nuxthub/core": "^0.10.12"
|
|
56
61
|
},
|
|
@@ -73,10 +78,10 @@
|
|
|
73
78
|
},
|
|
74
79
|
"devDependencies": {
|
|
75
80
|
"@electric-sql/pglite": "^0.5.16",
|
|
76
|
-
"@frigear-nu/admin": "^0.5.
|
|
77
|
-
"@frigear-nu/email-templates": "^0.1.
|
|
78
|
-
"@frigear-nu/menu": "^0.4.
|
|
79
|
-
"@frigear-nu/widget": "^0.4.
|
|
81
|
+
"@frigear-nu/admin": "^0.5.3",
|
|
82
|
+
"@frigear-nu/email-templates": "^0.1.6",
|
|
83
|
+
"@frigear-nu/menu": "^0.4.2",
|
|
84
|
+
"@frigear-nu/widget": "^0.4.4",
|
|
80
85
|
"@nuxt/devtools": "^4.2.0",
|
|
81
86
|
"@nuxt/eslint-config": "^1.17.0",
|
|
82
87
|
"@nuxt/module-builder": "^1.0.4",
|