@softure-ai/auth 0.0.0-stage → 0.1.5

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.
Files changed (239) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +381 -2
  3. package/dist/contract.d.ts +44 -0
  4. package/dist/contract.d.ts.map +1 -0
  5. package/dist/contract.js +2 -0
  6. package/dist/contract.js.map +1 -0
  7. package/dist/index.d.ts +188 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +84 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/mailing/index.d.ts +2 -0
  12. package/dist/mailing/index.d.ts.map +1 -0
  13. package/dist/mailing/index.js +4 -0
  14. package/dist/mailing/index.js.map +1 -0
  15. package/dist/mailing/reset-mail.d.ts +27 -0
  16. package/dist/mailing/reset-mail.d.ts.map +1 -0
  17. package/dist/mailing/reset-mail.js +55 -0
  18. package/dist/mailing/reset-mail.js.map +1 -0
  19. package/dist/messages/en.d.ts +97 -0
  20. package/dist/messages/en.d.ts.map +1 -0
  21. package/dist/messages/en.js +92 -0
  22. package/dist/messages/en.js.map +1 -0
  23. package/dist/messages/index.d.ts +199 -0
  24. package/dist/messages/index.d.ts.map +1 -0
  25. package/dist/messages/index.js +11 -0
  26. package/dist/messages/index.js.map +1 -0
  27. package/dist/messages/pl.d.ts +3 -0
  28. package/dist/messages/pl.d.ts.map +1 -0
  29. package/dist/messages/pl.js +91 -0
  30. package/dist/messages/pl.js.map +1 -0
  31. package/dist/next/actions.d.ts +19 -0
  32. package/dist/next/actions.d.ts.map +1 -0
  33. package/dist/next/actions.js +210 -0
  34. package/dist/next/actions.js.map +1 -0
  35. package/dist/next/context.d.ts +4 -0
  36. package/dist/next/context.d.ts.map +1 -0
  37. package/dist/next/context.js +13 -0
  38. package/dist/next/context.js.map +1 -0
  39. package/dist/next/current-user.d.ts +34 -0
  40. package/dist/next/current-user.d.ts.map +1 -0
  41. package/dist/next/current-user.js +77 -0
  42. package/dist/next/current-user.js.map +1 -0
  43. package/dist/next/index.d.ts +8 -0
  44. package/dist/next/index.d.ts.map +1 -0
  45. package/dist/next/index.js +9 -0
  46. package/dist/next/index.js.map +1 -0
  47. package/dist/next/logout-button.d.ts +7 -0
  48. package/dist/next/logout-button.d.ts.map +1 -0
  49. package/dist/next/logout-button.js +12 -0
  50. package/dist/next/logout-button.js.map +1 -0
  51. package/dist/next/messages.d.ts +5 -0
  52. package/dist/next/messages.d.ts.map +1 -0
  53. package/dist/next/messages.js +7 -0
  54. package/dist/next/messages.js.map +1 -0
  55. package/dist/next/pages.d.ts +18 -0
  56. package/dist/next/pages.d.ts.map +1 -0
  57. package/dist/next/pages.js +92 -0
  58. package/dist/next/pages.js.map +1 -0
  59. package/dist/next/params.d.ts +3 -0
  60. package/dist/next/params.d.ts.map +1 -0
  61. package/dist/next/params.js +4 -0
  62. package/dist/next/params.js.map +1 -0
  63. package/dist/next/route.d.ts +3 -0
  64. package/dist/next/route.d.ts.map +1 -0
  65. package/dist/next/route.js +17 -0
  66. package/dist/next/route.js.map +1 -0
  67. package/dist/next/search-params.d.ts +5 -0
  68. package/dist/next/search-params.d.ts.map +1 -0
  69. package/dist/next/search-params.js +13 -0
  70. package/dist/next/search-params.js.map +1 -0
  71. package/dist/next/session-cookie.d.ts +8 -0
  72. package/dist/next/session-cookie.d.ts.map +1 -0
  73. package/dist/next/session-cookie.js +34 -0
  74. package/dist/next/session-cookie.js.map +1 -0
  75. package/dist/options.d.ts +63 -0
  76. package/dist/options.d.ts.map +1 -0
  77. package/dist/options.js +74 -0
  78. package/dist/options.js.map +1 -0
  79. package/dist/password-reset-sender.d.ts +21 -0
  80. package/dist/password-reset-sender.d.ts.map +1 -0
  81. package/dist/password-reset-sender.js +12 -0
  82. package/dist/password-reset-sender.js.map +1 -0
  83. package/dist/proxy/index.d.ts +9 -0
  84. package/dist/proxy/index.d.ts.map +1 -0
  85. package/dist/proxy/index.js +41 -0
  86. package/dist/proxy/index.js.map +1 -0
  87. package/dist/redirect-target.d.ts +7 -0
  88. package/dist/redirect-target.d.ts.map +1 -0
  89. package/dist/redirect-target.js +25 -0
  90. package/dist/redirect-target.js.map +1 -0
  91. package/dist/roles.d.ts +5 -0
  92. package/dist/roles.d.ts.map +1 -0
  93. package/dist/roles.js +6 -0
  94. package/dist/roles.js.map +1 -0
  95. package/dist/safe-next-path.d.ts +3 -0
  96. package/dist/safe-next-path.d.ts.map +1 -0
  97. package/dist/safe-next-path.js +19 -0
  98. package/dist/safe-next-path.js.map +1 -0
  99. package/dist/schema.d.ts +302 -0
  100. package/dist/schema.d.ts.map +1 -0
  101. package/dist/schema.js +37 -0
  102. package/dist/schema.js.map +1 -0
  103. package/dist/scripts/index.d.ts +2 -0
  104. package/dist/scripts/index.d.ts.map +1 -0
  105. package/dist/scripts/index.js +3 -0
  106. package/dist/scripts/index.js.map +1 -0
  107. package/dist/scripts/role-scripts.d.ts +15 -0
  108. package/dist/scripts/role-scripts.d.ts.map +1 -0
  109. package/dist/scripts/role-scripts.js +76 -0
  110. package/dist/scripts/role-scripts.js.map +1 -0
  111. package/dist/server/change-password.d.ts +14 -0
  112. package/dist/server/change-password.d.ts.map +1 -0
  113. package/dist/server/change-password.js +47 -0
  114. package/dist/server/change-password.js.map +1 -0
  115. package/dist/server/health.d.ts +3 -0
  116. package/dist/server/health.d.ts.map +1 -0
  117. package/dist/server/health.js +15 -0
  118. package/dist/server/health.js.map +1 -0
  119. package/dist/server/index.d.ts +12 -0
  120. package/dist/server/index.d.ts.map +1 -0
  121. package/dist/server/index.js +14 -0
  122. package/dist/server/index.js.map +1 -0
  123. package/dist/server/login.d.ts +14 -0
  124. package/dist/server/login.d.ts.map +1 -0
  125. package/dist/server/login.js +49 -0
  126. package/dist/server/login.js.map +1 -0
  127. package/dist/server/options.d.ts +17 -0
  128. package/dist/server/options.d.ts.map +1 -0
  129. package/dist/server/options.js +36 -0
  130. package/dist/server/options.js.map +1 -0
  131. package/dist/server/password-reset.d.ts +54 -0
  132. package/dist/server/password-reset.d.ts.map +1 -0
  133. package/dist/server/password-reset.js +134 -0
  134. package/dist/server/password-reset.js.map +1 -0
  135. package/dist/server/password.d.ts +18 -0
  136. package/dist/server/password.d.ts.map +1 -0
  137. package/dist/server/password.js +77 -0
  138. package/dist/server/password.js.map +1 -0
  139. package/dist/server/privacy.d.ts +33 -0
  140. package/dist/server/privacy.d.ts.map +1 -0
  141. package/dist/server/privacy.js +52 -0
  142. package/dist/server/privacy.js.map +1 -0
  143. package/dist/server/rate-limits.d.ts +21 -0
  144. package/dist/server/rate-limits.d.ts.map +1 -0
  145. package/dist/server/rate-limits.js +39 -0
  146. package/dist/server/rate-limits.js.map +1 -0
  147. package/dist/server/reauthenticate.d.ts +7 -0
  148. package/dist/server/reauthenticate.d.ts.map +1 -0
  149. package/dist/server/reauthenticate.js +16 -0
  150. package/dist/server/reauthenticate.js.map +1 -0
  151. package/dist/server/register.d.ts +21 -0
  152. package/dist/server/register.d.ts.map +1 -0
  153. package/dist/server/register.js +52 -0
  154. package/dist/server/register.js.map +1 -0
  155. package/dist/server/roles.d.ts +25 -0
  156. package/dist/server/roles.d.ts.map +1 -0
  157. package/dist/server/roles.js +56 -0
  158. package/dist/server/roles.js.map +1 -0
  159. package/dist/server/session-token.d.ts +8 -0
  160. package/dist/server/session-token.d.ts.map +1 -0
  161. package/dist/server/session-token.js +18 -0
  162. package/dist/server/session-token.js.map +1 -0
  163. package/dist/server/sessions.d.ts +15 -0
  164. package/dist/server/sessions.d.ts.map +1 -0
  165. package/dist/server/sessions.js +41 -0
  166. package/dist/server/sessions.js.map +1 -0
  167. package/dist/server/switches.d.ts +9 -0
  168. package/dist/server/switches.d.ts.map +1 -0
  169. package/dist/server/switches.js +34 -0
  170. package/dist/server/switches.js.map +1 -0
  171. package/dist/server/validation.d.ts +10 -0
  172. package/dist/server/validation.d.ts.map +1 -0
  173. package/dist/server/validation.js +32 -0
  174. package/dist/server/validation.js.map +1 -0
  175. package/dist/session-cookie.d.ts +14 -0
  176. package/dist/session-cookie.d.ts.map +1 -0
  177. package/dist/session-cookie.js +31 -0
  178. package/dist/session-cookie.js.map +1 -0
  179. package/dist/ui/auth-forms.d.ts +57 -0
  180. package/dist/ui/auth-forms.d.ts.map +1 -0
  181. package/dist/ui/auth-forms.js +65 -0
  182. package/dist/ui/auth-forms.js.map +1 -0
  183. package/dist/ui/index.d.ts +2 -0
  184. package/dist/ui/index.d.ts.map +1 -0
  185. package/dist/ui/index.js +4 -0
  186. package/dist/ui/index.js.map +1 -0
  187. package/migrations/0001_create_users_and_sessions.sql +25 -0
  188. package/migrations/0002_create_user_roles.sql +10 -0
  189. package/migrations/0003_create_password_resets.sql +15 -0
  190. package/migrations/0004_index_users_created_at.sql +5 -0
  191. package/module.json +34 -0
  192. package/package.json +85 -4
  193. package/src/contract.ts +64 -0
  194. package/src/index.ts +100 -0
  195. package/src/mailing/index.ts +3 -0
  196. package/src/mailing/reset-mail.ts +73 -0
  197. package/src/messages/en.ts +91 -0
  198. package/src/messages/index.ts +15 -0
  199. package/src/messages/pl.ts +92 -0
  200. package/src/next/actions.ts +216 -0
  201. package/src/next/context.ts +14 -0
  202. package/src/next/current-user.ts +90 -0
  203. package/src/next/index.ts +24 -0
  204. package/src/next/logout-button.tsx +22 -0
  205. package/src/next/messages.ts +9 -0
  206. package/src/next/next-modules.d.ts +16 -0
  207. package/src/next/pages.tsx +179 -0
  208. package/src/next/params.ts +4 -0
  209. package/src/next/route.ts +17 -0
  210. package/src/next/search-params.ts +14 -0
  211. package/src/next/session-cookie.ts +39 -0
  212. package/src/options.ts +108 -0
  213. package/src/password-reset-sender.ts +33 -0
  214. package/src/proxy/index.ts +58 -0
  215. package/src/redirect-target.ts +24 -0
  216. package/src/roles.ts +7 -0
  217. package/src/safe-next-path.ts +15 -0
  218. package/src/schema.ts +45 -0
  219. package/src/scripts/index.ts +2 -0
  220. package/src/scripts/role-scripts.ts +98 -0
  221. package/src/server/change-password.ts +61 -0
  222. package/src/server/health.ts +16 -0
  223. package/src/server/index.ts +38 -0
  224. package/src/server/login.ts +61 -0
  225. package/src/server/options.ts +49 -0
  226. package/src/server/password-reset.ts +164 -0
  227. package/src/server/password.ts +90 -0
  228. package/src/server/privacy.ts +70 -0
  229. package/src/server/rate-limits.ts +44 -0
  230. package/src/server/reauthenticate.ts +16 -0
  231. package/src/server/register.ts +72 -0
  232. package/src/server/roles.ts +72 -0
  233. package/src/server/session-token.ts +21 -0
  234. package/src/server/sessions.ts +50 -0
  235. package/src/server/switches.ts +31 -0
  236. package/src/server/validation.ts +37 -0
  237. package/src/session-cookie.ts +47 -0
  238. package/src/ui/auth-forms.tsx +364 -0
  239. package/src/ui/index.ts +16 -0
