@krak-stack/auth 0.2.27 → 0.2.29
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
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @krak-stack/auth
|
|
2
2
|
|
|
3
|
+
## 0.2.29
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Keep email-code send history in memory for the current sign-in attempt instead of local storage, so resend cooldowns do not carry across login/logout. Switching between password and email code reuses an unexpired code and preserves its countdown without sending duplicate emails. Track cooldowns separately for each email address, allow explicit resends after 60 seconds, and send a fresh code when the previous one has expired. Refreshing the email-code page starts a new countdown without automatically sending another email. Disable resend while a request is pending.
|
|
8
|
+
|
|
9
|
+
## 0.2.28
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Wait for authoritative session revalidation before navigating after password, email-code, email-verification, and two-factor authentication. This prevents host route guards from reading a cached anonymous session and sending a successful first login back to sign-in.
|
|
14
|
+
|
|
3
15
|
## 0.2.27
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
|
|
2
3
|
export declare const AUTH_CLIENT_CHANGE_EVENT = "krakstack-auth:change";
|
|
3
4
|
export declare const refreshOnAuthChange: <A>(atom: Atom.Atom<A>) => Atom.Atom<A>;
|
|
4
5
|
export declare const authSessionAtom: (arg: string | undefined) => Atom.Writable<AsyncResult.AsyncResult<{
|
|
@@ -126,6 +127,69 @@ export declare const authSessionAtom: (arg: string | undefined) => Atom.Writable
|
|
|
126
127
|
readonly updatedAt: Date;
|
|
127
128
|
};
|
|
128
129
|
} | null, 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>, unknown>>>;
|
|
130
|
+
export declare const refreshAuthSession: (registry: AtomRegistry.AtomRegistry, baseUrl?: string | undefined) => Effect.Effect<{
|
|
131
|
+
readonly session: {
|
|
132
|
+
readonly id: string;
|
|
133
|
+
readonly expiresAt: Date;
|
|
134
|
+
readonly token: string;
|
|
135
|
+
readonly createdAt: Date;
|
|
136
|
+
readonly updatedAt: Date;
|
|
137
|
+
readonly ipAddress?: string | null | undefined;
|
|
138
|
+
readonly userAgent?: string | null | undefined;
|
|
139
|
+
readonly userId: string;
|
|
140
|
+
readonly impersonatedBy?: string | null | undefined;
|
|
141
|
+
readonly impersonatedByOrganizationId?: string | null | undefined;
|
|
142
|
+
readonly activeOrganizationId?: string | null | undefined;
|
|
143
|
+
};
|
|
144
|
+
readonly user: {
|
|
145
|
+
readonly id: string;
|
|
146
|
+
readonly name: string;
|
|
147
|
+
readonly email: string;
|
|
148
|
+
readonly emailVerified: boolean;
|
|
149
|
+
readonly locale?: "en" | "fr" | undefined;
|
|
150
|
+
readonly image: string | null;
|
|
151
|
+
readonly metadata?: {
|
|
152
|
+
readonly emails?: readonly {
|
|
153
|
+
readonly email: string;
|
|
154
|
+
readonly translations: readonly {
|
|
155
|
+
readonly locale: "en" | "fr";
|
|
156
|
+
readonly label: string;
|
|
157
|
+
}[];
|
|
158
|
+
}[] | undefined;
|
|
159
|
+
readonly phones?: readonly {
|
|
160
|
+
readonly number: string;
|
|
161
|
+
readonly extension?: string | undefined;
|
|
162
|
+
readonly translations: readonly {
|
|
163
|
+
readonly locale: "en" | "fr";
|
|
164
|
+
readonly label: string;
|
|
165
|
+
}[];
|
|
166
|
+
}[] | undefined;
|
|
167
|
+
readonly websites?: readonly {
|
|
168
|
+
readonly url: string;
|
|
169
|
+
readonly translations: readonly {
|
|
170
|
+
readonly locale: "en" | "fr";
|
|
171
|
+
readonly label: string;
|
|
172
|
+
}[];
|
|
173
|
+
}[] | undefined;
|
|
174
|
+
readonly socials?: readonly {
|
|
175
|
+
readonly platform: "facebook" | "github" | "instagram" | "linkedin" | "tiktok" | "x" | "youtube";
|
|
176
|
+
readonly url: string;
|
|
177
|
+
readonly translations: readonly {
|
|
178
|
+
readonly locale: "en" | "fr";
|
|
179
|
+
readonly label: string;
|
|
180
|
+
}[];
|
|
181
|
+
}[] | undefined;
|
|
182
|
+
} | null | undefined;
|
|
183
|
+
readonly role: string | null;
|
|
184
|
+
readonly banned: boolean | null;
|
|
185
|
+
readonly banReason?: string | null | undefined;
|
|
186
|
+
readonly banExpires?: Date | null | undefined;
|
|
187
|
+
readonly twoFactorEnabled?: boolean | undefined;
|
|
188
|
+
readonly lastLoginMethod?: string | null | undefined;
|
|
189
|
+
readonly createdAt: Date;
|
|
190
|
+
readonly updatedAt: Date;
|
|
191
|
+
};
|
|
192
|
+
} | null, 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, never>;
|
|
129
193
|
export declare const updateUserLocaleAtom: (arg: string | undefined) => Atom.AtomResultFn<"en" | "fr", void, 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 | import("effect/Schema").SchemaError>;
|
|
130
194
|
export declare const authOrganizationsAtom: (arg: string | undefined) => Atom.Atom<AsyncResult.AsyncResult<readonly {
|
|
131
195
|
readonly id: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Option } from "effect";
|
|
2
|
-
import { AsyncResult, Atom } from "effect/reactivity";
|
|
2
|
+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
|
|
3
3
|
import { authClientApi, authHttpClient } from "./auth-client-api.js";
|
|
4
4
|
const LAST_LOGIN_METHOD_COOKIE = "krakstack-auth.last_used_login_method";
|
|
5
5
|
export const AUTH_CLIENT_CHANGE_EVENT = "krakstack-auth:change";
|
|
@@ -18,7 +18,7 @@ export const refreshOnAuthChange = (atom) => {
|
|
|
18
18
|
// The built-in visibility helper is browser-only.
|
|
19
19
|
return globalThis.window ? Atom.refreshOnWindowFocus(reactive) : reactive;
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
const authSessionQueryAtom = Atom.family((baseUrl) => authClientApi(baseUrl)
|
|
22
22
|
.query("auth", "getSession", {
|
|
23
23
|
query: {},
|
|
24
24
|
timeToLive: "1 minute",
|
|
@@ -30,7 +30,18 @@ export const authSessionAtom = Atom.family((baseUrl) => Atom.optimistic(authClie
|
|
|
30
30
|
? AsyncResult.success(null)
|
|
31
31
|
: result;
|
|
32
32
|
}))
|
|
33
|
-
.pipe(refreshOnAuthChange))
|
|
33
|
+
.pipe(refreshOnAuthChange));
|
|
34
|
+
export const authSessionAtom = Atom.family((baseUrl) => Atom.optimistic(authSessionQueryAtom(baseUrl)));
|
|
35
|
+
export const refreshAuthSession = Effect.fn("refreshAuthSession")(function* (registry, baseUrl) {
|
|
36
|
+
const query = authSessionQueryAtom(baseUrl);
|
|
37
|
+
notifyAuthChange();
|
|
38
|
+
registry.refresh(query);
|
|
39
|
+
// Wait on the authoritative query, not its optimistic view, which retains
|
|
40
|
+
// the previous success without a waiting flag during revalidation.
|
|
41
|
+
return yield* AtomRegistry.getResult(registry, query, {
|
|
42
|
+
suspendOnWaiting: true,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
34
45
|
export const updateUserLocaleAtom = Atom.family((baseUrl) => Atom.optimisticFn(authSessionAtom(baseUrl), {
|
|
35
46
|
reducer: (state, locale) => AsyncResult.map(state, (session) => session ? { ...session, user: { ...session.user, locale } } : session),
|
|
36
47
|
fn: authClientApi(baseUrl).runtime.fn((locale) => Effect.gen(function* () {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useAtomSet, useAtomSuspense, useAtomValue } from "@effect/atom-react";
|
|
2
|
+
import { RegistryContext, useAtomSet, useAtomSuspense, useAtomValue, } from "@effect/atom-react";
|
|
3
3
|
import { FormBuilder, FormReact } from "@lucas-barake/effect-form-react";
|
|
4
4
|
import { Link, useNavigate, useRouter, useRouterState, } from "@tanstack/react-router";
|
|
5
5
|
import { Effect, Option, Schema } from "effect";
|
|
@@ -7,16 +7,16 @@ import { Atom, AsyncResult } from "effect/reactivity";
|
|
|
7
7
|
import { HttpClientError } from "effect/http";
|
|
8
8
|
import { REGEXP_ONLY_DIGITS } from "input-otp";
|
|
9
9
|
import { ArrowLeft, KeyRound, Loader2, Mail } from "lucide-react";
|
|
10
|
-
import { useEffect, useRef, useState } from "react";
|
|
10
|
+
import { useContext, useEffect, useRef, useState } from "react";
|
|
11
11
|
import { Button, buttonVariants } from "@/components/ui/button";
|
|
12
12
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card";
|
|
13
13
|
import { CheckboxField, ErrorMessage, TextField, effectFormMessages, } from "@krak-stack/registry/effect-form";
|
|
14
14
|
import { Field, FieldLabel } from "@/components/ui/field";
|
|
15
15
|
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp";
|
|
16
|
-
import { getLastUsedLoginMethod, notifyAuthChange } from "./auth-atoms.js";
|
|
16
|
+
import { getLastUsedLoginMethod, notifyAuthChange, refreshAuthSession, } from "./auth-atoms.js";
|
|
17
17
|
import { authClientApi, authHttpClient } from "./auth-client-api.js";
|
|
18
18
|
import { PasswordField } from "./password-field.js";
|
|
19
|
-
import {
|
|
19
|
+
import { emailOtpResendSeconds as getEmailOtpResendSeconds, shouldSendEmailOtp, EMAIL_OTP_RESEND_COOLDOWN_SECONDS, } from "./email-otp-state.js";
|
|
20
20
|
import { passwordResetSearch, passwordResetConfirmation, passwordResetConfirmationSearch, passwordResetCallbackUrl, requestPasswordResetEmail, resetPasswordAndSignOut, } from "./password-reset.js";
|
|
21
21
|
import { PasswordResetActions, PasswordResetFeedback, usePasswordResetRequest, } from "./password-reset-flow.js";
|
|
22
22
|
import { useKrakstackAuth } from "./auth-provider.js";
|
|
@@ -270,6 +270,7 @@ export const resolveInitialAuthMethod = ({ requestedMethod, lastLoginMethod, ema
|
|
|
270
270
|
return emailOtp ? "emailOtp" : "password";
|
|
271
271
|
};
|
|
272
272
|
export function Signin(props) {
|
|
273
|
+
const registry = useContext(RegistryContext);
|
|
273
274
|
const { baseUrl, locale, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
274
275
|
const router = useRouter();
|
|
275
276
|
const navigate = useNavigate();
|
|
@@ -299,12 +300,19 @@ export function Signin(props) {
|
|
|
299
300
|
const [emailSubmitted, setEmailSubmitted] = useState(initialMethodStep);
|
|
300
301
|
const [authMethod, setAuthMethod] = useState(initialAuthMethod);
|
|
301
302
|
const [otpSentTo, setOtpSentTo] = useState(initialMethodStep && initialAuthMethod === "emailOtp" ? initialEmail : "");
|
|
302
|
-
const [emailOtpResendAvailableAt, setEmailOtpResendAvailableAt] = useState(
|
|
303
|
+
const [emailOtpResendAvailableAt, setEmailOtpResendAvailableAt] = useState(() => initialMethodStep && initialAuthMethod === "emailOtp"
|
|
304
|
+
? Date.now() + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000
|
|
305
|
+
: 0);
|
|
303
306
|
const [emailOtpResendSeconds, setEmailOtpResendSeconds] = useState(0);
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
307
|
+
const otpSendsRef = useRef(new Map(initialMethodStep && initialAuthMethod === "emailOtp"
|
|
308
|
+
? [
|
|
309
|
+
[
|
|
310
|
+
initialEmail,
|
|
311
|
+
emailOtpResendAvailableAt -
|
|
312
|
+
EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000,
|
|
313
|
+
],
|
|
314
|
+
]
|
|
315
|
+
: []));
|
|
308
316
|
const [errorContext, setErrorContext] = useState("signIn");
|
|
309
317
|
const selectedAuthMethod = authMethod === "password" && options.emailPassword
|
|
310
318
|
? "password"
|
|
@@ -319,7 +327,7 @@ export function Signin(props) {
|
|
|
319
327
|
return;
|
|
320
328
|
}
|
|
321
329
|
const updateResendSeconds = () => {
|
|
322
|
-
const seconds =
|
|
330
|
+
const seconds = getEmailOtpResendSeconds(emailOtpResendAvailableAt, Date.now());
|
|
323
331
|
setEmailOtpResendSeconds(seconds);
|
|
324
332
|
if (seconds === 0)
|
|
325
333
|
setEmailOtpResendAvailableAt(0);
|
|
@@ -347,23 +355,18 @@ export function Signin(props) {
|
|
|
347
355
|
}));
|
|
348
356
|
return;
|
|
349
357
|
}
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
(
|
|
354
|
-
|
|
355
|
-
const sendEmailOtp = (
|
|
356
|
-
|
|
357
|
-
:
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
[email]: Date.now(),
|
|
363
|
-
};
|
|
364
|
-
otpSendsRef.current = sends;
|
|
365
|
-
persistOtpSends(sends);
|
|
366
|
-
})))).pipe(Effect.tap(() => Effect.sync(() => {
|
|
358
|
+
const sendEmailOtpRequest = authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
|
|
359
|
+
payload: { email, type: "sign-in" },
|
|
360
|
+
})), Effect.asVoid, Effect.tap(() => Effect.sync(() => {
|
|
361
|
+
otpSendsRef.current.set(email, Date.now());
|
|
362
|
+
})));
|
|
363
|
+
const sendEmailOtp = (shouldSendEmailOtp({
|
|
364
|
+
sentAt: otpSendsRef.current.get(email),
|
|
365
|
+
now: Date.now(),
|
|
366
|
+
resend: action.type === "resendEmailOtp",
|
|
367
|
+
})
|
|
368
|
+
? sendEmailOtpRequest
|
|
369
|
+
: Effect.void).pipe(Effect.tap(() => Effect.sync(() => {
|
|
367
370
|
setAuthMethod("emailOtp");
|
|
368
371
|
setEmailSubmitted(true);
|
|
369
372
|
setOtpSentTo(email);
|
|
@@ -372,7 +375,7 @@ export function Signin(props) {
|
|
|
372
375
|
search: authMethodSearch(searchString, "emailOtp", email),
|
|
373
376
|
replace: true,
|
|
374
377
|
});
|
|
375
|
-
setEmailOtpResendAvailableAt((otpSendsRef.current
|
|
378
|
+
setEmailOtpResendAvailableAt((otpSendsRef.current.get(email) ?? 0) +
|
|
376
379
|
EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
377
380
|
})));
|
|
378
381
|
if (action.type === "sendEmailOtp" ||
|
|
@@ -405,7 +408,7 @@ export function Signin(props) {
|
|
|
405
408
|
name: nameFromEmail(email),
|
|
406
409
|
},
|
|
407
410
|
})));
|
|
408
|
-
|
|
411
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
409
412
|
onNavigate(redirectTarget);
|
|
410
413
|
return;
|
|
411
414
|
}
|
|
@@ -426,7 +429,7 @@ export function Signin(props) {
|
|
|
426
429
|
onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
|
|
427
430
|
return;
|
|
428
431
|
}
|
|
429
|
-
|
|
432
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
430
433
|
onNavigate(result.url ?? redirectTarget);
|
|
431
434
|
}),
|
|
432
435
|
}));
|
|
@@ -458,10 +461,6 @@ export function Signin(props) {
|
|
|
458
461
|
password: "",
|
|
459
462
|
otp: "",
|
|
460
463
|
}));
|
|
461
|
-
const lastOtpSentAt = otpSends[formValues.email.trim()];
|
|
462
|
-
useEffect(() => {
|
|
463
|
-
setEmailOtpResendAvailableAt((lastOtpSentAt ?? 0) + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
464
|
-
}, [lastOtpSentAt]);
|
|
465
464
|
const resetForm = useAtomSet(form.reset);
|
|
466
465
|
const setEmail = useAtomSet(form.getFieldAtoms(form.fields.email).setValue);
|
|
467
466
|
const setPassword = useAtomSet(form.getFieldAtoms(form.fields.password).setValue);
|
|
@@ -531,7 +530,7 @@ export function Signin(props) {
|
|
|
531
530
|
setErrorContext("resetPassword");
|
|
532
531
|
submit({ type: "resetPassword" });
|
|
533
532
|
},
|
|
534
|
-
}, required: true }) })) : null, emailSubmitted && selectedAuthMethod === "emailOtp" ? (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(form.otp, { label: m.sign_in_email_otp_code }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.sign_in_otp_expiry }), _jsx("div", { className: "flex flex-wrap gap-x-4 gap-y-2 text-sm", children: otpSentTo ? (_jsx(Button, { type: "button", variant: "link", className: "h-auto px-0", disabled: emailOtpResendSeconds > 0, onClick: resendEmailOtp, children: emailOtpResendSeconds > 0
|
|
533
|
+
}, required: true }) })) : null, emailSubmitted && selectedAuthMethod === "emailOtp" ? (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(form.otp, { label: m.sign_in_email_otp_code }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.sign_in_otp_expiry }), _jsx("div", { className: "flex flex-wrap gap-x-4 gap-y-2 text-sm", children: otpSentTo ? (_jsx(Button, { type: "button", variant: "link", className: "h-auto px-0", disabled: submitResult.waiting || emailOtpResendSeconds > 0, onClick: resendEmailOtp, children: emailOtpResendSeconds > 0
|
|
535
534
|
? text(m.sign_in_email_otp_resend_countdown, {
|
|
536
535
|
seconds: String(emailOtpResendSeconds),
|
|
537
536
|
})
|
|
@@ -565,6 +564,7 @@ export function Signin(props) {
|
|
|
565
564
|
: m.sign_in_use_password] })) : null, options.google && !emailSubmitted ? (_jsxs(_Fragment, { children: [_jsxs(Button, { type: "button", variant: "outline", className: "w-full", disabled: submitResult.waiting, onClick: signInWithGoogle, children: [_jsx(GoogleLogo, {}), m.auth_continue_with_google] }), _jsx(AuthSubmitError, { result: socialSignInResult, labels: m, messages: { fallback: m.sign_in_error } })] })) : null] })) : null, !hasPrimaryAuth && !options.google ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.oauth_client_no_auth_methods })) : null] })] }));
|
|
566
565
|
}
|
|
567
566
|
export function VerifyEmail(props) {
|
|
567
|
+
const registry = useContext(RegistryContext);
|
|
568
568
|
const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
|
|
569
569
|
const navigate = useNavigate();
|
|
570
570
|
const searchString = useRouterState({
|
|
@@ -595,7 +595,7 @@ export function VerifyEmail(props) {
|
|
|
595
595
|
otp: value.otp.trim(),
|
|
596
596
|
},
|
|
597
597
|
})));
|
|
598
|
-
|
|
598
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
599
599
|
onNavigate(redirectTarget);
|
|
600
600
|
}),
|
|
601
601
|
}));
|
|
@@ -702,6 +702,7 @@ function ResetPasswordForm({ token, ...props }) {
|
|
|
702
702
|
}, children: [_jsx(form.password, { label: m.reset_password_new_password, autoComplete: "new-password", minLength: 8, required: true }), _jsx(AuthSubmitError, { result: submitResult, labels: m, messages: { fallback: m.reset_password_error } }), _jsxs(Button, { type: "submit", disabled: submitResult.waiting, children: [submitResult.waiting ? (_jsx(Loader2, { className: "animate-spin", "data-icon": "inline-start" })) : null, effectFormMessages().submit] })] }) }) })] }));
|
|
703
703
|
}
|
|
704
704
|
export function TwoFactor(props) {
|
|
705
|
+
const registry = useContext(RegistryContext);
|
|
705
706
|
const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
|
|
706
707
|
const navigate = useNavigate();
|
|
707
708
|
const searchString = useRouterState({
|
|
@@ -743,7 +744,7 @@ export function TwoFactor(props) {
|
|
|
743
744
|
: client.auth.twoFactorVerifyTotp({
|
|
744
745
|
payload: verification,
|
|
745
746
|
})));
|
|
746
|
-
|
|
747
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
747
748
|
onNavigate(getResultRedirectUrl(result) ?? redirectTarget);
|
|
748
749
|
}),
|
|
749
750
|
}));
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { Atom } from "effect/reactivity";
|
|
2
1
|
export declare const EMAIL_OTP_RESEND_COOLDOWN_SECONDS = 60;
|
|
3
|
-
export declare const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
reusable: boolean;
|
|
10
|
-
resendSeconds: number;
|
|
11
|
-
};
|
|
2
|
+
export declare const emailOtpResendSeconds: (availableAt: number, now: number) => number;
|
|
3
|
+
export declare const shouldSendEmailOtp: ({ sentAt, now, resend, }: {
|
|
4
|
+
sentAt: number | undefined;
|
|
5
|
+
now: number;
|
|
6
|
+
resend: boolean;
|
|
7
|
+
}) => boolean;
|
|
@@ -1,25 +1,8 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
2
|
-
import { KeyValueStore } from "effect/persistence";
|
|
3
|
-
import { Atom } from "effect/reactivity";
|
|
4
1
|
export const EMAIL_OTP_RESEND_COOLDOWN_SECONDS = 60;
|
|
5
2
|
const EMAIL_OTP_LIFETIME_MS = 5 * 60 * 1000;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const emailOtpSendsAtom = Atom.family((baseUrl) => Atom.kvs({
|
|
13
|
-
runtime: storageRuntime,
|
|
14
|
-
key: `krakstack-auth:email-otp-sends:v1:${baseUrl}`,
|
|
15
|
-
schema: EmailOtpSends,
|
|
16
|
-
defaultValue: () => ({}),
|
|
17
|
-
}));
|
|
18
|
-
export const emailOtpSendState = (sentAt, now) => ({
|
|
19
|
-
reusable: sentAt !== undefined &&
|
|
20
|
-
now >= sentAt &&
|
|
21
|
-
now - sentAt < EMAIL_OTP_LIFETIME_MS,
|
|
22
|
-
resendSeconds: sentAt === undefined
|
|
23
|
-
? 0
|
|
24
|
-
: Math.max(0, Math.ceil((sentAt + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000 - now) / 1000)),
|
|
25
|
-
});
|
|
3
|
+
export const emailOtpResendSeconds = (availableAt, now) => Math.max(0, Math.ceil((availableAt - now) / 1000));
|
|
4
|
+
export const shouldSendEmailOtp = ({ sentAt, now, resend, }) => sentAt === undefined ||
|
|
5
|
+
now < sentAt ||
|
|
6
|
+
now - sentAt >= EMAIL_OTP_LIFETIME_MS ||
|
|
7
|
+
(resend &&
|
|
8
|
+
emailOtpResendSeconds(sentAt + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000, now) === 0);
|