@frigear-nu/users 3.1.2 → 3.1.3

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.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@frigear-nu/users",
3
3
  "configKey": "users",
4
- "version": "3.1.2",
4
+ "version": "3.1.3",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.4",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, createResolver, addComponent, addServerImportsDir, hasNuxtModule, addPlugin } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addComponent, addServerImportsDir, hasNuxtModule, addPlugin, addServerPlugin } from '@nuxt/kit';
2
2
  import { registerTailwindSource, registerLocales, registerDatabaseTable, registerSearchIndex, inlineModuleRuntime, addModulePages, addModuleRoutes, resolvePagePath, addOptionalMenuSource } from '@frigear-nu/module-kit/utils';
3
3
  import { setAuthUserStore, registerAccess } from '@frigear-nu/auth/utils';
4
4
  import { resolveAuthMode } from '@frigear-nu/auth/mode';
@@ -104,8 +104,15 @@ const module$1 = defineNuxtModule({
104
104
  nuxt.options.runtimeConfig.users.locales = locales.map((locale) => typeof locale === "string" ? locale : locale.code);
105
105
  if (hasNuxtModule("@nuxtjs/i18n", nuxt)) {
106
106
  addPlugin({ src: resolver.resolve("./runtime/app/plugins/remember-locale.client"), mode: "client" });
107
+ if (hasNuxtModule("@nuxt/ui", nuxt)) {
108
+ addPlugin({ src: resolver.resolve("./runtime/app/plugins/offer-locale.client"), mode: "client" });
109
+ }
110
+ }
111
+ if (hasNuxtModule("@frigear-nu/oauth-server", nuxt)) {
112
+ addServerPlugin(resolver.resolve("./runtime/server/plugins/oauth-claims"));
107
113
  }
108
114
  });
115
+ addServerPlugin(resolver.resolve("./runtime/server/plugins/frigear-locale"));
109
116
  const withAdmin = !!settings.admin;
110
117
  registerAccess(nuxt, usersAccess, withAdmin ? {} : { permissions: USERS_SELF_SERVICE });
111
118
  const adminOptions = typeof settings.admin === "object" ? settings.admin : {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
+ export default _default;
@@ -0,0 +1,64 @@
1
+ import { userLocaleOfferKey, userLocaleToOffer } from "../../utils/locale.js";
2
+ import { defineNuxtPlugin, useModuleT, useToast, useUserSession, watch } from "#imports";
3
+ export default defineNuxtPlugin({
4
+ name: "users:offer-locale",
5
+ dependsOn: ["i18n:plugin"],
6
+ setup(nuxtApp) {
7
+ const i18n = nuxtApp.$i18n;
8
+ if (!i18n) return;
9
+ nuxtApp.hook("app:mounted", () => {
10
+ const { loggedIn, user } = useUserSession();
11
+ const toast = useToast();
12
+ const t = useModuleT("users");
13
+ const offered = /* @__PURE__ */ new Set();
14
+ const answered = (key) => {
15
+ try {
16
+ return sessionStorage.getItem(key) === "1";
17
+ } catch {
18
+ return false;
19
+ }
20
+ };
21
+ const remember = (key) => {
22
+ try {
23
+ sessionStorage.setItem(key, "1");
24
+ } catch {
25
+ }
26
+ };
27
+ const offer = () => {
28
+ const current = user.value;
29
+ if (!loggedIn.value || !current) return;
30
+ const locales = i18n.locales.value.map((locale2) => typeof locale2 === "string" ? { code: locale2 } : locale2);
31
+ const locale = userLocaleToOffer(current.locale, i18n.locale.value, locales.map((item) => item.code));
32
+ if (!locale) return;
33
+ const key = userLocaleOfferKey(current.id, locale);
34
+ if (offered.has(key) || answered(key)) return;
35
+ offered.add(key);
36
+ const language = locales.find((item) => item.code === locale)?.name ?? locale;
37
+ toast.add({
38
+ "id": "users:offer-locale",
39
+ "title": t("offerLocale.title", "Your language is {language}", { language }),
40
+ "description": t("offerLocale.description", "Your account is set to {language}. Show this site in it?", { language }),
41
+ "icon": "i-lucide-languages",
42
+ "actions": [{
43
+ label: t("offerLocale.switch", "Switch to {language}", { language }),
44
+ color: "primary",
45
+ variant: "solid",
46
+ onClick: () => {
47
+ remember(key);
48
+ void i18n.setLocale(locale);
49
+ }
50
+ }],
51
+ // Closed by hand or timed out: not offered again this session.
52
+ "onUpdate:open": (open) => {
53
+ if (!open) remember(key);
54
+ }
55
+ });
56
+ };
57
+ let timer;
58
+ watch([loggedIn, () => user.value?.locale, () => i18n.locale.value], () => {
59
+ clearTimeout(timer);
60
+ timer = setTimeout(offer, 1e3);
61
+ }, { immediate: true });
62
+ });
63
+ }
64
+ });
@@ -144,6 +144,11 @@
144
144
  "create": "Opret bruger \"{term}\"",
145
145
  "empty": "Ingen matcher",
146
146
  "hint": "Skriv et navn for at søge"
147
+ },
148
+ "offerLocale": {
149
+ "title": "Dit sprog er {language}",
150
+ "description": "Din konto er sat til {language}. Vil du se siden på det sprog?",
151
+ "switch": "Skift til {language}"
147
152
  }
