@softure-ai/auth 0.2.10 → 0.2.12

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/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
4
4
  production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
5
5
  "Definition of done"). Versions before the first one below are described in their GitHub Releases (`auth@x.y.z`).
6
6
 
7
+ ## 0.2.12
8
+
9
+ - The content of 0.2.11, which was never published: its release stopped at the gates (ESLint ran out of memory in
10
+ `release.yml`) before publishing, and a tag is never moved.
11
+
12
+ ## 0.2.11
13
+
14
+ - `LogoutForm` in `@softure-ai/auth/ui` (#874): the sign-out form as a client component taking the logout action, with
15
+ an optional `beforeSignOut` awaited before the sign-out (e.g. `clearDeviceData` of `@softure-ai/pwa`); its failure
16
+ is logged by kind and the sign-out goes on. `LogoutButton` renders it, with the same props and markup as before.
17
+
7
18
  ## 0.2.10
8
19
 
9
20
  - The GitHub sign-in example and the `clientIdEnv`/`clientSecretEnv` hints name `GH_AUTH_CLIENT_ID` and
package/README.md CHANGED
@@ -328,6 +328,28 @@ ends the session and goes to that path instead of `afterLogout`, e.g. a consent
328
328
  someone else" returning to the same authorization request after the new login. The path is checked
329
329
  like login's `next`: anything but a path on this app falls back to `afterLogout`.
330
330
 
331
+ To clear what the device keeps before the sign-out (an installed app's offline copy), render `LogoutForm` from
332
+ `@softure-ai/auth/ui` in a client component with `beforeSignOut`. It is the same plain form (it works without
333
+ JavaScript); with JavaScript the first submit is held while `beforeSignOut()` runs, then the action runs whatever it
334
+ did. A throw or rejection is logged by its kind only and the sign-out goes on. With `@softure-ai/pwa`:
335
+
336
+ ```tsx
337
+ "use client";
338
+ import { logoutAction } from "@softure-ai/auth/next";
339
+ import { LogoutForm } from "@softure-ai/auth/ui";
340
+ import type { AuthMessages } from "@softure-ai/auth";
341
+ import { clearDeviceData } from "@softure-ai/pwa";
342
+
343
+ async function clearDevice(): Promise<void> {
344
+ const { failed } = await clearDeviceData();
345
+ if (failed.length > 0) console.error(`sign-out: device data left: ${failed.join(", ")}`);
346
+ }
347
+
348
+ export function SignOut({ messages }: { messages: AuthMessages }) {
349
+ return <LogoutForm action={logoutAction} messages={messages} beforeSignOut={clearDevice} />;
350
+ }
351
+ ```
352
+
331
353
  `requireUser({ next: "/account" })` brings the user back after login; `getCurrentUser()` returns
332
354
  the user or `null`. Both read the session once per request. The redirect to login goes through the
333
355
  app's `rewriteRedirect` (options table); a page outside the proxy's guard passes its own search
@@ -817,6 +839,9 @@ plugs `@softure-ai/mailing` in here. With `passwordReset.enqueue`, it runs in th
817
839
  `passwordReset.enqueue(ctx, userId)`: where a reset request is answered, for an existing account, instead of the
818
840
  sender. `enqueuePasswordReset` from `@softure-ai/auth/jobs` plugs `@softure-ai/jobs` in here.
819
841
 
842
+ `beforeSignOut()` on `LogoutForm` (`/ui`): awaited in the browser before the logout action, e.g. `clearDeviceData` of
843
+ `@softure-ai/pwa` (section 4). Its failure is logged by kind and never stops the sign-out.
844
+
820
845
  ## 11. GDPR
821
846
 
822
847
  The module stores an email, a display name from an OpenID Connect provider, a password hash, session rows, role rows
package/dist/index.js CHANGED
@@ -40,7 +40,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
40
40
  export const auth = defineModule({
41
41
  manifest: {
42
42
  id: MODULE_ID,
43
- version: "0.2.10",
43
+ version: "0.2.12",
44
44
  dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
45
45
  dbSchema: "auth",
46
46
  tables: ["users", "sessions", "user_roles", "password_resets", "identities", "oidc_states"],
@@ -1,4 +1,4 @@
1
- import { type ButtonVariant } from "@softure-ai/ui";
1
+ import type { ButtonVariant } from "@softure-ai/ui";
2
2
  export interface LogoutButtonProps {
3
3
  readonly variant?: ButtonVariant;
4
4
  readonly className?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"logout-button.d.ts","sourceRoot":"","sources":["../../src/next/logout-button.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAI5D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,kGAAkG;IAClG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,YAAY,CAAC,EAAE,OAAqB,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,iBAAiB,+BAUzF"}
1
+ {"version":3,"file":"logout-button.d.ts","sourceRoot":"","sources":["../../src/next/logout-button.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAKpD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,kGAAkG;IAClG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,YAAY,CAAC,EAAE,OAAqB,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,iBAAiB,+BAEzF"}
@@ -1,12 +1,12 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- // A logout button for the app's own pages: a plain form posting to `logoutAction`, so it works
3
- // without JavaScript. Server component. `next` rides along as a hidden field.
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ // A logout button for the app's own pages: `LogoutForm` posting to `logoutAction`, so it works without JavaScript.
3
+ // Server component. `next` rides along as a hidden field. To clear the device before the sign-out, render
4
+ // `LogoutForm` of `@softure-ai/auth/ui` with `beforeSignOut` from a client component instead.
4
5
  import { getSoftureConfig } from "@softure-ai/core/next";
5
- import { Button } from "@softure-ai/ui";
6
+ import { LogoutForm } from "../ui/logout-form.js";
6
7
  import { logoutAction } from "./actions.js";
7
8
  import { getAuthMessages } from "./messages.js";
8
9
  export function LogoutButton({ variant = "secondary", className, next }) {
9
- const messages = getAuthMessages(getSoftureConfig());
10
- return (_jsxs("form", { action: logoutAction, className: className, children: [next === undefined ? null : _jsx("input", { type: "hidden", name: "next", value: next }), _jsx(Button, { type: "submit", variant: variant, children: messages.logout.submit })] }));
10
+ return _jsx(LogoutForm, { action: logoutAction, messages: getAuthMessages(getSoftureConfig()), next: next, variant: variant, className: className });
11
11
  }
12
12
  //# sourceMappingURL=logout-button.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"logout-button.js","sourceRoot":"","sources":["../../src/next/logout-button.tsx"],"names":[],"mappings":";AAAA,+FAA+F;AAC/F,8EAA8E;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAShD,MAAM,UAAU,YAAY,CAAC,EAAE,OAAO,GAAG,WAAW,EAAE,SAAS,EAAE,IAAI,EAAqB;IACxF,MAAM,QAAQ,GAAG,eAAe,CAAC,gBAAgB,EAAE,CAAC,CAAC;IACrD,OAAO,CACL,gBAAM,MAAM,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,aAC7C,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,gBAAO,IAAI,EAAC,QAAQ,EAAC,IAAI,EAAC,MAAM,EAAC,KAAK,EAAE,IAAI,GAAI,EAC7E,KAAC,MAAM,IAAC,IAAI,EAAC,QAAQ,EAAC,OAAO,EAAE,OAAO,YACnC,QAAQ,CAAC,MAAM,CAAC,MAAM,GAChB,IACJ,CACR,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"logout-button.js","sourceRoot":"","sources":["../../src/next/logout-button.tsx"],"names":[],"mappings":";AAAA,mHAAmH;AACnH,0GAA0G;AAC1G,8FAA8F;AAC9F,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAShD,MAAM,UAAU,YAAY,CAAC,EAAE,OAAO,GAAG,WAAW,EAAE,SAAS,EAAE,IAAI,EAAqB;IACxF,OAAO,KAAC,UAAU,IAAC,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,eAAe,CAAC,gBAAgB,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,GAAI,CAAC;AACjJ,CAAC"}
@@ -1,3 +1,4 @@
1
1
  export { type AuthFormAction, type AuthFormSlot, ChangePasswordForm, type ChangePasswordFormProps, type ConsentProps, ForgotPasswordForm, type ForgotPasswordFormProps, LoginForm, type LoginFormProps, RegisterForm, type RegisterFormProps, ResetPasswordForm, type ResetPasswordFormProps, } from "./auth-forms.js";
2
+ export { LogoutForm, type LogoutFormProps } from "./logout-form.js";
2
3
  export { OidcSignIn, type OidcSignInProps, type OidcSignInProvider, type OidcSignInSlot } from "./oidc-sign-in.js";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,uBAAuB,EAC5B,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,uBAAuB,EAC5B,SAAS,EACT,KAAK,cAAc,EACnB,YAAY,EACZ,KAAK,iBAAiB,EACtB,iBAAiB,EACjB,KAAK,sBAAsB,GAC5B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,KAAK,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,uBAAuB,EAC5B,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,uBAAuB,EAC5B,SAAS,EACT,KAAK,cAAc,EACnB,YAAY,EACZ,KAAK,iBAAiB,EACtB,iBAAiB,EACjB,KAAK,sBAAsB,GAC5B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,KAAK,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/ui/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // The auth forms, to compose into the app's own pages. They take the server actions from
2
2
  // `@softure-ai/auth/next` as props: no `next/*` import here (docs/02-module-standard.md §5).
3
3
  export { ChangePasswordForm, ForgotPasswordForm, LoginForm, RegisterForm, ResetPasswordForm, } from "./auth-forms.js";
4
+ export { LogoutForm } from "./logout-form.js";
4
5
  export { OidcSignIn } from "./oidc-sign-in.js";
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,6FAA6F;AAC7F,OAAO,EAGL,kBAAkB,EAGlB,kBAAkB,EAElB,SAAS,EAET,YAAY,EAEZ,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAsE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,6FAA6F;AAC7F,OAAO,EAGL,kBAAkB,EAGlB,kBAAkB,EAElB,SAAS,EAET,YAAY,EAEZ,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAwB,MAAM,kBAAkB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAsE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type ButtonVariant } from "@softure-ai/ui";
2
+ import type { AuthMessages } from "../messages/index.js";
3
+ export interface LogoutFormProps {
4
+ readonly action: (formData: FormData) => void | Promise<void>;
5
+ readonly messages: AuthMessages;
6
+ /** Where to go after logout (a path on this app), e.g. `/login?next=…`; checked again on the server. */
7
+ readonly next?: string;
8
+ /** Awaited before the sign-out. A throw or rejection is logged by its kind only, and the sign-out goes on. */
9
+ readonly beforeSignOut?: () => unknown;
10
+ readonly variant?: ButtonVariant;
11
+ readonly className?: string;
12
+ }
13
+ export declare function LogoutForm({ action, messages, next, beforeSignOut, variant, className }: LogoutFormProps): import("react").JSX.Element;
14
+ //# sourceMappingURL=logout-form.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logout-form.d.ts","sourceRoot":"","sources":["../../src/ui/logout-form.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAOzD,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9D,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,wGAAwG;IACxG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,8GAA8G;IAC9G,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,OAAO,CAAC;IACvC,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAaD,wBAAgB,UAAU,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,aAAa,EAAE,OAAqB,EAAE,SAAS,EAAE,EAAE,eAAe,+BAoCtH"}
@@ -0,0 +1,45 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { errorLogLabel } from "@softure-ai/core";
4
+ import { Button } from "@softure-ai/ui";
5
+ import { useEffect, useRef, useState } from "react";
6
+ async function runBeforeSignOut(beforeSignOut) {
7
+ try {
8
+ await beforeSignOut();
9
+ }
10
+ catch (error) {
11
+ // The kind only: a message could carry the user's data, and the sign-out must not wait for a fix.
12
+ console.error(`@softure-ai/auth: before sign-out failed: ${errorLogLabel(error)}`);
13
+ }
14
+ }
15
+ export function LogoutForm({ action, messages, next, beforeSignOut, variant = "secondary", className }) {
16
+ const [stage, setStage] = useState("idle");
17
+ const formRef = useRef(null);
18
+ const isClearedRef = useRef(false);
19
+ // Submitted again once the button is enabled, with the button as the submitter, as a click would.
20
+ useEffect(() => {
21
+ const form = formRef.current;
22
+ if (stage !== "submitting" || form === null)
23
+ return;
24
+ isClearedRef.current = true;
25
+ setStage("idle");
26
+ form.requestSubmit(form.querySelector('button[type="submit"]'));
27
+ }, [stage]);
28
+ function holdForBeforeSignOut(event) {
29
+ if (beforeSignOut === undefined)
30
+ return;
31
+ if (isClearedRef.current) {
32
+ // The second submit goes through; a later one runs beforeSignOut again.
33
+ isClearedRef.current = false;
34
+ return;
35
+ }
36
+ // A prevented submit does not run the form's action; the second submit does.
37
+ event.preventDefault();
38
+ if (stage !== "idle")
39
+ return;
40
+ setStage("holding");
41
+ void runBeforeSignOut(beforeSignOut).then(() => setStage("submitting"));
42
+ }
43
+ return (_jsxs("form", { ref: formRef, action: action, onSubmit: holdForBeforeSignOut, className: className, children: [next === undefined ? null : _jsx("input", { type: "hidden", name: "next", value: next }), _jsx(Button, { type: "submit", variant: variant, pending: stage === "holding", children: messages.logout.submit })] }));
44
+ }
45
+ //# sourceMappingURL=logout-form.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logout-form.js","sourceRoot":"","sources":["../../src/ui/logout-form.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAkB,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAmBpE,KAAK,UAAU,gBAAgB,CAAC,aAA4B;IAC1D,IAAI,CAAC;QACH,MAAM,aAAa,EAAE,CAAC;IACxB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,kGAAkG;QAClG,OAAO,CAAC,KAAK,CAAC,6CAA6C,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAID,MAAM,UAAU,UAAU,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,GAAG,WAAW,EAAE,SAAS,EAAmB;IACrH,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAc,MAAM,CAAC,CAAC;IACxD,MAAM,OAAO,GAAG,MAAM,CAAkB,IAAI,CAAC,CAAC;IAC9C,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAEnC,kGAAkG;IAClG,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;QAC7B,IAAI,KAAK,KAAK,YAAY,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO;QACpD,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC;QAC5B,QAAQ,CAAC,MAAM,CAAC,CAAC;QACjB,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,aAAa,CAAoB,uBAAuB,CAAC,CAAC,CAAC;IACrF,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,SAAS,oBAAoB,CAAC,KAAiC;QAC7D,IAAI,aAAa,KAAK,SAAS;YAAE,OAAO;QACxC,IAAI,YAAY,CAAC,OAAO,EAAE,CAAC;YACzB,wEAAwE;YACxE,YAAY,CAAC,OAAO,GAAG,KAAK,CAAC;YAC7B,OAAO;QACT,CAAC;QACD,6EAA6E;QAC7E,KAAK,CAAC,cAAc,EAAE,CAAC;QACvB,IAAI,KAAK,KAAK,MAAM;YAAE,OAAO;QAC7B,QAAQ,CAAC,SAAS,CAAC,CAAC;QACpB,KAAK,gBAAgB,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,CACL,gBAAM,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,SAAS,EAAE,SAAS,aACrF,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,gBAAO,IAAI,EAAC,QAAQ,EAAC,IAAI,EAAC,MAAM,EAAC,KAAK,EAAE,IAAI,GAAI,EAC7E,KAAC,MAAM,IAAC,IAAI,EAAC,QAAQ,EAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,KAAK,SAAS,YACjE,QAAQ,CAAC,MAAM,CAAC,MAAM,GAChB,IACJ,CACR,CAAC;AACJ,CAAC"}
package/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "auth",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "dependsOn": { "security": "^0.2.0", "mailing": "^0.2.0?", "ops": "^0.2.0?", "jobs": "^0.1.1?" },
5
5
  "dbSchema": "auth",
6
6
  "tables": ["users", "sessions", "user_roles", "password_resets", "identities", "oidc_states"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softure-ai/auth",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "SOFTURE AI auth module: users, scrypt passwords, database sessions, register with consent, login, logout, password change, roles, a proxy route guard, pages and forms for Next.js, and a password reset sender over @softure-ai/mailing, sent in the request or by a background job through @softure-ai/jobs.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -46,7 +46,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
46
46
  export const auth = defineModule({
47
47
  manifest: {
48
48
  id: MODULE_ID,
49
- version: "0.2.10",
49
+ version: "0.2.12",
50
50
  dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
51
51
  dbSchema: "auth",
52
52
  tables: ["users", "sessions", "user_roles", "password_resets", "identities", "oidc_states"],
@@ -1,7 +1,9 @@
1
- // A logout button for the app's own pages: a plain form posting to `logoutAction`, so it works
2
- // without JavaScript. Server component. `next` rides along as a hidden field.
1
+ // A logout button for the app's own pages: `LogoutForm` posting to `logoutAction`, so it works without JavaScript.
2
+ // Server component. `next` rides along as a hidden field. To clear the device before the sign-out, render
3
+ // `LogoutForm` of `@softure-ai/auth/ui` with `beforeSignOut` from a client component instead.
3
4
  import { getSoftureConfig } from "@softure-ai/core/next";
4
- import { Button, type ButtonVariant } from "@softure-ai/ui";
5
+ import type { ButtonVariant } from "@softure-ai/ui";
6
+ import { LogoutForm } from "../ui/logout-form.js";
5
7
  import { logoutAction } from "./actions.js";
6
8
  import { getAuthMessages } from "./messages.js";
7
9
 
@@ -13,13 +15,5 @@ export interface LogoutButtonProps {
13
15
  }
14
16
 
15
17
  export function LogoutButton({ variant = "secondary", className, next }: LogoutButtonProps) {
16
- const messages = getAuthMessages(getSoftureConfig());
17
- return (
18
- <form action={logoutAction} className={className}>
19
- {next === undefined ? null : <input type="hidden" name="next" value={next} />}
20
- <Button type="submit" variant={variant}>
21
- {messages.logout.submit}
22
- </Button>
23
- </form>
24
- );
18
+ return <LogoutForm action={logoutAction} messages={getAuthMessages(getSoftureConfig())} next={next} variant={variant} className={className} />;
25
19
  }
package/src/ui/index.ts CHANGED
@@ -15,4 +15,5 @@ export {
15
15
  ResetPasswordForm,
16
16
  type ResetPasswordFormProps,
17
17
  } from "./auth-forms.js";
18
+ export { LogoutForm, type LogoutFormProps } from "./logout-form.js";
18
19
  export { OidcSignIn, type OidcSignInProps, type OidcSignInProvider, type OidcSignInSlot } from "./oidc-sign-in.js";
@@ -0,0 +1,71 @@
1
+ "use client";
2
+
3
+ import { errorLogLabel } from "@softure-ai/core";
4
+ import { Button, type ButtonVariant } from "@softure-ai/ui";
5
+ import { type FormEvent, useEffect, useRef, useState } from "react";
6
+ import type { AuthMessages } from "../messages/index.js";
7
+
8
+ // The sign-out control: a plain form posting to the logout action (`logoutAction` of `@softure-ai/auth/next`), so it
9
+ // works without JavaScript. With `beforeSignOut`, the first submit is held while the app clears what the device keeps
10
+ // (e.g. `clearDeviceData` of `@softure-ai/pwa`); then the form is submitted again and the action runs, whatever
11
+ // `beforeSignOut` did.
12
+
13
+ export interface LogoutFormProps {
14
+ readonly action: (formData: FormData) => void | Promise<void>;
15
+ readonly messages: AuthMessages;
16
+ /** Where to go after logout (a path on this app), e.g. `/login?next=…`; checked again on the server. */
17
+ readonly next?: string;
18
+ /** Awaited before the sign-out. A throw or rejection is logged by its kind only, and the sign-out goes on. */
19
+ readonly beforeSignOut?: () => unknown;
20
+ readonly variant?: ButtonVariant;
21
+ readonly className?: string;
22
+ }
23
+
24
+ async function runBeforeSignOut(beforeSignOut: () => unknown): Promise<void> {
25
+ try {
26
+ await beforeSignOut();
27
+ } catch (error) {
28
+ // The kind only: a message could carry the user's data, and the sign-out must not wait for a fix.
29
+ console.error(`@softure-ai/auth: before sign-out failed: ${errorLogLabel(error)}`);
30
+ }
31
+ }
32
+
33
+ type LogoutStage = "idle" | "holding" | "submitting";
34
+
35
+ export function LogoutForm({ action, messages, next, beforeSignOut, variant = "secondary", className }: LogoutFormProps) {
36
+ const [stage, setStage] = useState<LogoutStage>("idle");
37
+ const formRef = useRef<HTMLFormElement>(null);
38
+ const isClearedRef = useRef(false);
39
+
40
+ // Submitted again once the button is enabled, with the button as the submitter, as a click would.
41
+ useEffect(() => {
42
+ const form = formRef.current;
43
+ if (stage !== "submitting" || form === null) return;
44
+ isClearedRef.current = true;
45
+ setStage("idle");
46
+ form.requestSubmit(form.querySelector<HTMLButtonElement>('button[type="submit"]'));
47
+ }, [stage]);
48
+
49
+ function holdForBeforeSignOut(event: FormEvent<HTMLFormElement>): void {
50
+ if (beforeSignOut === undefined) return;
51
+ if (isClearedRef.current) {
52
+ // The second submit goes through; a later one runs beforeSignOut again.
53
+ isClearedRef.current = false;
54
+ return;
55
+ }
56
+ // A prevented submit does not run the form's action; the second submit does.
57
+ event.preventDefault();
58
+ if (stage !== "idle") return;
59
+ setStage("holding");
60
+ void runBeforeSignOut(beforeSignOut).then(() => setStage("submitting"));
61
+ }
62
+
63
+ return (
64
+ <form ref={formRef} action={action} onSubmit={holdForBeforeSignOut} className={className}>
65
+ {next === undefined ? null : <input type="hidden" name="next" value={next} />}
66
+ <Button type="submit" variant={variant} pending={stage === "holding"}>
67
+ {messages.logout.submit}
68
+ </Button>
69
+ </form>
70
+ );
71
+ }