@krak-stack/auth 0.2.27 → 0.2.28
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,11 @@
|
|
|
1
1
|
# @krak-stack/auth
|
|
2
2
|
|
|
3
|
+
## 0.2.28
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 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.
|
|
8
|
+
|
|
3
9
|
## 0.2.27
|
|
4
10
|
|
|
5
11
|
### 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,13 +7,13 @@ 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
19
|
import { emailOtpSendsAtom, emailOtpSendState, EMAIL_OTP_RESEND_COOLDOWN_SECONDS, } from "./email-otp-state.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();
|
|
@@ -405,7 +406,7 @@ export function Signin(props) {
|
|
|
405
406
|
name: nameFromEmail(email),
|
|
406
407
|
},
|
|
407
408
|
})));
|
|
408
|
-
|
|
409
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
409
410
|
onNavigate(redirectTarget);
|
|
410
411
|
return;
|
|
411
412
|
}
|
|
@@ -426,7 +427,7 @@ export function Signin(props) {
|
|
|
426
427
|
onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
|
|
427
428
|
return;
|
|
428
429
|
}
|
|
429
|
-
|
|
430
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
430
431
|
onNavigate(result.url ?? redirectTarget);
|
|
431
432
|
}),
|
|
432
433
|
}));
|
|
@@ -565,6 +566,7 @@ export function Signin(props) {
|
|
|
565
566
|
: 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
567
|
}
|
|
567
568
|
export function VerifyEmail(props) {
|
|
569
|
+
const registry = useContext(RegistryContext);
|
|
568
570
|
const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
|
|
569
571
|
const navigate = useNavigate();
|
|
570
572
|
const searchString = useRouterState({
|
|
@@ -595,7 +597,7 @@ export function VerifyEmail(props) {
|
|
|
595
597
|
otp: value.otp.trim(),
|
|
596
598
|
},
|
|
597
599
|
})));
|
|
598
|
-
|
|
600
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
599
601
|
onNavigate(redirectTarget);
|
|
600
602
|
}),
|
|
601
603
|
}));
|
|
@@ -702,6 +704,7 @@ function ResetPasswordForm({ token, ...props }) {
|
|
|
702
704
|
}, 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
705
|
}
|
|
704
706
|
export function TwoFactor(props) {
|
|
707
|
+
const registry = useContext(RegistryContext);
|
|
705
708
|
const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
|
|
706
709
|
const navigate = useNavigate();
|
|
707
710
|
const searchString = useRouterState({
|
|
@@ -743,7 +746,7 @@ export function TwoFactor(props) {
|
|
|
743
746
|
: client.auth.twoFactorVerifyTotp({
|
|
744
747
|
payload: verification,
|
|
745
748
|
})));
|
|
746
|
-
|
|
749
|
+
yield* refreshAuthSession(registry, baseUrl);
|
|
747
750
|
onNavigate(getResultRedirectUrl(result) ?? redirectTarget);
|
|
748
751
|
}),
|
|
749
752
|
}));
|