@krak-stack/auth 0.2.22 → 0.2.24

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,51 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { RegistryContext, useAtomSet, useAtomValue } from "@effect/atom-react";
3
+ import { Effect } from "effect";
4
+ import { AtomRegistry } from "effect/reactivity";
5
+ import { Loader2 } from "lucide-react";
6
+ import { useContext, useEffect, useState } from "react";
7
+ import { Button } from "@/components/ui/button";
8
+ import { authClientApi } from "./auth-client-api.js";
9
+ import { PASSWORD_RESET_RESEND_COOLDOWN_MS, passwordResetResendSeconds, requestPasswordResetEmail, } from "./password-reset.js";
10
+ // The caller owns routing and confirmation visibility. A mounted flow never
11
+ // sends automatically, including when its initial timestamp came from a URL.
12
+ export const usePasswordResetRequest = (baseUrl, initialSentAt = null) => {
13
+ const [lastSentAt, setLastSentAt] = useState(null);
14
+ const [now, setNow] = useState(Date.now);
15
+ const [action] = useState(() => authClientApi(baseUrl).runtime.fn((input) => requestPasswordResetEmail({ baseUrl, ...input }).pipe(Effect.flatMap(() => Effect.sync(() => {
16
+ const sentAt = Date.now();
17
+ setLastSentAt(sentAt);
18
+ setNow(sentAt);
19
+ return sentAt;
20
+ })), Effect.tap((sentAt) => Effect.promise(async () => {
21
+ await input.onSent?.(sentAt);
22
+ })))));
23
+ const send = useAtomSet(action);
24
+ const result = useAtomValue(action);
25
+ const registry = useContext(RegistryContext);
26
+ const request = (input) => Effect.sync(() => registry.set(action, input)).pipe(Effect.flatMap(() => AtomRegistry.getResult(registry, action, { suspendOnWaiting: true })));
27
+ const sentAt = Math.max(initialSentAt ?? 0, lastSentAt ?? 0) || null;
28
+ useEffect(() => {
29
+ if (sentAt === null)
30
+ return;
31
+ const update = () => setNow(Date.now());
32
+ update();
33
+ if (Date.now() >= sentAt + PASSWORD_RESET_RESEND_COOLDOWN_MS)
34
+ return;
35
+ const timer = window.setInterval(() => {
36
+ update();
37
+ if (Date.now() >= sentAt + PASSWORD_RESET_RESEND_COOLDOWN_MS) {
38
+ window.clearInterval(timer);
39
+ }
40
+ }, 1000);
41
+ return () => window.clearInterval(timer);
42
+ }, [sentAt]);
43
+ return {
44
+ send,
45
+ request,
46
+ result,
47
+ resendSeconds: passwordResetResendSeconds(sentAt, now),
48
+ };
49
+ };
50
+ export const PasswordResetFeedback = ({ pending, sendingLabel, error, }) => pending ? (_jsx("p", { role: "status", className: "text-muted-foreground text-sm", children: sendingLabel })) : (_jsx(_Fragment, { children: error }));
51
+ export const PasswordResetActions = ({ pending, resendSeconds, onResend, resendLabel, countdownLabel, }) => (_jsx("div", { className: "flex flex-wrap gap-2", children: _jsxs(Button, { type: "button", disabled: pending || resendSeconds > 0, onClick: onResend, children: [pending ? (_jsx(Loader2, { className: "animate-spin", "data-icon": "inline-start" })) : null, resendSeconds > 0 ? countdownLabel(resendSeconds) : resendLabel] }) }));
@@ -0,0 +1,34 @@
1
+ import { Effect, Schema } from "effect";
2
+ import type { KrakstackAuthLocale } from "./auth-provider.js";
3
+ export declare const passwordResetSearch: (searchString: string) => {
4
+ [k: string]: string;
5
+ };
6
+ export declare const passwordResetConfirmationSearch: (searchString: string, email: string, sentAt: number) => {
7
+ method: string;
8
+ email: string;
9
+ resetSentAt: number;
10
+ };
11
+ export declare const passwordResetConfirmation: (searchString: string, now?: number) => {
12
+ readonly email: string;
13
+ readonly resetSentAt: number;
14
+ } | null;
15
+ export declare const passwordResetCallbackUrl: (searchString: string, origin: string, email?: string) => string;
16
+ export declare const PASSWORD_RESET_RESEND_COOLDOWN_MS = 60000;
17
+ export declare const passwordResetResendSeconds: (sentAt: number | null, now: number) => number;
18
+ export declare const requestPasswordResetEmail: ({ baseUrl, locale, email, redirectTo, }: {
19
+ baseUrl?: string | undefined;
20
+ locale: () => KrakstackAuthLocale;
21
+ email: string;
22
+ redirectTo: string;
23
+ }) => Effect.Effect<{
24
+ readonly status: boolean;
25
+ readonly message: string;
26
+ }, import("../auth/schema.js").AuthBadRequest | import("../auth/schema.js").AuthConflict | import("../auth/schema.js").AuthExpectationFailed | import("../auth/schema.js").AuthForbidden | import("../auth/schema.js").AuthInternalServerError | import("../auth/schema.js").AuthNotFound | import("../auth/schema.js").AuthTooManyRequests | import("../auth/schema.js").AuthUnauthorized | import("effect/http/HttpClientError").HttpClientError | Schema.SchemaError, never>;
27
+ export declare const resetPasswordAndSignOut: ({ baseUrl, locale, newPassword, token, }: {
28
+ baseUrl?: string | undefined;
29
+ locale: () => KrakstackAuthLocale;
30
+ newPassword: string;
31
+ token: string;
32
+ }) => Effect.Effect<{
33
+ readonly success: boolean;
34
+ }, import("../auth/schema.js").AuthBadRequest | import("../auth/schema.js").AuthConflict | import("../auth/schema.js").AuthExpectationFailed | import("../auth/schema.js").AuthForbidden | import("../auth/schema.js").AuthInternalServerError | import("../auth/schema.js").AuthNotFound | import("../auth/schema.js").AuthTooManyRequests | import("../auth/schema.js").AuthUnauthorized | import("effect/http/HttpClientError").HttpClientError | Schema.SchemaError, never>;
@@ -0,0 +1,41 @@
1
+ import { Effect, Option, Schema } from "effect";
2
+ import { authHttpClient } from "./auth-client-api.js";
3
+ import { EmailAddress } from "../schema.js";
4
+ export const passwordResetSearch = (searchString) => {
5
+ const search = new URLSearchParams(searchString);
6
+ for (const key of ["resetSentAt", "token", "error"])
7
+ search.delete(key);
8
+ return Object.fromEntries(search);
9
+ };
10
+ export const passwordResetConfirmationSearch = (searchString, email, sentAt) => ({
11
+ ...passwordResetSearch(searchString),
12
+ method: "password",
13
+ email,
14
+ resetSentAt: sentAt,
15
+ });
16
+ export const passwordResetConfirmation = (searchString, now = Date.now()) => {
17
+ const Timestamp = Schema.NumberFromString.check(Schema.isInt(), Schema.isBetween({ minimum: 1, maximum: now })).annotate({ identifier: "PasswordResetTimestamp" });
18
+ const Confirmation = Schema.Struct({
19
+ email: EmailAddress,
20
+ // Routers may JSON-quote numeric strings to preserve their string type.
21
+ resetSentAt: Schema.Union([Timestamp, Schema.fromJsonString(Timestamp)]),
22
+ }).annotate({ identifier: "PasswordResetConfirmation" });
23
+ return Option.getOrNull(Schema.decodeUnknownOption(Confirmation)(Object.fromEntries(new URLSearchParams(searchString))));
24
+ };
25
+ export const passwordResetCallbackUrl = (searchString, origin, email) => {
26
+ const url = new URL("/reset-password", origin);
27
+ url.search = new URLSearchParams(passwordResetSearch(searchString)).toString();
28
+ if (email !== undefined) {
29
+ url.searchParams.set("email", email);
30
+ url.searchParams.set("method", "password");
31
+ }
32
+ return url.href;
33
+ };
34
+ export const PASSWORD_RESET_RESEND_COOLDOWN_MS = 60_000;
35
+ export const passwordResetResendSeconds = (sentAt, now) => sentAt === null
36
+ ? 0
37
+ : Math.max(0, Math.ceil((sentAt + PASSWORD_RESET_RESEND_COOLDOWN_MS - now) / 1000));
38
+ export const requestPasswordResetEmail = ({ baseUrl, locale, email, redirectTo, }) => authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.requestPasswordReset({ payload: { email, redirectTo } })));
39
+ export const resetPasswordAndSignOut = ({ baseUrl, locale, newPassword, token, }) => authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.resetPassword({ payload: { newPassword, token } }).pipe(
40
+ // Server-side session revocation does not clear browser session cookies.
41
+ Effect.flatMap(() => client.auth.signOut({ payload: {} })))));
@@ -12,6 +12,13 @@ declare const messages: {
12
12
  readonly user_account_confirm_revoke: "Revoke account";
13
13
  readonly user_account_connected: "Connected";
14
14
  readonly user_account_current_password: "Current password";
15
+ readonly sign_in_forgot_password: "Forgot your password?";
16
+ readonly user_password_reset_title: "Check your email";
17
+ readonly user_password_reset_description: "We sent you a link to reset your password. After resetting, sign in again to continue.";
18
+ readonly user_password_reset_error: "Unable to send password reset email. Please try again.";
19
+ readonly user_password_reset_sending: "Sending reset email…";
20
+ readonly user_password_reset_resend: "Resend email";
21
+ readonly user_password_reset_countdown: "Resend in {seconds}s";
15
22
  readonly user_account_google_connect: "Set up";
16
23
  readonly user_account_google_description: "Use your Google account as a sign-in option.";
17
24
  readonly user_account_google_link_error: "Could not connect Google.";
@@ -108,6 +115,7 @@ declare const messages: {
108
115
  readonly user_contact_add_website: "Add website";
109
116
  readonly user_contact_description: "Add up to 8 public contacts of each type, separately from the email used to sign in.";
110
117
  readonly user_contact_email_type: "Email";
118
+ readonly user_contact_primary: "Primary";
111
119
  readonly user_contact_extension: "Extension";
112
120
  readonly user_contact_label: "Label";
113
121
  readonly user_contact_phone_type: "Phone";
@@ -149,6 +157,13 @@ declare const messages: {
149
157
  readonly user_account_confirm_revoke: "Révoquer le compte";
150
158
  readonly user_account_connected: "Connecté";
151
159
  readonly user_account_current_password: "Mot de passe actuel";
160
+ readonly sign_in_forgot_password: "Mot de passe oublié?";
161
+ readonly user_password_reset_title: "Consultez votre courriel";
162
+ readonly user_password_reset_description: "Nous vous avons envoyé un lien pour réinitialiser votre mot de passe. Après la réinitialisation, reconnectez-vous pour continuer.";
163
+ readonly user_password_reset_error: "Impossible d’envoyer le courriel de réinitialisation. Veuillez réessayer.";
164
+ readonly user_password_reset_sending: "Envoi du courriel de réinitialisation…";
165
+ readonly user_password_reset_resend: "Renvoyer le courriel";
166
+ readonly user_password_reset_countdown: "Renvoyer dans {seconds} s";
152
167
  readonly user_account_google_connect: "Configurer";
153
168
  readonly user_account_google_description: "Utilisez votre compte Google comme option de connexion.";
154
169
  readonly user_account_google_link_error: "Impossible de connecter Google.";
@@ -245,6 +260,7 @@ declare const messages: {
245
260
  readonly user_contact_add_website: "Ajouter un site Web";
246
261
  readonly user_contact_description: "Ajoutez jusqu’à 8 coordonnées publiques de chaque type, distinctes du courriel utilisé pour vous connecter.";
247
262
  readonly user_contact_email_type: "Courriel";
263
+ readonly user_contact_primary: "Principal";
248
264
  readonly user_contact_extension: "Poste";
249
265
  readonly user_contact_label: "Libellé";
250
266
  readonly user_contact_phone_type: "Téléphone";
@@ -288,6 +304,8 @@ type UserFormType = {
288
304
  websites: ReadonlyArray<ContactWebsiteValue>;
289
305
  socials: ReadonlyArray<ContactSocialValue>;
290
306
  };
307
+ export declare const userContactEmailDefaults: (metadata: UserMetadata, email: string) => ContactEmailValue[];
308
+ export declare const userPrimaryContactEmail: (email: string) => ContactEmailValue;
291
309
  export declare const UserContactEmailsForm: Schema.$Array<Schema.Struct<{
292
310
  readonly email: Schema.String;
293
311
  readonly translations: Schema.$Array<Schema.Struct<{