@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.
- package/SECURITY.md +1 -1
- package/dist/react/components/account-internal.d.ts +45 -0
- package/dist/react/components/account-internal.js +111 -0
- package/dist/react/components/change-password-form.d.ts +171 -0
- package/dist/react/components/change-password-form.js +130 -0
- package/dist/react/components/class-names.d.ts +3 -2
- package/dist/react/components/class-names.js +3 -2
- package/dist/react/components/internal.d.ts +1 -0
- package/dist/react/components/internal.js +3 -3
- package/dist/react/components/linked-accounts.d.ts +212 -0
- package/dist/react/components/linked-accounts.js +98 -0
- package/dist/react/components/mod.d.ts +16 -3
- package/dist/react/components/mod.js +12 -2
- package/dist/react/components/session-list.d.ts +204 -0
- package/dist/react/components/session-list.js +86 -0
- package/dist/react/components/types.d.ts +33 -12
- package/dist/react/components/use-auth-form.d.ts +1 -1
- package/dist/react/components/use-auth-form.js +1 -1
- package/package.json +1 -1
package/SECURITY.md
CHANGED
|
@@ -13,7 +13,7 @@ reports seriously and value the time of security researchers.
|
|
|
13
13
|
|
|
14
14
|
Pre-1.0, security fixes land on the latest release only. From 1.0 on, the latest
|
|
15
15
|
minor of the current major receives fixes, and the final minor of the previous
|
|
16
|
-
major receives critical fixes for 6 months after a new major ships.
|
|
16
|
+
major receives critical fixes for 6 months after a new major ships. Security fixes are announced through a GitHub security advisory or by deprecating affected versions on npm, so a pinned release that a fix affects can be found.
|
|
17
17
|
|
|
18
18
|
## Reporting a vulnerability
|
|
19
19
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type ReactNode, type RefObject } from "react";
|
|
2
|
+
import type { AccountActionResult } from "./types.ts";
|
|
3
|
+
export interface AccountActionRunner {
|
|
4
|
+
pendingKey: string | null;
|
|
5
|
+
error: string | null;
|
|
6
|
+
successCount: number;
|
|
7
|
+
/** The element that had focus when the last action started. */
|
|
8
|
+
focusedAtStart: RefObject<Element | null>;
|
|
9
|
+
run(key: string, action: () => Promise<AccountActionResult | void> | AccountActionResult | void): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One in-flight action at a time across a list's buttons: a second activation
|
|
13
|
+
* while one is pending is ignored, and a returned `error` or a throw becomes
|
|
14
|
+
* the list's error message.
|
|
15
|
+
*/
|
|
16
|
+
export declare function useAccountAction(): AccountActionRunner;
|
|
17
|
+
/**
|
|
18
|
+
* Moves focus to the list root when the control that had focus as a
|
|
19
|
+
* successful action started leaves the page (a removed row's button drops
|
|
20
|
+
* focus to `<body>`). It stops watching once focus moves away from that
|
|
21
|
+
* control, and never moves focus that is somewhere else on the page.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useFocusAfterRemoval(runner: AccountActionRunner, items: unknown): RefObject<HTMLDivElement | null>;
|
|
24
|
+
export declare function AccountActionError(props: {
|
|
25
|
+
error: string | null;
|
|
26
|
+
className: string | undefined;
|
|
27
|
+
}): ReactNode;
|
|
28
|
+
/**
|
|
29
|
+
* A one-button `<form>`: its hidden `fields` post natively to `action` before
|
|
30
|
+
* JavaScript runs, and `onActivate` handles the submit once it does. Always
|
|
31
|
+
* `post`, so a pre-hydration submit never puts the fields in a URL.
|
|
32
|
+
*/
|
|
33
|
+
export declare function ActionForm(props: {
|
|
34
|
+
fields: Record<string, string>;
|
|
35
|
+
action: string | undefined;
|
|
36
|
+
onActivate: () => void;
|
|
37
|
+
pending: boolean;
|
|
38
|
+
label: string;
|
|
39
|
+
pendingLabel: string;
|
|
40
|
+
accessibleName?: string;
|
|
41
|
+
dataAttribute: `data-oauth2-${string}`;
|
|
42
|
+
dataValue?: string;
|
|
43
|
+
className: string | undefined;
|
|
44
|
+
}): ReactNode;
|
|
45
|
+
export declare function formatAccountDate(date: Date): string;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState, } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* One in-flight action at a time across a list's buttons: a second activation
|
|
5
|
+
* while one is pending is ignored, and a returned `error` or a throw becomes
|
|
6
|
+
* the list's error message.
|
|
7
|
+
*/
|
|
8
|
+
export function useAccountAction() {
|
|
9
|
+
const [pendingKey, setPendingKey] = useState(null);
|
|
10
|
+
const [error, setError] = useState(null);
|
|
11
|
+
const [successCount, setSuccessCount] = useState(0);
|
|
12
|
+
const runningRef = useRef(false);
|
|
13
|
+
const focusedAtStart = useRef(null);
|
|
14
|
+
const run = useCallback(async (key, action) => {
|
|
15
|
+
if (runningRef.current)
|
|
16
|
+
return;
|
|
17
|
+
runningRef.current = true;
|
|
18
|
+
focusedAtStart.current =
|
|
19
|
+
typeof document === "undefined" ? null : document.activeElement;
|
|
20
|
+
setPendingKey(key);
|
|
21
|
+
setError(null);
|
|
22
|
+
let message = null;
|
|
23
|
+
try {
|
|
24
|
+
const result = await action();
|
|
25
|
+
if (result?.error)
|
|
26
|
+
message = result.error;
|
|
27
|
+
}
|
|
28
|
+
catch (thrown) {
|
|
29
|
+
message =
|
|
30
|
+
thrown instanceof Error ? thrown.message : "Something went wrong.";
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
runningRef.current = false;
|
|
34
|
+
setPendingKey(null);
|
|
35
|
+
}
|
|
36
|
+
if (message)
|
|
37
|
+
setError(message);
|
|
38
|
+
else
|
|
39
|
+
setSuccessCount((count) => count + 1);
|
|
40
|
+
}, []);
|
|
41
|
+
return { pendingKey, error, successCount, focusedAtStart, run };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Moves focus to the list root when the control that had focus as a
|
|
45
|
+
* successful action started leaves the page (a removed row's button drops
|
|
46
|
+
* focus to `<body>`). It stops watching once focus moves away from that
|
|
47
|
+
* control, and never moves focus that is somewhere else on the page.
|
|
48
|
+
*/
|
|
49
|
+
export function useFocusAfterRemoval(runner, items) {
|
|
50
|
+
const rootRef = useRef(null);
|
|
51
|
+
const handled = useRef(0);
|
|
52
|
+
const watched = useRef(null);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (typeof document === "undefined")
|
|
55
|
+
return;
|
|
56
|
+
if (runner.successCount !== handled.current) {
|
|
57
|
+
handled.current = runner.successCount;
|
|
58
|
+
const control = runner.focusedAtStart.current;
|
|
59
|
+
watched.current =
|
|
60
|
+
control === null || control === document.body ? null : control;
|
|
61
|
+
}
|
|
62
|
+
const control = watched.current;
|
|
63
|
+
if (!control)
|
|
64
|
+
return;
|
|
65
|
+
const active = document.activeElement;
|
|
66
|
+
if (!control.isConnected) {
|
|
67
|
+
watched.current = null;
|
|
68
|
+
if (active === null || active === document.body) {
|
|
69
|
+
rootRef.current?.focus({ preventScroll: true });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (active !== control) {
|
|
73
|
+
watched.current = null;
|
|
74
|
+
}
|
|
75
|
+
}, [runner.successCount, items, runner.focusedAtStart]);
|
|
76
|
+
return rootRef;
|
|
77
|
+
}
|
|
78
|
+
export function AccountActionError(props) {
|
|
79
|
+
const { error, className } = props;
|
|
80
|
+
const ref = useRef(null);
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (error)
|
|
83
|
+
ref.current?.focus();
|
|
84
|
+
}, [error]);
|
|
85
|
+
if (!error)
|
|
86
|
+
return null;
|
|
87
|
+
return (_jsx("div", { ref: ref, tabIndex: -1, "data-oauth2-error-summary": "", className: className, children: _jsx("span", { role: "alert", "data-oauth2-error-summary-message": "", children: error }) }));
|
|
88
|
+
}
|
|
89
|
+
function ignoreActivation(event) {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A one-button `<form>`: its hidden `fields` post natively to `action` before
|
|
94
|
+
* JavaScript runs, and `onActivate` handles the submit once it does. Always
|
|
95
|
+
* `post`, so a pre-hydration submit never puts the fields in a URL.
|
|
96
|
+
*/
|
|
97
|
+
export function ActionForm(props) {
|
|
98
|
+
const { fields, action, onActivate, pending, label, pendingLabel, accessibleName, dataAttribute, dataValue = "", className, } = props;
|
|
99
|
+
const onSubmit = (event) => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
onActivate();
|
|
102
|
+
};
|
|
103
|
+
const text = pending ? pendingLabel : label;
|
|
104
|
+
return (_jsxs("form", { onSubmit: onSubmit, action: action, method: "post", "data-oauth2-action-form": "", children: [Object.entries(fields).map(([name, value]) => (_jsx("input", { type: "hidden", name: name, value: value }, name))), _jsx("button", { type: "submit", "aria-disabled": pending ? true : undefined, "aria-label": accessibleName ? `${text} ${accessibleName}` : undefined, onClick: pending ? ignoreActivation : undefined, [dataAttribute]: dataValue, className: className, children: text })] }));
|
|
105
|
+
}
|
|
106
|
+
export function formatAccountDate(date) {
|
|
107
|
+
return date.toLocaleString(undefined, {
|
|
108
|
+
dateStyle: "medium",
|
|
109
|
+
timeStyle: "short",
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link ChangePasswordForm} — a default, accessible, unstyled form for a
|
|
3
|
+
* signed-in person to change their password, or to set a first one on an
|
|
4
|
+
* account that signs in without a password. Built on {@link useAuthForm}: it
|
|
5
|
+
* collects the passwords, checks the confirmation and length on the client,
|
|
6
|
+
* and hands the values to your `onSubmit`.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { type ReactNode } from "react";
|
|
11
|
+
import type { AuthFormState } from "./use-auth-form.ts";
|
|
12
|
+
import type { BaseAuthFormProps } from "./types.ts";
|
|
13
|
+
/** Values collected by {@link ChangePasswordForm}. */
|
|
14
|
+
export type ChangePasswordValues = {
|
|
15
|
+
/**
|
|
16
|
+
* The current password. Always `""` when `hasPassword` is `false`, since
|
|
17
|
+
* the field is not rendered.
|
|
18
|
+
*/
|
|
19
|
+
currentPassword: string;
|
|
20
|
+
/** The new password. */
|
|
21
|
+
newPassword: string;
|
|
22
|
+
/** Confirmation of the new password; the form blocks a mismatch. */
|
|
23
|
+
confirmPassword: string;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to sign out the account's other sessions with the change. Fixed
|
|
26
|
+
* by the `revokeOtherSessions` prop unless it is `"optional"`, when the
|
|
27
|
+
* person's checkbox decides.
|
|
28
|
+
*/
|
|
29
|
+
revokeOtherSessions: boolean;
|
|
30
|
+
};
|
|
31
|
+
/** Visible text overrides for {@link ChangePasswordForm}. */
|
|
32
|
+
export interface ChangePasswordLabels {
|
|
33
|
+
/** Label for the current-password field. Default: `"Current password"`. */
|
|
34
|
+
currentPassword?: string;
|
|
35
|
+
/** Label for the new-password field. Default: `"New password"`. */
|
|
36
|
+
newPassword?: string;
|
|
37
|
+
/** Label for the confirmation field. Default: `"Confirm new password"`. */
|
|
38
|
+
confirmPassword?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Submit button text. Default: `"Update password"`, or `"Set password"`
|
|
41
|
+
* when `hasPassword` is `false`.
|
|
42
|
+
*/
|
|
43
|
+
submit?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Submit button text while submitting. Default: `"Updating…"`, or
|
|
46
|
+
* `"Setting…"` when `hasPassword` is `false`.
|
|
47
|
+
*/
|
|
48
|
+
submitting?: string;
|
|
49
|
+
/** Mismatch message on the confirmation field. Default: `"Passwords do not match."`. */
|
|
50
|
+
passwordMismatch?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Message on the new-password field when it is shorter than `minLength`.
|
|
53
|
+
* Default: `"Use at least {minLength} characters."`.
|
|
54
|
+
*/
|
|
55
|
+
tooShort?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Label of the checkbox shown when `revokeOtherSessions` is `"optional"`.
|
|
58
|
+
* Default: `"Sign out of my other sessions"`.
|
|
59
|
+
*/
|
|
60
|
+
revokeOtherSessions?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Text under the fields when `revokeOtherSessions` is `true`. Default:
|
|
63
|
+
* `"Your other sessions will be signed out."`. Pass `""` to show none.
|
|
64
|
+
*/
|
|
65
|
+
revokeOtherSessionsNotice?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Status message after a successful submit. Default: `"Password updated."`,
|
|
68
|
+
* or `"Password set."` when `hasPassword` is `false`.
|
|
69
|
+
*/
|
|
70
|
+
success?: string;
|
|
71
|
+
}
|
|
72
|
+
/** Props for {@link ChangePasswordForm}. */
|
|
73
|
+
export interface ChangePasswordFormProps extends BaseAuthFormProps<ChangePasswordValues> {
|
|
74
|
+
/**
|
|
75
|
+
* Whether the account has a password now. Default: `true`, which asks for
|
|
76
|
+
* the current password. Pass `false` for an account that signs in another
|
|
77
|
+
* way — a social account, a passkey or an emailed code — to set a first
|
|
78
|
+
* password: the current-password field is dropped and the button reads
|
|
79
|
+
* "Set password". Confirm it is the account owner on your server before
|
|
80
|
+
* setting one, since no current password proves it.
|
|
81
|
+
*
|
|
82
|
+
* Your server decides between setting and changing from the account it has
|
|
83
|
+
* stored, never from this prop or an empty `currentPassword`: refuse a
|
|
84
|
+
* request without the current password for an account that has one.
|
|
85
|
+
* Should it return `fieldErrors.currentPassword` anyway — `hasPassword` was
|
|
86
|
+
* stale — the form shows that message in its error summary, since there is
|
|
87
|
+
* no field to attach it to.
|
|
88
|
+
*/
|
|
89
|
+
hasPassword?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* What happens to the account's other sessions. Default: `true` — the
|
|
92
|
+
* change signs them out, the form says so, and `onSubmit` receives
|
|
93
|
+
* `revokeOtherSessions: true`. `"optional"` renders a checkbox, checked to
|
|
94
|
+
* start, so the person decides. `false` keeps them and says nothing. Your
|
|
95
|
+
* server performs the sign-out; this only states and forwards the intent.
|
|
96
|
+
*
|
|
97
|
+
* A no-JavaScript post carries the same choice as `revokeOtherSessions`:
|
|
98
|
+
* `"true"` or `"false"` from a hidden field when the prop fixes it, or
|
|
99
|
+
* `"true"` from the checked checkbox (absent when unchecked) in
|
|
100
|
+
* `"optional"` mode.
|
|
101
|
+
*/
|
|
102
|
+
revokeOtherSessions?: boolean | "optional";
|
|
103
|
+
/**
|
|
104
|
+
* Shortest new password to accept. Sets the field's `minlength` and blocks
|
|
105
|
+
* a shorter one on the client. Enforce your password policy on the server
|
|
106
|
+
* too; this only saves a round trip.
|
|
107
|
+
*/
|
|
108
|
+
minLength?: number;
|
|
109
|
+
/** Text overrides. */
|
|
110
|
+
labels?: ChangePasswordLabels;
|
|
111
|
+
/**
|
|
112
|
+
* Render-prop escape hatch. When provided, you own the markup and receive
|
|
113
|
+
* the live {@link AuthFormState}; the component still owns state, the
|
|
114
|
+
* client-side checks and submit.
|
|
115
|
+
*/
|
|
116
|
+
children?: (state: AuthFormState<ChangePasswordValues>) => ReactNode;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A ready-to-use change-password form, which also sets a first password on an
|
|
120
|
+
* account without one (`hasPassword={false}`). Wire `onSubmit` to your server
|
|
121
|
+
* and return `{ fieldErrors: { currentPassword: "…" } }` for a wrong current
|
|
122
|
+
* password, or `{ fieldErrors: { newPassword: "…" } }` for one your policy
|
|
123
|
+
* refuses; focus moves to the first errored field.
|
|
124
|
+
*
|
|
125
|
+
* After a successful submit the password fields are cleared and a status
|
|
126
|
+
* message appears above the form, which stays in place.
|
|
127
|
+
*
|
|
128
|
+
* @example Change a password, signing out other sessions
|
|
129
|
+
* ```tsx
|
|
130
|
+
* import { ChangePasswordForm } from "@udibo/oauth2/react/components";
|
|
131
|
+
*
|
|
132
|
+
* function ChangePassword() {
|
|
133
|
+
* return (
|
|
134
|
+
* <ChangePasswordForm
|
|
135
|
+
* minLength={12}
|
|
136
|
+
* onSubmit={async (values) => {
|
|
137
|
+
* const response = await fetch("/account/password", {
|
|
138
|
+
* method: "POST",
|
|
139
|
+
* headers: { "content-type": "application/json" },
|
|
140
|
+
* body: JSON.stringify(values),
|
|
141
|
+
* });
|
|
142
|
+
* if (!response.ok) return await response.json();
|
|
143
|
+
* }}
|
|
144
|
+
* />
|
|
145
|
+
* );
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @example Set a first password on a social or passkey account
|
|
150
|
+
* ```tsx
|
|
151
|
+
* import { ChangePasswordForm } from "@udibo/oauth2/react/components";
|
|
152
|
+
*
|
|
153
|
+
* function SetPassword(props: { hasPassword: boolean }) {
|
|
154
|
+
* return (
|
|
155
|
+
* <ChangePasswordForm
|
|
156
|
+
* hasPassword={props.hasPassword}
|
|
157
|
+
* revokeOtherSessions="optional"
|
|
158
|
+
* onSubmit={async (values) => {
|
|
159
|
+
* const response = await fetch("/account/password", {
|
|
160
|
+
* method: "PUT",
|
|
161
|
+
* headers: { "content-type": "application/json" },
|
|
162
|
+
* body: JSON.stringify(values),
|
|
163
|
+
* });
|
|
164
|
+
* if (!response.ok) return await response.json();
|
|
165
|
+
* }}
|
|
166
|
+
* />
|
|
167
|
+
* );
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export declare function ChangePasswordForm(props: ChangePasswordFormProps): ReactNode;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* {@link ChangePasswordForm} — a default, accessible, unstyled form for a
|
|
4
|
+
* signed-in person to change their password, or to set a first one on an
|
|
5
|
+
* account that signs in without a password. Built on {@link useAuthForm}: it
|
|
6
|
+
* collects the passwords, checks the confirmation and length on the client,
|
|
7
|
+
* and hands the values to your `onSubmit`.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { useEffect, useState } from "react";
|
|
12
|
+
import { useAuthForm } from "./use-auth-form.js";
|
|
13
|
+
import { AuthFormShell, SubmitButton, TextField, useFocusFirstError, } from "./internal.js";
|
|
14
|
+
import { useAuthFormClassNames } from "./class-names.js";
|
|
15
|
+
function withoutCurrentPasswordField(result) {
|
|
16
|
+
const message = result?.fieldErrors?.currentPassword;
|
|
17
|
+
if (!result || message === undefined)
|
|
18
|
+
return result;
|
|
19
|
+
const { currentPassword: _moved, ...fieldErrors } = result.fieldErrors ?? {};
|
|
20
|
+
return { ...result, error: result.error || message, fieldErrors };
|
|
21
|
+
}
|
|
22
|
+
const CLEARED_PASSWORDS = {
|
|
23
|
+
currentPassword: "",
|
|
24
|
+
newPassword: "",
|
|
25
|
+
confirmPassword: "",
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* A ready-to-use change-password form, which also sets a first password on an
|
|
29
|
+
* account without one (`hasPassword={false}`). Wire `onSubmit` to your server
|
|
30
|
+
* and return `{ fieldErrors: { currentPassword: "…" } }` for a wrong current
|
|
31
|
+
* password, or `{ fieldErrors: { newPassword: "…" } }` for one your policy
|
|
32
|
+
* refuses; focus moves to the first errored field.
|
|
33
|
+
*
|
|
34
|
+
* After a successful submit the password fields are cleared and a status
|
|
35
|
+
* message appears above the form, which stays in place.
|
|
36
|
+
*
|
|
37
|
+
* @example Change a password, signing out other sessions
|
|
38
|
+
* ```tsx
|
|
39
|
+
* import { ChangePasswordForm } from "@udibo/oauth2/react/components";
|
|
40
|
+
*
|
|
41
|
+
* function ChangePassword() {
|
|
42
|
+
* return (
|
|
43
|
+
* <ChangePasswordForm
|
|
44
|
+
* minLength={12}
|
|
45
|
+
* onSubmit={async (values) => {
|
|
46
|
+
* const response = await fetch("/account/password", {
|
|
47
|
+
* method: "POST",
|
|
48
|
+
* headers: { "content-type": "application/json" },
|
|
49
|
+
* body: JSON.stringify(values),
|
|
50
|
+
* });
|
|
51
|
+
* if (!response.ok) return await response.json();
|
|
52
|
+
* }}
|
|
53
|
+
* />
|
|
54
|
+
* );
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @example Set a first password on a social or passkey account
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { ChangePasswordForm } from "@udibo/oauth2/react/components";
|
|
61
|
+
*
|
|
62
|
+
* function SetPassword(props: { hasPassword: boolean }) {
|
|
63
|
+
* return (
|
|
64
|
+
* <ChangePasswordForm
|
|
65
|
+
* hasPassword={props.hasPassword}
|
|
66
|
+
* revokeOtherSessions="optional"
|
|
67
|
+
* onSubmit={async (values) => {
|
|
68
|
+
* const response = await fetch("/account/password", {
|
|
69
|
+
* method: "PUT",
|
|
70
|
+
* headers: { "content-type": "application/json" },
|
|
71
|
+
* body: JSON.stringify(values),
|
|
72
|
+
* });
|
|
73
|
+
* if (!response.ok) return await response.json();
|
|
74
|
+
* }}
|
|
75
|
+
* />
|
|
76
|
+
* );
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function ChangePasswordForm(props) {
|
|
81
|
+
const { onSubmit, hasPassword = true, revokeOtherSessions = true, minLength, className, labels, children, } = props;
|
|
82
|
+
const [submittedWithPassword, setSubmittedWithPassword] = useState(hasPassword);
|
|
83
|
+
const normalize = (values) => ({
|
|
84
|
+
...values,
|
|
85
|
+
currentPassword: hasPassword ? values.currentPassword : "",
|
|
86
|
+
revokeOtherSessions: revokeOtherSessions === "optional"
|
|
87
|
+
? values.revokeOtherSessions
|
|
88
|
+
: revokeOtherSessions,
|
|
89
|
+
});
|
|
90
|
+
const { onSuccess } = props;
|
|
91
|
+
const form = useAuthForm({
|
|
92
|
+
initialValues: {
|
|
93
|
+
...CLEARED_PASSWORDS,
|
|
94
|
+
revokeOtherSessions: revokeOtherSessions !== false,
|
|
95
|
+
},
|
|
96
|
+
onSubmit: async (values) => {
|
|
97
|
+
setSubmittedWithPassword(hasPassword);
|
|
98
|
+
const result = await onSubmit(normalize(values));
|
|
99
|
+
return hasPassword ? result : withoutCurrentPasswordField(result);
|
|
100
|
+
},
|
|
101
|
+
onSuccess: onSuccess ? (values) => onSuccess(normalize(values)) : undefined,
|
|
102
|
+
validate: (values) => {
|
|
103
|
+
const errors = {};
|
|
104
|
+
if (minLength !== undefined && values.newPassword.length < minLength) {
|
|
105
|
+
errors.newPassword =
|
|
106
|
+
labels?.tooShort ?? `Use at least ${minLength} characters.`;
|
|
107
|
+
}
|
|
108
|
+
if (values.newPassword !== values.confirmPassword) {
|
|
109
|
+
errors.confirmPassword =
|
|
110
|
+
labels?.passwordMismatch ?? "Passwords do not match.";
|
|
111
|
+
}
|
|
112
|
+
return errors;
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
const { setValue, setValues, succeeded, submitCount } = form;
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (succeeded)
|
|
118
|
+
setValues(CLEARED_PASSWORDS);
|
|
119
|
+
}, [succeeded, submitCount, setValues]);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
setValue("revokeOtherSessions", revokeOtherSessions !== false);
|
|
122
|
+
}, [revokeOtherSessions, setValue]);
|
|
123
|
+
useFocusFirstError(form);
|
|
124
|
+
const classNames = useAuthFormClassNames(props.classNames);
|
|
125
|
+
const revokeField = form.getFieldProps("revokeOtherSessions");
|
|
126
|
+
const notice = labels?.revokeOtherSessionsNotice ??
|
|
127
|
+
"Your other sessions will be signed out.";
|
|
128
|
+
return (_jsxs(AuthFormShell, { name: hasPassword ? "change-password" : "set-password", form: form, className: className, classNames: classNames, render: children, action: props.action, method: props.method ?? "post", formRef: props.formRef, aboveForm: form.succeeded ? (_jsx("div", { "data-oauth2-success": "", role: "status", className: classNames.success, children: labels?.success ??
|
|
129
|
+
(submittedWithPassword ? "Password updated." : "Password set.") })) : null, children: [hasPassword ? (_jsx(TextField, { fieldProps: form.getFieldProps("currentPassword"), label: labels?.currentPassword ?? "Current password", type: "password", autoComplete: "current-password", required: true, error: form.errors.currentPassword })) : null, _jsx(TextField, { fieldProps: form.getFieldProps("newPassword"), label: labels?.newPassword ?? "New password", type: "password", autoComplete: "new-password", minLength: minLength, required: true, error: form.errors.newPassword }), _jsx(TextField, { fieldProps: form.getFieldProps("confirmPassword"), label: labels?.confirmPassword ?? "Confirm new password", type: "password", autoComplete: "new-password", required: true, error: form.errors.confirmPassword }), revokeOtherSessions === "optional" ? (_jsx("div", { "data-oauth2-field": "revokeOtherSessions", className: classNames.field, children: _jsxs("label", { htmlFor: revokeField.id, "data-oauth2-label": "", className: classNames.label, children: [_jsx("input", { ...revokeField, value: "true", type: "checkbox", "data-oauth2-input": "", className: classNames.input }), labels?.revokeOtherSessions ?? "Sign out of my other sessions"] }) })) : (_jsx("input", { type: "hidden", name: "revokeOtherSessions", value: String(revokeOtherSessions) })), revokeOtherSessions === true && notice ? (_jsx("p", { "data-oauth2-revoke-notice": "", className: classNames.hint, children: notice })) : null, _jsx(SubmitButton, { isSubmitting: form.isSubmitting, label: labels?.submit ?? (hasPassword ? "Update password" : "Set password"), submittingLabel: labels?.submitting ?? (hasPassword ? "Updating…" : "Setting…") })] }));
|
|
130
|
+
}
|
|
@@ -68,8 +68,9 @@ export interface AuthFormClassNamesProviderProps {
|
|
|
68
68
|
* own `classNames` prop — overrides the inherited map slot by slot, leaving the
|
|
69
69
|
* slots it does not name intact.
|
|
70
70
|
*
|
|
71
|
-
* It themes the auth **forms
|
|
72
|
-
* and
|
|
71
|
+
* It themes the auth **forms**, `ChangePasswordForm` included; `UserMenu`,
|
|
72
|
+
* `SessionList` and `LinkedAccounts` are deliberately outside its reach and
|
|
73
|
+
* take their classes from their own `className` / `classNames` props.
|
|
73
74
|
*
|
|
74
75
|
* An inline `classNames` literal is fine here — the published map is cached on
|
|
75
76
|
* the slot values rather than on the object's identity, so re-rendering with an
|
|
@@ -92,8 +92,9 @@ export function AuthFormClassNamesScope(props) {
|
|
|
92
92
|
* own `classNames` prop — overrides the inherited map slot by slot, leaving the
|
|
93
93
|
* slots it does not name intact.
|
|
94
94
|
*
|
|
95
|
-
* It themes the auth **forms
|
|
96
|
-
* and
|
|
95
|
+
* It themes the auth **forms**, `ChangePasswordForm` included; `UserMenu`,
|
|
96
|
+
* `SessionList` and `LinkedAccounts` are deliberately outside its reach and
|
|
97
|
+
* take their classes from their own `className` / `classNames` props.
|
|
97
98
|
*
|
|
98
99
|
* An inline `classNames` literal is fine here — the published map is cached on
|
|
99
100
|
* the slot values rather than on the object's identity, so re-rendering with an
|
|
@@ -9,7 +9,7 @@ import { AuthFormClassNamesScope, useAuthFormClassNames, } from "./class-names.j
|
|
|
9
9
|
*/
|
|
10
10
|
export function AuthFormShell(props) {
|
|
11
11
|
const { name, form, className, classNames, render, rootProps, aboveForm, succeeded, children, action, method, formRef, } = props;
|
|
12
|
-
return (_jsx(AuthFormClassNamesScope, { classNames: classNames, children: render ? (render(form)) : (_jsxs("div", { "data-oauth2-form": name, ...rootProps, className: combineClassNames(className, classNames.root), children: [aboveForm, succeeded ?? (_jsxs("form", { onSubmit: form.handleSubmit, action: action, method: method ??
|
|
12
|
+
return (_jsx(AuthFormClassNamesScope, { classNames: classNames, children: render ? (render(form)) : (_jsxs("div", { "data-oauth2-form": name, ...rootProps, className: combineClassNames(className, classNames.root), children: [aboveForm, succeeded ?? (_jsxs("form", { onSubmit: form.handleSubmit, action: action, method: method ?? "post", ref: formRef, noValidate: true, "data-oauth2-form-element": "", className: classNames.form, children: [_jsx(ErrorSummary, { error: form.formError, focusTrigger: form.submitCount }), children] }))] })) }));
|
|
13
13
|
}
|
|
14
14
|
function ignoreActivation(event) {
|
|
15
15
|
event.preventDefault();
|
|
@@ -20,9 +20,9 @@ export function SubmitButton(props) {
|
|
|
20
20
|
return (_jsx("div", { "data-oauth2-actions": "", className: classNames.actions, children: _jsx("button", { type: "submit", "aria-disabled": isSubmitting ? true : undefined, onClick: isSubmitting ? ignoreActivation : undefined, "data-oauth2-submit": "", className: classNames.submit, children: isSubmitting ? submittingLabel : label }) }));
|
|
21
21
|
}
|
|
22
22
|
export function TextField(props) {
|
|
23
|
-
const { fieldProps, label, type = "text", autoComplete, required, error, inputRef, children, } = props;
|
|
23
|
+
const { fieldProps, label, type = "text", autoComplete, required, minLength, error, inputRef, children, } = props;
|
|
24
24
|
const classNames = useAuthFormClassNames();
|
|
25
|
-
return (_jsxs("div", { "data-oauth2-field": fieldProps.name, className: classNames.field, children: [_jsx("label", { htmlFor: fieldProps.id, "data-oauth2-label": "", className: classNames.label, children: label }), _jsx("input", { ...fieldProps, type: type, autoComplete: autoComplete, required: required, "data-oauth2-input": "", className: classNames.input, ref: inputRef }), children, error ? (_jsx("p", { id: `${fieldProps.id}-error`, role: "alert", "data-oauth2-error": "", className: classNames.error, children: error })) : null] }));
|
|
25
|
+
return (_jsxs("div", { "data-oauth2-field": fieldProps.name, className: classNames.field, children: [_jsx("label", { htmlFor: fieldProps.id, "data-oauth2-label": "", className: classNames.label, children: label }), _jsx("input", { ...fieldProps, type: type, autoComplete: autoComplete, required: required, minLength: minLength, "data-oauth2-input": "", className: classNames.input, ref: inputRef }), children, error ? (_jsx("p", { id: `${fieldProps.id}-error`, role: "alert", "data-oauth2-error": "", className: classNames.error, children: error })) : null] }));
|
|
26
26
|
}
|
|
27
27
|
export function ErrorSummary(props) {
|
|
28
28
|
const { error, focusTrigger } = props;
|