@krak-stack/auth 0.2.10 → 0.2.11

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.
@@ -2,7 +2,7 @@ import { Effect } from "effect";
2
2
  import { AtomHttpApi } from "effect/unstable/reactivity";
3
3
  import { HttpApi, HttpApiClient } from "effect/unstable/httpapi";
4
4
  import { AuthClientApi } from "../api.js";
5
- export declare const authHttpClient: (baseUrl?: string | undefined) => Effect.Effect<{
5
+ export declare const authHttpClient: (baseUrl?: string | undefined, getLocale?: (() => "en" | "fr") | undefined) => Effect.Effect<{
6
6
  readonly admin: {
7
7
  readonly createDomain: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"createDomain", "POST", "/api/auth/admin/domains", never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
8
8
  readonly hostname: import("effect/Schema").NonEmptyString;
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { FetchHttpClient, HttpClient } from "effect/unstable/http";
2
+ import { FetchHttpClient, HttpClient, HttpClientRequest, } from "effect/unstable/http";
3
3
  import { AtomHttpApi } from "effect/unstable/reactivity";
4
4
  import { HttpApi, HttpApiClient } from "effect/unstable/httpapi";
5
5
  import { AuthClientApi } from "../api.js";
@@ -11,9 +11,14 @@ const authClientApiUrl = (baseUrl) => {
11
11
  const withCredentials = (client) => HttpClient.makeWith((request) => client.postprocess(request).pipe(Effect.provideService(FetchHttpClient.RequestInit, {
12
12
  credentials: "include",
13
13
  })), client.preprocess);
14
- export const authHttpClient = (baseUrl) => HttpApiClient.make(AuthClientApi, {
14
+ export const authHttpClient = (baseUrl, getLocale) => HttpApiClient.make(AuthClientApi, {
15
15
  baseUrl: authClientApiUrl(baseUrl),
16
- transformClient: withCredentials,
16
+ transformClient: (client) => withCredentials(client).pipe(HttpClient.mapRequest((request) => {
17
+ const requestLocale = getLocale?.();
18
+ return requestLocale
19
+ ? HttpClientRequest.setHeader(request, "accept-language", requestLocale)
20
+ : request;
21
+ })),
17
22
  }).pipe(Effect.provide(FetchHttpClient.layer));
18
23
  const createAuthClientApiClient = (key) => {
19
24
  class AuthClientApiClient extends AtomHttpApi.Service()(`AuthClientApi:${key}`, {
@@ -6,7 +6,7 @@ import { Effect, Option, Schema } from "effect";
6
6
  import { Atom, AsyncResult } from "effect/unstable/reactivity";
7
7
  import { REGEXP_ONLY_DIGITS } from "input-otp";
8
8
  import { KeyRound, Loader2, Mail } from "lucide-react";
9
- import { useEffect, useState } from "react";
9
+ import { useEffect, useRef, useState } from "react";
10
10
  import { Button, buttonVariants } from "@/components/ui/button";
11
11
  import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card";
12
12
  import { CheckboxField, ErrorMessage, SubmitError, TextField, effectFormMessages, } from "@krak-stack/registry/effect-form";
@@ -147,9 +147,16 @@ const labels = (locale) => ({
147
147
  });
148
148
  const useAuthFormOptions = ({ baseUrl, locale }) => {
149
149
  const auth = useKrakstackAuth();
150
+ const resolvedLocale = locale ?? auth?.locale ?? "en";
151
+ const localeRef = useRef(resolvedLocale);
152
+ useEffect(() => {
153
+ localeRef.current = resolvedLocale;
154
+ }, [resolvedLocale]);
150
155
  return {
151
156
  baseUrl: baseUrl ?? auth?.baseUrl,
152
- labels: labels(locale ?? auth?.locale ?? "en"),
157
+ // Form atoms retain their initial handlers; resolve the locale when sending.
158
+ locale: () => localeRef.current,
159
+ labels: labels(resolvedLocale),
153
160
  projectConfig: auth?.projectConfig,
154
161
  };
155
162
  };
@@ -206,7 +213,7 @@ export const resolveInitialAuthMethod = ({ requestedMethod, lastLoginMethod, ema
206
213
  return emailOtp ? "emailOtp" : "password";
207
214
  };
208
215
  export function Signin(props) {
209
- const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
216
+ const { baseUrl, locale, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
210
217
  const navigate = useNavigate();
211
218
  const searchString = useRouterState({
212
219
  select: (state) => state.location.searchStr,
@@ -265,7 +272,7 @@ export function Signin(props) {
265
272
  mode: { validation: "onSubmit" },
266
273
  onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
267
274
  const email = value.email.trim();
268
- const sendEmailOtp = authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
275
+ const sendEmailOtp = authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
269
276
  payload: { email, type: "sign-in" },
270
277
  })), Effect.tap(() => Effect.sync(() => {
271
278
  setAuthMethod("emailOtp");
@@ -300,7 +307,7 @@ export function Signin(props) {
300
307
  yield* sendEmailOtp;
301
308
  return;
302
309
  }
303
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmailOtp({
310
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.signInEmailOtp({
304
311
  payload: {
305
312
  email,
306
313
  otp: value.otp.trim(),
@@ -323,7 +330,7 @@ export function Signin(props) {
323
330
  password: value.password,
324
331
  callbackURL: redirectTarget,
325
332
  };
326
- const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmail({ payload })));
333
+ const result = yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.signInEmail({ payload })));
327
334
  if (hasTwoFactorRedirect(result)) {
328
335
  onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
329
336
  return;
@@ -357,7 +364,7 @@ export function Signin(props) {
357
364
  callbackURL: socialRedirectTarget,
358
365
  errorCallbackURL: "/sign-in",
359
366
  };
360
- const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInSocial({ payload })));
367
+ const result = yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.signInSocial({ payload })));
361
368
  notifyAuthChange();
362
369
  if (result.url)
363
370
  onNavigate(result.url);
@@ -423,7 +430,7 @@ export function Signin(props) {
423
430
  return;
424
431
  event.preventDefault();
425
432
  event.currentTarget.form?.requestSubmit();
426
- }, required: true })) : null, emailSubmitted && selectedAuthMethod === "password" ? (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(form.password, { label: m.field_password, type: "password", autoComplete: "current-password", required: true }), _jsxs("div", { className: "flex flex-wrap gap-x-4 gap-y-2 text-sm", children: [_jsx(Link, { className: authLinkClassName, to: "/reset-password", search: authMethodSearch(searchString, "password", formValues.email.trim()), children: m.sign_in_forgot_password }), canChangeAuthMethod ? (_jsxs(Button, { type: "button", variant: "link", className: "h-auto px-0", onClick: switchAuthMethod, children: [_jsx(Mail, {}), m.sign_in_use_email_otp] })) : null] })] })) : 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 }), _jsxs("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
433
+ }, required: true })) : null, emailSubmitted && selectedAuthMethod === "password" ? (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(form.password, { label: m.field_password, type: "password", autoFocus: true, autoComplete: "current-password", required: true }), _jsxs("div", { className: "flex flex-wrap gap-x-4 gap-y-2 text-sm", children: [_jsx(Link, { className: authLinkClassName, to: "/reset-password", search: authMethodSearch(searchString, "password", formValues.email.trim()), children: m.sign_in_forgot_password }), canChangeAuthMethod ? (_jsxs(Button, { type: "button", variant: "link", className: "h-auto px-0", onClick: switchAuthMethod, children: [_jsx(Mail, {}), m.sign_in_use_email_otp] })) : null] })] })) : 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 }), _jsxs("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
427
434
  ? text(m.sign_in_email_otp_resend_countdown, {
428
435
  seconds: String(emailOtpResendSeconds),
429
436
  })
@@ -434,7 +441,7 @@ export function Signin(props) {
434
441
  : m.auth_sign_in] }) })] }) })) : null, options.google && !emailSubmitted ? (_jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [hasPrimaryAuth ? (_jsx(Divider, { label: m.auth_or_continue_with })) : null, _jsxs(Button, { type: "button", variant: "outline", className: "w-full", disabled: submitResult.waiting, onClick: signInWithGoogle, children: [_jsx(GoogleLogo, {}), m.auth_continue_with_google] }), _jsx(SubmitError, { result: socialSignInResult })] })) : null, !hasPrimaryAuth && !options.google ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.oauth_client_no_auth_methods })) : null] })] }));