148
153
  }
149
154
  }
@@ -144,6 +144,11 @@
144
144
  "create": "Create user \"{term}\"",
145
145
  "empty": "Nobody matches",
146
146
  "hint": "Type a name to search"
147
+ },
148
+ "offerLocale": {
149
+ "title": "Your language is {language}",
150
+ "description": "Your account is set to {language}. Show this site in it?",
151
+ "switch": "Switch to {language}"
147
152
  }
148
153
  }
149
154
  }
@@ -0,0 +1,12 @@
1
+ /** What this plugin reads and writes of auth's `auth:frigear:login` context (its `FrigearLoginContext`). */
2
+ export interface UsersFrigearLoginContext {
3
+ user: {
4
+ id: number;
5
+ locale?: unknown;
6
+ } & Record<string, unknown>;
7
+ claims: Record<string, unknown>;
8
+ }
9
+ /** Stores the claimed language on the user when it is valid and differs, and hands the updated user back. */
10
+ export declare function storeFrigearLocaleClaim(ctx: UsersFrigearLoginContext): Promise<void>;
11
+ declare const _default: any;
12
+ export default _default;
@@ -0,0 +1,12 @@
1
+ import { db, schema } from "@nuxthub/db";
2
+ import { eq } from "drizzle-orm";
3
+ export async function storeFrigearLocaleClaim(ctx) {
4
+ const locale = parseUserLocale(ctx.claims.locale);
5
+ if (!locale || ctx.user.locale === locale) return;
6
+ await db.update(schema.users).set({ locale, updatedAt: /* @__PURE__ */ new Date() }).where(eq(schema.users.id, Number(ctx.user.id)));
7
+ ctx.user = { ...ctx.user, locale };
8
+ }
9
+ export default defineNitroPlugin((nitro) => {
10
+ const hooks = nitro.hooks;
11
+ hooks.hook("auth:frigear:login", storeFrigearLocaleClaim);
12
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * What this plugin reads and writes of oauth-server's `oauth:claims` context (its `ClaimsContext`). Typed here: the
3
+ * OAuth server depends on this package, so this package cannot depend on it.
4
+ */
5
+ export interface UsersOAuthClaimsContext {
6
+ user: {
7
+ locale?: unknown;
8
+ };
9
+ scope: string[];
10
+ tokenType: string;
11
+ claims: Record<string, unknown>;
12
+ }
13
+ /** Adds the `locale` claim: identity claims only (access tokens stay bare), with the profile scope, never replacing one. */
14
+ export declare function addUserLocaleClaim(ctx: UsersOAuthClaimsContext): void;
15
+ declare const _default: any;
16
+ export default _default;
@@ -0,0 +1,9 @@
1
+ export function addUserLocaleClaim(ctx) {
2
+ if (ctx.tokenType === "access_token" || !ctx.scope.includes("profile")) return;
3
+ const locale = ctx.user.locale;
4
+ if (typeof locale === "string" && locale && !("locale" in ctx.claims)) ctx.claims.locale = locale;
5
+ }
6
+ export default defineNitroPlugin((nitro) => {
7
+ const hooks = nitro.hooks;
8
+ hooks.hook("oauth:claims", addUserLocaleClaim);
9
+ });
@@ -8,3 +8,10 @@ export declare const USER_LOCALE_PATTERN: RegExp;
8
8
  * them, any short BCP-47 tag counts (`USER_LOCALE_PATTERN`), as given.
9
9
  */
10
10
  export declare function normalizeUserLocale(value: unknown, known?: readonly string[]): string | null;
11
+ /**
12
+ * The saved language (`users.locale`) to offer a signed-in user, as the app spells it: one of the app's locales
13
+ * (`known`) other than the one the page is shown in (`current`). `null` when there is nothing to offer.
14
+ */
15
+ export declare function userLocaleToOffer(saved: unknown, current: string | null | undefined, known: readonly string[]): string | null;
16
+ /** The `sessionStorage` key that remembers an answered offer of `locale` to `userId`, so it is not made again. */
17
+ export declare function userLocaleOfferKey(userId: unknown, locale: string): string;
@@ -7,3 +7,11 @@ export function normalizeUserLocale(value, known = []) {
7
7
  if (known.length) return known.find((code) => code.toLowerCase() === tag.toLowerCase()) ?? null;
8
8
  return USER_LOCALE_PATTERN.test(tag) ? tag : null;
9
9
  }
10
+ export function userLocaleToOffer(saved, current, known) {
11
+ if (!known.length || !current) return null;
12
+ const code = normalizeUserLocale(saved, known);
13
+ return code && code.toLowerCase() !== current.toLowerCase() ? code : null;
14
+ }
15
+ export function userLocaleOfferKey(userId, locale) {
16
+ return `users:locale-offer:${String(userId)}:${locale}`;
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigear-nu/users",
3
- "version": "3.1.2",
3
+ "version": "3.1.3",
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.4",
38
- "@frigear-nu/mcp": "^0.5.0",
39
- "@frigear-nu/module-kit": "^1.3.7",
40
- "@frigear-nu/notify": "^0.2.6",
41
- "@frigear-nu/ui": "^0.2.7",
37
+ "@frigear-nu/auth": "^0.6.5",
38
+ "@frigear-nu/mcp": "^0.5.1",
39
+ "@frigear-nu/module-kit": "^1.3.9",
40
+ "@frigear-nu/notify": "^0.2.7",
41
+ "@frigear-nu/ui": "^0.2.9",
42
42
  "@libsql/client": "^0.18.0",
43
43
  "@nuxt/kit": "^4.6.4",
44
44
  "drizzle-kit": "^0.31.11",
@@ -48,9 +48,9 @@
48
48
  },
49
49
  "peerDependencies": {
50
50
  "@frigear-nu/admin": ">=0.0.0",
51
- "@frigear-nu/email-templates": "^0.1.1",
52
- "@frigear-nu/menu": "^0.4.0",
53
- "@frigear-nu/widget": "^0.4.0",
51
+ "@frigear-nu/email-templates": "^0.1.3",
52
+ "@frigear-nu/menu": "^0.4.1",
53
+ "@frigear-nu/widget": "^0.4.2",
54
54
  "@nuxt/ui": "^4.12.0",
55
55
  "@nuxthub/core": "^0.10.12"
56
56
  },
@@ -73,10 +73,10 @@
73
73
  },
74
74
  "devDependencies": {
75
75
  "@electric-sql/pglite": "^0.5.16",
76
- "@frigear-nu/admin": "^0.5.0",
77
- "@frigear-nu/email-templates": "^0.1.1",
78
- "@frigear-nu/menu": "^0.4.0",
79
- "@frigear-nu/widget": "^0.4.0",
76
+ "@frigear-nu/admin": "^0.5.2",
77
+ "@frigear-nu/email-templates": "^0.1.3",
78
+ "@frigear-nu/menu": "^0.4.1",
79
+ "@frigear-nu/widget": "^0.4.2",
80
80
  "@nuxt/devtools": "^4.2.0",
81
81
  "@nuxt/eslint-config": "^1.17.0",
82
82
  "@nuxt/module-builder": "^1.0.4",