@@ -0,0 +1,364 @@
1
+ "use client";
2
+
3
+ import { formatMessage, type Locale } from "@softure-ai/core";
4
+ import { Button, Checkbox, type ClassNames, createSlotClassGetter, FormError, PasswordField, TextField } from "@softure-ai/ui";
5
+ import { type ReactNode, useActionState } from "react";
6
+ import { type AuthFormField, type AuthFormState, INITIAL_AUTH_FORM_STATE } from "../contract.js";
7
+ import { type AuthMessages, getAuthErrorMessage } from "../messages/index.js";
8
+
9
+ // The auth forms. Each submits straight to its server action through `useActionState`, so it
10
+ // works before (and without) JavaScript, and a server-side redirect after login is followed.
11
+ // Copy comes from the module's messages; styling only from @softure-ai/ui classes and tokens.
12
+
13
+ export type AuthFormAction = (previous: AuthFormState, formData: FormData) => Promise<AuthFormState>;
14
+
15
+ export type AuthFormSlot = "root" | "form" | "footer" | "link" | "notice";
16
+
17
+ interface AuthFormBaseProps {
18
+ readonly action: AuthFormAction;
19
+ readonly messages: AuthMessages;
20
+ /** Locale of the built-in copy of the ui primitives. */
21
+ readonly locale?: Locale;
22
+ readonly classNames?: ClassNames<AuthFormSlot>;
23
+ readonly unstyled?: boolean;
24
+ }
25
+
26
+ const DEFAULT_CLASSES: Readonly<Record<AuthFormSlot, string>> = {
27
+ root: "sft:flex sft:flex-col sft:gap-3 sft:font-sans",
28
+ form: "sft:flex sft:flex-col sft:gap-3",
29
+ footer: "sft:m-0 sft:text-sm sft:text-muted",
30
+ link: "sft:font-medium sft:text-accent",
31
+ notice: "sft:m-0 sft:text-sm sft:text-success",
32
+ };
33
+
34
+ /** The error of one field, or of the form when no field owns it. */
35
+ function useErrors(state: AuthFormState, messages: AuthMessages) {
36
+ const message = state.error === undefined ? undefined : getAuthErrorMessage(messages, state.error);
37
+ return {
38
+ at: (field: AuthFormField) => (state.field === field ? message : undefined),
39
+ form: state.field === undefined ? message : undefined,
40
+ };
41
+ }
42
+
43
+ function FormShell({
44
+ formAction,
45
+ isPending,
46
+ submitLabel,
47
+ pendingLabel,
48
+ formError,
49
+ footer,
50
+ slot,
51
+ unstyled,
52
+ children,
53
+ }: {
54
+ formAction: (formData: FormData) => void;
55
+ isPending: boolean;
56
+ submitLabel: string;
57
+ pendingLabel: string;
58
+ formError: string | undefined;
59
+ footer?: ReactNode;
60
+ slot: (part: AuthFormSlot) => string | undefined;
61
+ unstyled: boolean | undefined;
62
+ children: ReactNode;
63
+ }) {
64
+ return (
65
+ <div className={slot("root")}>
66
+ <form action={formAction} className={slot("form")}>
67
+ {children}
68
+ <FormError message={formError} unstyled={unstyled} />
69
+ <Button type="submit" variant="primary" fullWidth pending={isPending} unstyled={unstyled}>
70
+ {isPending ? pendingLabel : submitLabel}
71
+ </Button>
72
+ </form>
73
+ {footer}
74
+ </div>
75
+ );
76
+ }
77
+
78
+ export interface LoginFormProps extends AuthFormBaseProps {
79
+ /** Where to go after login; checked again on the server. */
80
+ readonly next?: string;
81
+ /** Link to the register page; omitted when registration is not offered. */
82
+ readonly registerHref?: string;
83
+ /** Link to the password reset request page; omitted when reset is not offered. */
84
+ readonly forgotPasswordHref?: string;
85
+ /** A confirmation shown under the form, e.g. after a password reset. */
86
+ readonly notice?: string;
87
+ }
88
+
89
+ export function LoginForm({ action, messages, locale, next, registerHref, forgotPasswordHref, notice, classNames, unstyled }: LoginFormProps) {
90
+ const [state, formAction, isPending] = useActionState(action, INITIAL_AUTH_FORM_STATE);
91
+ const errors = useErrors(state, messages);
92
+ const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
93
+ const footer =
94
+ registerHref === undefined && forgotPasswordHref === undefined && notice === undefined ? undefined : (
95
+ <>
96
+ {notice === undefined ? null : (
97
+ <p role="status" className={slot("notice")}>
98
+ {notice}
99
+ </p>
100
+ )}
101
+ {forgotPasswordHref === undefined ? null : (
102
+ <p className={slot("footer")}>
103
+ <a href={forgotPasswordHref} className={slot("link")}>
104
+ {messages.login.forgotPassword}
105
+ </a>
106
+ </p>
107
+ )}
108
+ {registerHref === undefined ? null : (
109
+ <p className={slot("footer")}>
110
+ {messages.login.noAccount}{" "}
111
+ <a href={registerHref} className={slot("link")}>
112
+ {messages.login.registerLink}
113
+ </a>
114
+ </p>
115
+ )}
116
+ </>
117
+ );
118
+ return (
119
+ <FormShell
120
+ formAction={formAction}
121
+ isPending={isPending}
122
+ submitLabel={messages.login.submit}
123
+ pendingLabel={messages.login.pending}
124
+ formError={errors.form}
125
+ footer={footer}
126
+ slot={slot}
127
+ unstyled={unstyled}
128
+ >
129
+ {next === undefined ? null : <input type="hidden" name="next" value={next} />}
130
+ <TextField
131
+ name="email"
132
+ type="email"
133
+ label={messages.fields.email}
134
+ autoComplete="email"
135
+ required
136
+ defaultValue={state.email ?? ""}
137
+ error={errors.at("email")}
138
+ unstyled={unstyled}
139
+ locale={locale}
140
+ />
141
+ <PasswordField
142
+ name="password"
143
+ label={messages.fields.password}
144
+ autoComplete="current-password"
145
+ error={errors.at("password")}
146
+ unstyled={unstyled}
147
+ locale={locale}
148
+ />
149
+ </FormShell>
150
+ );
151
+ }
152
+
153
+ export interface RegisterFormProps extends AuthFormBaseProps {
154
+ readonly next?: string;
155
+ readonly loginHref?: string;
156
+ /** Show the consent checkbox (the module's `requireConsent`). */
157
+ readonly requireConsent: boolean;
158
+ /** The password policy's minimum, for the hint and the browser check. */
159
+ readonly minPasswordLength: number;
160
+ /** The consent statement; may contain links to the terms. Defaults to `messages.fields.consent`. */
161
+ readonly consentLabel?: ReactNode;
162
+ }
163
+
164
+ export function RegisterForm({
165
+ action,
166
+ messages,
167
+ locale,
168
+ next,
169
+ loginHref,
170
+ requireConsent,
171
+ minPasswordLength,
172
+ consentLabel,
173
+ classNames,
174
+ unstyled,
175
+ }: RegisterFormProps) {
176
+ const [state, formAction, isPending] = useActionState(action, INITIAL_AUTH_FORM_STATE);
177
+ const errors = useErrors(state, messages);
178
+ const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
179
+ const footer =
180
+ loginHref === undefined ? undefined : (
181
+ <p className={slot("footer")}>
182
+ {messages.register.hasAccount}{" "}
183
+ <a href={loginHref} className={slot("link")}>
184
+ {messages.register.loginLink}
185
+ </a>
186
+ </p>
187
+ );
188
+ return (
189
+ <FormShell
190
+ formAction={formAction}
191
+ isPending={isPending}
192
+ submitLabel={messages.register.submit}
193
+ pendingLabel={messages.register.pending}
194
+ formError={errors.form}
195
+ footer={footer}
196
+ slot={slot}
197
+ unstyled={unstyled}
198
+ >
199
+ {next === undefined ? null : <input type="hidden" name="next" value={next} />}
200
+ <TextField
201
+ name="email"
202
+ type="email"
203
+ label={messages.fields.email}
204
+ autoComplete="email"
205
+ required
206
+ defaultValue={state.email ?? ""}
207
+ error={errors.at("email")}
208
+ unstyled={unstyled}
209
+ locale={locale}
210
+ />
211
+ <PasswordField
212
+ name="password"
213
+ label={messages.fields.password}
214
+ hint={formatMessage(messages.fields.newPasswordHint, { minLength: minPasswordLength })}
215
+ autoComplete="new-password"
216
+ minLength={minPasswordLength}
217
+ error={errors.at("password")}
218
+ unstyled={unstyled}
219
+ locale={locale}
220
+ />
221
+ {requireConsent ? (
222
+ <Checkbox name="consent" label={consentLabel ?? messages.fields.consent} required error={errors.at("consent")} unstyled={unstyled} />
223
+ ) : null}
224
+ </FormShell>
225
+ );
226
+ }
227
+
228
+ export interface ChangePasswordFormProps extends AuthFormBaseProps {
229
+ readonly minPasswordLength: number;
230
+ }
231
+
232
+ export function ChangePasswordForm({ action, messages, locale, minPasswordLength, classNames, unstyled }: ChangePasswordFormProps) {
233
+ const [state, formAction, isPending] = useActionState(action, INITIAL_AUTH_FORM_STATE);
234
+ const errors = useErrors(state, messages);
235
+ const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
236
+ const notice =
237
+ state.status === "ok" ? (
238
+ <p role="status" className={slot("notice")}>
239
+ {messages.changePassword.success}
240
+ </p>
241
+ ) : undefined;
242
+ return (
243
+ <FormShell
244
+ formAction={formAction}
245
+ isPending={isPending}
246
+ submitLabel={messages.changePassword.submit}
247
+ pendingLabel={messages.changePassword.pending}
248
+ formError={errors.form}
249
+ footer={notice}
250
+ slot={slot}
251
+ unstyled={unstyled}
252
+ >
253
+ <PasswordField
254
+ name="currentPassword"
255
+ label={messages.fields.currentPassword}
256
+ autoComplete="current-password"
257
+ error={errors.at("currentPassword")}
258
+ unstyled={unstyled}
259
+ locale={locale}
260
+ />
261
+ <PasswordField
262
+ name="newPassword"
263
+ label={messages.fields.newPassword}
264
+ hint={formatMessage(messages.fields.newPasswordHint, { minLength: minPasswordLength })}
265
+ autoComplete="new-password"
266
+ minLength={minPasswordLength}
267
+ error={errors.at("newPassword")}
268
+ unstyled={unstyled}
269
+ locale={locale}
270
+ />
271
+ </FormShell>
272
+ );
273
+ }
274
+
275
+ export interface ForgotPasswordFormProps extends AuthFormBaseProps {
276
+ /** How long a link works, for the confirmation. */
277
+ readonly ttlMinutes: number;
278
+ readonly loginHref?: string;
279
+ }
280
+
281
+ /** Asks for a reset link. Its confirmation is the same whether or not the email has an account. */
282
+ export function ForgotPasswordForm({ action, messages, locale, ttlMinutes, loginHref, classNames, unstyled }: ForgotPasswordFormProps) {
283
+ const [state, formAction, isPending] = useActionState(action, INITIAL_AUTH_FORM_STATE);
284
+ const errors = useErrors(state, messages);
285
+ const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
286
+ const backToLogin =
287
+ loginHref === undefined ? undefined : (
288
+ <p className={slot("footer")}>
289
+ <a href={loginHref} className={slot("link")}>
290
+ {messages.forgotPassword.backToLogin}
291
+ </a>
292
+ </p>
293
+ );
294
+ if (state.status === "ok") {
295
+ return (
296
+ <div className={slot("root")}>
297
+ <p role="status" className={slot("notice")}>
298
+ {formatMessage(messages.forgotPassword.sent, { ttlMinutes })}
299
+ </p>
300
+ {backToLogin}
301
+ </div>
302
+ );
303
+ }
304
+ return (
305
+ <FormShell
306
+ formAction={formAction}
307
+ isPending={isPending}
308
+ submitLabel={messages.forgotPassword.submit}
309
+ pendingLabel={messages.forgotPassword.pending}
310
+ formError={errors.form}
311
+ footer={backToLogin}
312
+ slot={slot}
313
+ unstyled={unstyled}
314
+ >
315
+ <TextField
316
+ name="email"
317
+ type="email"
318
+ label={messages.fields.email}
319
+ autoComplete="email"
320
+ required
321
+ defaultValue={state.email ?? ""}
322
+ error={errors.at("email")}
323
+ unstyled={unstyled}
324
+ locale={locale}
325
+ />
326
+ </FormShell>
327
+ );
328
+ }
329
+
330
+ export interface ResetPasswordFormProps extends AuthFormBaseProps {
331
+ /** The token from the reset link; checked again on the server. */
332
+ readonly token: string;
333
+ readonly minPasswordLength: number;
334
+ }
335
+
336
+ /** Sets a new password from a reset link; its action goes to the login page on success. */
337
+ export function ResetPasswordForm({ action, messages, locale, token, minPasswordLength, classNames, unstyled }: ResetPasswordFormProps) {
338
+ const [state, formAction, isPending] = useActionState(action, INITIAL_AUTH_FORM_STATE);
339
+ const errors = useErrors(state, messages);
340
+ const slot = createSlotClassGetter({ defaults: DEFAULT_CLASSES, classNames, unstyled });
341
+ return (
342
+ <FormShell
343
+ formAction={formAction}
344
+ isPending={isPending}
345
+ submitLabel={messages.resetPassword.submit}
346
+ pendingLabel={messages.resetPassword.pending}
347
+ formError={errors.form}
348
+ slot={slot}
349
+ unstyled={unstyled}
350
+ >
351
+ <input type="hidden" name="token" value={token} />
352
+ <PasswordField
353
+ name="newPassword"
354
+ label={messages.fields.newPassword}
355
+ hint={formatMessage(messages.fields.newPasswordHint, { minLength: minPasswordLength })}
356
+ autoComplete="new-password"
357
+ minLength={minPasswordLength}
358
+ error={errors.at("newPassword")}
359
+ unstyled={unstyled}
360
+ locale={locale}
361
+ />
362
+ </FormShell>
363
+ );
364
+ }
@@ -0,0 +1,16 @@
1
+ // The auth forms, to compose into the app's own pages. They take the server actions from
2
+ // `@softure-ai/auth/next` as props: no `next/*` import here (docs/02-module-standard.md §5).
3
+ export {
4
+ type AuthFormAction,
5
+ type AuthFormSlot,
6
+ ChangePasswordForm,
7
+ type ChangePasswordFormProps,
8
+ ForgotPasswordForm,
9
+ type ForgotPasswordFormProps,
10
+ LoginForm,
11
+ type LoginFormProps,
12
+ RegisterForm,
13
+ type RegisterFormProps,
14
+ ResetPasswordForm,
15
+ type ResetPasswordFormProps,
16
+ } from "./auth-forms.js";