435
442
  }
436
443
  export function VerifyEmail(props) {
437
- const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
444
+ const { baseUrl, locale, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
438
445
  const navigate = useNavigate();
439
446
  const searchString = useRouterState({
440
447
  select: (state) => state.location.searchStr,
@@ -449,7 +456,7 @@ export function VerifyEmail(props) {
449
456
  mode: { validation: "onSubmit" },
450
457
  onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
451
458
  if (action === "resend") {
452
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
459
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
453
460
  payload: {
454
461
  email: value.email.trim(),
455
462
  type: "email-verification",
@@ -458,7 +465,7 @@ export function VerifyEmail(props) {
458
465
  setResent(true);
459
466
  return;
460
467
  }
461
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.verifyEmailOtp({
468
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.verifyEmailOtp({
462
469
  payload: {
463
470
  email: value.email.trim(),
464
471
  otp: value.otp.trim(),
@@ -485,7 +492,7 @@ export function VerifyEmail(props) {
485
492
  }, children: [_jsx(form.email, { label: m.field_email, type: "email", autoComplete: "email", required: true }), _jsx(form.otp, { label: m.verify_email_code }), _jsx(SubmitError, { result: submitResult }), resent ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.verify_email_resent })) : null, _jsxs("div", { className: "flex flex-wrap items-center gap-3", children: [_jsxs(Button, { type: "submit", disabled: submitResult.waiting, children: [submitResult.waiting ? (_jsx(Loader2, { className: "animate-spin", "data-icon": "inline-start" })) : null, effectFormMessages().submit] }), _jsx(Button, { type: "button", variant: "ghost", disabled: submitResult.waiting, onClick: resendCode, children: m.verify_email_resend })] })] }) }), _jsx("p", { className: "text-muted-foreground mt-6 text-center text-sm", children: _jsx(Link, { className: authLinkClassName, to: "/sign-in", children: m.forgot_password_back_to_sign_in }) })] })] }));
