@udibo/oauth2 0.54.0 → 0.55.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.
@@ -0,0 +1,86 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { AccountActionError, ActionForm, formatAccountDate, useAccountAction, useFocusAfterRemoval, } from "./account-internal.js";
3
+ import { combineClassNames } from "./internal.js";
4
+ const OTHERS_KEY = "\u0000revoke-other-sessions";
5
+ /**
6
+ * A ready-to-use list of an account's signed-in sessions. Pass the sessions
7
+ * your loader read and wire `onRevoke` / `onRevokeOthers` to your server;
8
+ * after an action succeeds, pass the updated `sessions` so the list follows.
9
+ *
10
+ * Each session shows its device, the current-session badge, its IP address
11
+ * and its sign-in and last-active times. A failed action shows its message in
12
+ * an error summary that takes focus, and a pending button keeps focus with
13
+ * `aria-disabled="true"`.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * import {
18
+ * SessionList,
19
+ * type SessionListItem,
20
+ * } from "@udibo/oauth2/react/components";
21
+ *
22
+ * function Sessions(props: { sessions: SessionListItem[] }) {
23
+ * return (
24
+ * <SessionList
25
+ * sessions={props.sessions}
26
+ * onRevoke={async (session) => {
27
+ * const response = await fetch(`/account/sessions/${session.id}`, {
28
+ * method: "DELETE",
29
+ * });
30
+ * if (!response.ok) return { error: "Couldn't sign out that session." };
31
+ * }}
32
+ * onRevokeOthers={async () => {
33
+ * const response = await fetch("/account/sessions", { method: "DELETE" });
34
+ * if (!response.ok) return { error: "Couldn't sign out other sessions." };
35
+ * }}
36
+ * />
37
+ * );
38
+ * }
39
+ * ```
40
+ */
41
+ export function SessionList(props) {
42
+ const { sessions, onRevoke, onRevokeOthers, formatDate = formatAccountDate, action, className, classNames, labels, children, } = props;
43
+ const runner = useAccountAction();
44
+ const rootRef = useFocusAfterRemoval(runner, sessions);
45
+ const revoke = async (session) => {
46
+ if (!onRevoke || session.current)
47
+ return;
48
+ await runner.run(session.id, () => onRevoke(session));
49
+ };
50
+ const revokeOthers = async () => {
51
+ if (!onRevokeOthers)
52
+ return;
53
+ await runner.run(OTHERS_KEY, () => onRevokeOthers());
54
+ };
55
+ if (children) {
56
+ return (_jsx(_Fragment, { children: children({
57
+ sessions,
58
+ pendingSessionId: runner.pendingKey === OTHERS_KEY ? null : runner.pendingKey,
59
+ revokingOthers: runner.pendingKey === OTHERS_KEY,
60
+ error: runner.error,
61
+ revoke,
62
+ revokeOthers,
63
+ }) }));
64
+ }
65
+ const otherCount = sessions.filter((session) => !session.current).length;
66
+ const deviceOf = (session) => session.device || (labels?.unknownDevice ?? "Unknown device");
67
+ return (_jsxs("div", { ref: rootRef, tabIndex: -1, "data-oauth2-session-list": "", className: combineClassNames(className, classNames?.root), children: [_jsx(AccountActionError, { error: runner.error, className: classNames?.errorSummary }), sessions.length === 0 ? (_jsx("p", { "data-oauth2-empty": "", className: classNames?.empty, children: labels?.empty ?? "No active sessions." })) : (_jsx("ul", { "data-oauth2-session-items": "", className: classNames?.list, children: sessions.map((session) => {
68
+ const device = deviceOf(session);
69
+ const meta = [
70
+ session.ipAddress || null,
71
+ session.createdAt
72
+ ? `${labels?.signedIn ?? "Signed in"} ${formatDate(session.createdAt)}`
73
+ : null,
74
+ session.lastActiveAt
75
+ ? `${labels?.lastActive ?? "Last active"} ${formatDate(session.lastActiveAt)}`
76
+ : null,
77
+ ].filter((part) => part !== null);
78
+ return (_jsxs("li", { "data-oauth2-session": session.id, ...(session.current
79
+ ? { "data-oauth2-current-session": "" }
80
+ : {}), className: classNames?.item, children: [_jsx("span", { "data-oauth2-session-device": "", className: classNames?.device, children: device }), session.current ? (_jsx("span", { "data-oauth2-session-badge": "", className: classNames?.badge, children: labels?.current ?? "Current session" })) : null, meta.length > 0 ? (_jsx("span", { "data-oauth2-session-meta": "", className: classNames?.meta, children: meta.join(" · ") })) : null, onRevoke && !session.current ? (_jsx(ActionForm, { fields: { intent: "revoke-session", sessionId: session.id }, action: action, onActivate: () => {
81
+ void revoke(session);
82
+ }, pending: runner.pendingKey === session.id, label: labels?.revoke ?? "Sign out", pendingLabel: labels?.revoking ?? "Signing out…", accessibleName: device, dataAttribute: "data-oauth2-revoke-session", dataValue: session.id, className: classNames?.revoke })) : null] }, session.id));
83
+ }) })), onRevokeOthers && otherCount > 0 ? (_jsx("div", { "data-oauth2-actions": "", className: classNames?.actions, children: _jsx(ActionForm, { fields: { intent: "revoke-other-sessions" }, action: action, onActivate: () => {
84
+ void revokeOthers();
85
+ }, pending: runner.pendingKey === OTHERS_KEY, label: labels?.revokeOthers ?? "Sign out everywhere else", pendingLabel: labels?.revokingOthers ?? "Signing out…", dataAttribute: "data-oauth2-revoke-other-sessions", className: classNames?.revokeOthers }) })) : null] }));
86
+ }
@@ -42,6 +42,24 @@ export interface AuthFormResult {
42
42
  * {@link AuthFormResult} (or throw) to report the outcome.
43
43
  */
44
44
  export type AuthSubmitHandler<V extends AuthFormValues> = (values: V) => Promise<AuthFormResult | void> | AuthFormResult | void;
45
+ /**
46
+ * The result an account action — signing out a session, disconnecting a linked
47
+ * account — may return. Return nothing to signal success; return `error` to
48
+ * show a message in the component's error summary, which takes focus. A thrown
49
+ * error is shown the same way, using its message.
50
+ */
51
+ export interface AccountActionResult {
52
+ /** The message shown in the component's error summary. */
53
+ error?: string;
54
+ }
55
+ /**
56
+ * The app-provided function an account component calls for one of its row
57
+ * actions, with the item the person acted on. Resolve to nothing on success;
58
+ * the component shows its pending label until the returned promise settles.
59
+ * Update the data you pass the component yourself, for example by
60
+ * revalidating, so the row disappears.
61
+ */
62
+ export type AccountActionHandler<T> = (item: T) => Promise<AccountActionResult | void> | AccountActionResult | void;
45
63
  /**
46
64
  * A social / federated identity provider to render a "Continue with …" button
47
65
  * for. Supply an array of these to a sign-in or sign-up component and the
@@ -84,14 +102,16 @@ export interface PasskeyOption {
84
102
  }
85
103
  /**
86
104
  * The class hook every prebuilt component shares. Component-specific class-name
87
- * maps ({@link AuthFormClassNames}, `UserMenuClassNames`) extend this, so `root`
105
+ * maps ({@link AuthFormClassNames}, `UserMenuClassNames`,
106
+ * `SessionListClassNames`, `LinkedAccountsClassNames`) extend this, so `root`
88
107
  * means the same thing everywhere.
89
108
  */
90
109
  export interface BaseClassNames {
91
110
  /**
92
111
  * The component's outermost element — the one carrying its
93
- * `data-oauth2-form` / `data-oauth2-user-menu` marker. Merged with the
94
- * component's top-level `className` prop.
112
+ * `data-oauth2-form`, `data-oauth2-user-menu`, `data-oauth2-session-list` or
113
+ * `data-oauth2-linked-accounts` marker. Merged with the component's
114
+ * top-level `className` prop.
95
115
  */
96
116
  root?: string;
97
117
  }
@@ -196,10 +216,10 @@ export interface BaseAuthFormProps<V extends AuthFormValues> {
196
216
  */
197
217
  classNames?: AuthFormClassNames;
198
218
  /**
199
- * `action` for the rendered `<form>`. Set it (with {@link method}) on a
200
- * server-rendered page so the form still posts when JavaScript never
201
- * arrives; every rendered field carries its `name`, so a native post sends
202
- * those fields under the keys `onSubmit` receives. A value the form holds
219
+ * `action` for the rendered `<form>`. Set it on a server-rendered page so
220
+ * the form still posts to that route when JavaScript never arrives; every
221
+ * rendered field carries its `name`, so a native post sends those fields
222
+ * under the keys `onSubmit` receives. A value the form holds
203
223
  * without a rendered field (such as `ResetPasswordForm`'s `token`) is not
204
224
  * posted, so the server's handler must obtain it another way. Once React is
205
225
  * running, `onSubmit` handles the submit and the native post is suppressed.
@@ -208,11 +228,12 @@ export interface BaseAuthFormProps<V extends AuthFormValues> {
208
228
  */
209
229
  action?: string;
210
230
  /**
211
- * `method` for the rendered `<form>`. Defaults to `"post"` whenever
212
- * {@link action} is set, because the fields these forms render — passwords
213
- * and one-time codes — must never reach a URL, where a `get` form
214
- * would put them: browser history, the `Referer` sent to every third-party
215
- * asset on the destination page, and the server's access log.
231
+ * `method` for the rendered `<form>`. Defaults to `"post"`, with or without
232
+ * {@link action}. A submit before React hydrates, or after its bundle fails
233
+ * to load, is a native one; with no `action` it posts back to the current
234
+ * page. Do not pass `"get"`: it puts the fields these forms render —
235
+ * passwords, one-time codes, email addresses — in the URL, browser history
236
+ * and the server's access log.
216
237
  */
217
238
  method?: "get" | "post";
218
239
  /**
@@ -113,7 +113,7 @@ export interface AuthFormState<V extends AuthFormValues> {
113
113
  * },
114
114
  * });
115
115
  * return (
116
- * <form onSubmit={form.handleSubmit}>
116
+ * <form method="post" onSubmit={form.handleSubmit}>
117
117
  * <input {...form.getFieldProps("identifier")} />
118
118
  * <input {...form.getFieldProps("password")} type="password" />
119
119
  * {form.formError ? <p role="alert">{form.formError}</p> : null}
@@ -30,7 +30,7 @@ import { useCallback, useId, useMemo, useRef, useState, } from "react";
30
30
  * },
31
31
  * });
32
32
  * return (
33
- * <form onSubmit={form.handleSubmit}>
33
+ * <form method="post" onSubmit={form.handleSubmit}>
34
34
  * <input {...form.getFieldProps("identifier")} />
35
35
  * <input {...form.getFieldProps("password")} type="password" />
36
36
  * {form.formError ? <p role="alert">{form.formError}</p> : null}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@udibo/oauth2",
3
- "version": "0.54.0",
3
+ "version": "0.55.1",
4
4
  "description": "A flexible OAuth2 toolkit: framework-agnostic authorization & resource servers, a typed client, Hono adapters (including a backend-for-frontend), Express adapters, and React bindings.",
5
5
  "keywords": [
6
6
  "authorization-server",