486
493
  }
487
494
  export function ForgotPassword(props) {
488
- const { baseUrl, labels: m } = useAuthFormOptions(props);
495
+ const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
489
496
  const searchString = useRouterState({
490
497
  select: (state) => state.location.searchStr,
491
498
  });
@@ -497,7 +504,7 @@ export function ForgotPassword(props) {
497
504
  mode: { validation: "onSubmit" },
498
505
  onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
499
506
  setSubmitted(false);
500
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.requestPasswordReset({
507
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.requestPasswordReset({
501
508
  payload: {
502
509
  email: value.email.trim(),
503
510
  redirectTo: `${window.location.origin}/reset-password`,
@@ -522,7 +529,7 @@ export function ResetPassword(props) {
522
529
  return token ? (_jsx(ResetPasswordForm, { ...props, token: token })) : (_jsx(ForgotPassword, { ...props }));
523
530
  }
524
531
  function ResetPasswordForm({ token, ...props }) {
525
- const { baseUrl, labels: m } = useAuthFormOptions(props);
532
+ const { baseUrl, locale, labels: m } = useAuthFormOptions(props);
526
533
  const navigate = useNavigate();
527
534
  const onSuccess = () => navigate({ to: "/sign-in" });
528
535
  const [form] = useState(() => FormReact.make(resetPasswordFormBuilder, {
@@ -530,7 +537,7 @@ function ResetPasswordForm({ token, ...props }) {
530
537
  fields: { password: TextField },
531
538
  mode: { validation: "onSubmit" },
532
539
  onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
533
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.resetPassword({
540
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.resetPassword({
534
541
  payload: {
535
542
  newPassword: value.password,
536
543
  token,
@@ -548,7 +555,7 @@ function ResetPasswordForm({ token, ...props }) {
548
555
  }, children: [_jsx(form.password, { label: m.reset_password_new_password, type: "password", autoComplete: "new-password", minLength: 8, required: true }), _jsx(SubmitError, { result: submitResult }), _jsxs(Button, { type: "submit", disabled: submitResult.waiting, children: [submitResult.waiting ? (_jsx(Loader2, { className: "animate-spin", "data-icon": "inline-start" })) : null, effectFormMessages().submit] })] }) }) })] }));
549
556
  }
550
557
  export function TwoFactor(props) {
551
- const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
558
+ const { baseUrl, locale, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
552
559
  const navigate = useNavigate();
553
560
  const searchString = useRouterState({
554
561
  select: (state) => state.location.searchStr,
@@ -565,7 +572,7 @@ export function TwoFactor(props) {
565
572
  mode: { validation: "onSubmit" },
566
573
  onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
567
574
  if (action.type === "sendEmailCode") {
568
- yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorSendOtp({ payload: {} })));
575
+ yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => client.auth.twoFactorSendOtp({ payload: {} })));
569
576
  setEmailCodeSent(true);
570
577
  setMode("email");
571
578
  return;
@@ -578,7 +585,7 @@ export function TwoFactor(props) {
578
585
  oauth_query: oauthQuery,
579
586
  }
580
587
  : { code, trustDevice: value.trustDevice };
581
- const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => action.mode === "backup"
588
+ const result = yield* authHttpClient(baseUrl, locale).pipe(Effect.flatMap((client) => action.mode === "backup"
582
589
  ? client.auth.twoFactorVerifyBackupCode({
583
590
  payload: verification,
584
591
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.2.10",
3
+ "version": "0.2.11",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [