@krak-stack/auth 0.1.31 → 0.1.33

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.
@@ -1,12 +1,12 @@
1
1
  import type { AuthUiClient } from "./auth-client";
2
+ import { type KrakstackAuthLocale } from "./auth-provider";
2
3
  type AuthFormProps = {
3
- authClient: AuthUiClient;
4
+ authClient?: AuthUiClient;
4
5
  baseUrl?: string | undefined;
6
+ locale?: KrakstackAuthLocale | undefined;
5
7
  };
6
- export declare function Signin({ authClient, baseUrl }: AuthFormProps): import("react").JSX.Element;
7
- export declare function Signup({ authClient, baseUrl }: AuthFormProps): import("react").JSX.Element;
8
- export declare function ResetPassword({ authClient }: {
9
- authClient: AuthUiClient;
10
- }): import("react").JSX.Element;
11
- export declare function TwoFactor({ authClient, baseUrl }: AuthFormProps): import("react").JSX.Element;
8
+ export declare function Signin(props: AuthFormProps): import("react").JSX.Element;
9
+ export declare function Signup(props: AuthFormProps): import("react").JSX.Element;
10
+ export declare function ResetPassword(props: AuthFormProps): import("react").JSX.Element;
11
+ export declare function TwoFactor(props: AuthFormProps): import("react").JSX.Element;
12
12
  export {};
@@ -12,6 +12,7 @@ import { FieldError, useAppForm } from "@/components/ui/form";
12
12
  import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp";
13
13
  import { cn } from "@/lib/utils";
14
14
  import { authApiClient } from "./auth-api-client";
15
+ import { useKrakstackAuth } from "./auth-provider";
15
16
  const EMAIL_OTP_RESEND_COOLDOWN_SECONDS = 60;
16
17
  const defaultMessages = {
17
18
  en: {
@@ -117,9 +118,21 @@ const defaultMessages = {
117
118
  two_factor_verify_error: "Impossible de vérifier le code.",
118
119
  },
119
120
  };
120
- const labels = () => ({
121
- ...defaultMessages[currentLocale()],
121
+ const labels = (locale) => ({
122
+ ...defaultMessages[locale],
122
123
  });
124
+ const useAuthFormOptions = ({ authClient, baseUrl, locale }) => {
125
+ const auth = useKrakstackAuth();
126
+ const resolvedAuthClient = authClient ?? auth?.authClient;
127
+ if (!resolvedAuthClient) {
128
+ throw new Error("Krakstack auth components require an authClient prop or KrakstackAuthProvider.");
129
+ }
130
+ return {
131
+ authClient: resolvedAuthClient,
132
+ baseUrl: baseUrl ?? auth?.baseUrl,
133
+ labels: labels(locale ?? auth?.locale ?? "en"),
134
+ };
135
+ };
123
136
  const useAuthProjectConfig = (baseUrl, searchString) => {
124
137
  const projectId = getSearchParam(searchString, "projectId");
125
138
  const clientId = getSearchParam(searchString, "client_id");
@@ -141,12 +154,9 @@ const useAuthProjectConfig = (baseUrl, searchString) => {
141
154
  }), { suspendOnWaiting: true });
142
155
  return result.value;
143
156
  };
144
- const currentLocale = () => typeof navigator !== "undefined" &&
145
- navigator.language.toLowerCase().startsWith("fr")
146
- ? "fr"
147
- : "en";
148
157
  const text = (value, params) => Object.entries(params ?? {}).reduce((current, [key, replacement]) => current.replace(`{${key}}`, replacement), value);
149
- export function Signin({ authClient, baseUrl }) {
158
+ export function Signin(props) {
159
+ const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
150
160
  const navigate = useNavigate();
151
161
  const searchString = useRouterState({
152
162
  select: (state) => state.location.searchStr,
@@ -158,7 +168,6 @@ export function Signin({ authClient, baseUrl }) {
158
168
  const authOptions = projectConfig?.authOptions ?? {};
159
169
  const onNavigate = (target) => navigateTarget(target, navigate);
160
170
  const onTwoFactorRedirect = (href) => navigate({ href });
161
- const m = labels();
162
171
  const options = {
163
172
  emailPassword: authOptions.emailPassword ?? true,
164
173
  emailOtp: authOptions.emailOtp ?? true,
@@ -343,7 +352,8 @@ export function Signin({ authClient, baseUrl }) {
343
352
  ? m.sign_in_email_otp_verify
344
353
  : 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", onClick: signInWithGoogle, children: [_jsx(GoogleLogo, {}), m.auth_continue_with_google] })] })) : null, !hasPrimaryAuth && !options.google ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.oauth_client_no_auth_methods })) : null, options.signUp ? (_jsxs("p", { className: "text-muted-foreground mt-6 text-center text-sm", children: [m.sign_in_need_account, " ", _jsx("a", { className: cn(buttonVariants({ variant: "link" }), "h-auto px-0"), href: `/sign-up${searchString}`, children: m.auth_sign_up })] })) : null] })] }));
345
354
  }
346
- export function Signup({ authClient, baseUrl }) {
355
+ export function Signup(props) {
356
+ const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
347
357
  const navigate = useNavigate();
348
358
  const searchString = useRouterState({
349
359
  select: (state) => state.location.searchStr,
@@ -353,7 +363,6 @@ export function Signup({ authClient, baseUrl }) {
353
363
  const authOptions = projectConfig?.authOptions ?? {};
354
364
  const onNavigate = (target) => navigate({ href: target });
355
365
  const onVerifyEmail = (email) => navigate({ to: "/verify-email", search: { email } });
356
- const m = labels();
357
366
  const options = {
358
367
  google: authOptions.google ?? true,
359
368
  signUp: authOptions.signUp ?? true,
@@ -427,14 +436,14 @@ export function Signup({ authClient, baseUrl }) {
427
436
  form.handleSubmit();
428
437
  }, children: [options.signUpName ? (_jsx(form.AppField, { name: "name", children: (field) => (_jsx(field.TextField, { label: m.field_name, autoComplete: "name", required: true })) })) : null, _jsx(form.AppField, { name: "email", children: (field) => (_jsx(field.TextField, { label: m.field_email, type: "email", autoComplete: "email", required: true })) }), _jsx(form.AppField, { name: "password", children: (field) => (_jsx(field.TextField, { label: m.field_password, type: "password", autoComplete: "new-password", minLength: 8, required: true })) }), _jsx(form.FormError, {}), _jsx(form.SubmitButton, {})] }) }), options.google ? (_jsxs("div", { className: "mt-4 flex flex-col gap-3", children: [_jsx(Divider, { label: m.auth_or_continue_with }), _jsxs(Button, { type: "button", variant: "outline", className: "w-full", onClick: signUpWithGoogle, children: [_jsx(GoogleLogo, {}), m.auth_continue_with_google] })] })) : null, _jsxs("p", { className: "text-muted-foreground mt-6 text-center text-sm", children: [m.sign_up_have_account, " ", _jsx("a", { className: "text-foreground font-medium underline-offset-4 hover:underline", href: `/sign-in${searchString}`, children: m.auth_sign_in })] })] })] }));
429
438
  }
430
- export function ResetPassword({ authClient }) {
439
+ export function ResetPassword(props) {
440
+ const { authClient, labels: m } = useAuthFormOptions(props);
431
441
  const navigate = useNavigate();
432
442
  const searchString = useRouterState({
433
443
  select: (state) => state.location.searchStr,
434
444
  });
435
445
  const token = getSearchParam(searchString, "token") ?? "";
436
446
  const onSuccess = () => navigate({ to: "/sign-in" });
437
- const m = labels();
438
447
  const form = useAppForm({
439
448
  defaultValues: { password: "" },
440
449
  onSubmit: async ({ value, formApi }) => {
@@ -467,7 +476,8 @@ export function ResetPassword({ authClient }) {
467
476
  form.handleSubmit();
468
477
  }, children: [_jsx(form.AppField, { name: "password", children: (field) => (_jsx(field.TextField, { label: m.reset_password_new_password, type: "password", autoComplete: "new-password", minLength: 8, required: true })) }), _jsx(form.FormError, {}), _jsx(form.SubmitButton, {})] }) }) })] }));
469
478
  }
470
- export function TwoFactor({ authClient, baseUrl }) {
479
+ export function TwoFactor(props) {
480
+ const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
471
481
  const navigate = useNavigate();
472
482
  const searchString = useRouterState({
473
483
  select: (state) => state.location.searchStr,
@@ -476,7 +486,6 @@ export function TwoFactor({ authClient, baseUrl }) {
476
486
  const redirectTarget = getRedirectTarget(searchString, projectConfig?.authDomain, projectConfig?.rootDomain);
477
487
  const oauthQuery = getOAuthQuery(searchString);
478
488
  const onNavigate = (target) => navigateTarget(target, navigate);
479
- const m = labels();
480
489
  const [mode, setMode] = useState("totp");
481
490
  const [emailCodeSent, setEmailCodeSent] = useState(false);
482
491
  const form = useAppForm({
@@ -0,0 +1,16 @@
1
+ import { type ReactNode } from "react";
2
+ import type { AuthUiClient } from "./auth-client";
3
+ export type KrakstackAuthLocale = "en" | "fr";
4
+ export type KrakstackAuthProviderProps = {
5
+ children: ReactNode;
6
+ locale?: KrakstackAuthLocale | undefined;
7
+ baseUrl?: string | undefined;
8
+ authClient?: AuthUiClient | undefined;
9
+ };
10
+ export type KrakstackAuthContextValue = {
11
+ locale: KrakstackAuthLocale;
12
+ baseUrl?: string | undefined;
13
+ authClient: AuthUiClient;
14
+ };
15
+ export declare function KrakstackAuthProvider({ children, locale, baseUrl, authClient, }: KrakstackAuthProviderProps): import("react").JSX.Element;
16
+ export declare const useKrakstackAuth: () => KrakstackAuthContextValue | null;
@@ -0,0 +1,22 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { apiKeyClient } from "@better-auth/api-key/client";
3
+ import { emailOTPClient, genericOAuthClient, organizationClient, twoFactorClient, } from "better-auth/client/plugins";
4
+ import { createAuthClient } from "better-auth/react";
5
+ import { createContext, useContext, useMemo } from "react";
6
+ const KrakstackAuthContext = createContext(null);
7
+ const createAuthUiClient = (baseUrl) => createAuthClient({
8
+ ...(baseUrl ? { baseURL: baseUrl } : {}),
9
+ plugins: [
10
+ emailOTPClient(),
11
+ organizationClient(),
12
+ twoFactorClient(),
13
+ apiKeyClient(),
14
+ genericOAuthClient(),
15
+ ],
16
+ });
17
+ export function KrakstackAuthProvider({ children, locale = "en", baseUrl, authClient, }) {
18
+ const resolvedAuthClient = useMemo(() => authClient ?? createAuthUiClient(baseUrl), [authClient, baseUrl]);
19
+ const value = useMemo(() => ({ authClient: resolvedAuthClient, baseUrl, locale }), [resolvedAuthClient, baseUrl, locale]);
20
+ return (_jsx(KrakstackAuthContext.Provider, { value: value, children: children }));
21
+ }
22
+ export const useKrakstackAuth = () => useContext(KrakstackAuthContext);
@@ -1,6 +1,8 @@
1
+ export { KrakstackAuthProvider, useKrakstackAuth } from "./auth-provider";
1
2
  export { OrganizationSwitcher } from "./organization-switcher";
2
3
  export { ResetPassword, Signin, Signup, TwoFactor } from "./auth-forms";
3
4
  export { UserButton } from "./user-button";
5
+ export type { KrakstackAuthContextValue, KrakstackAuthLocale, KrakstackAuthProviderProps, } from "./auth-provider";
4
6
  export type { OrganizationSwitcherDialog } from "./organization-switcher";
5
7
  export type { UserButtonDialog } from "./user-button";
6
8
  export type { AuthUiClient } from "./auth-client";
@@ -1,3 +1,4 @@
1
+ export { KrakstackAuthProvider, useKrakstackAuth } from "./auth-provider";
1
2
  export { OrganizationSwitcher } from "./organization-switcher";
2
3
  export { ResetPassword, Signin, Signup, TwoFactor } from "./auth-forms";
3
4
  export { UserButton } from "./user-button";
@@ -1,10 +1,11 @@
1
1
  export const isRecord = (value) => typeof value === "object" && value !== null;
2
+ const isAssetPath = (path) => path.startsWith("/api/assets/") || path.startsWith("/api/auth/assets/");
2
3
  export const assetPath = (value) => {
3
4
  if (!value?.trim())
4
5
  return null;
5
6
  try {
6
7
  const url = new URL(value);
7
- return url.pathname.startsWith("/api/assets/") ? url.pathname : value;
8
+ return isAssetPath(url.pathname) ? url.pathname : value;
8
9
  }
9
10
  catch {
10
11
  return value;
@@ -14,7 +15,7 @@ export const assetUrl = (value, baseUrl) => {
14
15
  const path = value?.trim();
15
16
  if (!path)
16
17
  return "";
17
- if (!path.startsWith("/api/assets/"))
18
+ if (!isAssetPath(path))
18
19
  return path;
19
20
  if (!baseUrl?.trim())
20
21
  return path;
@@ -215,6 +215,11 @@ export const ExtraUploadedAsset = Schema.Struct({
215
215
  identifier: "ExtraUploadedAsset",
216
216
  title: "Extra uploaded asset",
217
217
  description: "Stable public asset URL returned after an image upload.",
218
+ examples: [
219
+ {
220
+ url: "https://auth.example.com/api/auth/assets/logos/organizations/logo.png",
221
+ },
222
+ ],
218
223
  });
219
224
  export class ExtraBadRequest extends Schema.ErrorClass("ExtraBadRequest")({
220
225
  _tag: Schema.tag("ExtraBadRequest"),
package/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ export * from "./schema";
2
2
  export { AuthApi } from "./api";
3
3
  export { AuthService, type AuthServiceLayerOptions } from "./service";
4
4
  export { AuthClientConfig } from "./config";
5
- export { OrganizationSwitcher, ResetPassword, Signin, Signup, TwoFactor, UserButton, type AuthUiClient, type OrganizationSwitcherDialog, type UserButtonDialog, } from "./components";
5
+ export { proxyAuthRequest } from "./server/proxy";
6
+ export { OrganizationSwitcher, KrakstackAuthProvider, ResetPassword, Signin, Signup, TwoFactor, UserButton, type AuthUiClient, type KrakstackAuthContextValue, type KrakstackAuthLocale, type KrakstackAuthProviderProps, type OrganizationSwitcherDialog, type UserButtonDialog, } from "./components";
6
7
  export * as BetterAuth from "./better-auth";
7
8
  export * as Extra from "./extra";
8
9
  export * as Server from "./server";
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@ export * from "./schema";
2
2
  export { AuthApi } from "./api";
3
3
  export { AuthService } from "./service";
4
4
  export { AuthClientConfig } from "./config";
5
- export { OrganizationSwitcher, ResetPassword, Signin, Signup, TwoFactor, UserButton, } from "./components";
5
+ export { proxyAuthRequest } from "./server/proxy";
6
+ export { OrganizationSwitcher, KrakstackAuthProvider, ResetPassword, Signin, Signup, TwoFactor, UserButton, } from "./components";
6
7
  export * as BetterAuth from "./better-auth";
7
8
  export * as Extra from "./extra";
8
9
  export * as Server from "./server";
@@ -1,5 +1,6 @@
1
1
  export { AuthService } from "../service";
2
2
  export { AuthClientConfig } from "../config";
3
3
  export { ServerApi, ServerOrganizationsApiGroup, ServerUsersApiGroup, } from "./api";
4
+ export { proxyAuthRequest } from "./proxy";
4
5
  export type { ServerActiveOrganization, ServerMembersResponse, ServerOrganizationsResponse, ServerUsersResponse, } from "./schema";
5
6
  export type { Member, Organization, OrganizationMetadata, User, } from "../schema";
@@ -1,3 +1,4 @@
1
1
  export { AuthService } from "../service";
2
2
  export { AuthClientConfig } from "../config";
3
3
  export { ServerApi, ServerOrganizationsApiGroup, ServerUsersApiGroup, } from "./api";
4
+ export { proxyAuthRequest } from "./proxy";
@@ -0,0 +1 @@
1
+ export declare const proxyAuthRequest: (request: Request, baseUrl: string | URL) => Promise<Response>;
@@ -0,0 +1,41 @@
1
+ const blockedRequestHeaders = [
2
+ "connection",
3
+ "content-length",
4
+ "forwarded",
5
+ "host",
6
+ "keep-alive",
7
+ "proxy-authenticate",
8
+ "proxy-authorization",
9
+ "te",
10
+ "trailer",
11
+ "transfer-encoding",
12
+ "upgrade",
13
+ "cdn-loop",
14
+ ];
15
+ const proxyHeaders = (request) => {
16
+ const headers = new Headers(request.headers);
17
+ for (const header of blockedRequestHeaders)
18
+ headers.delete(header);
19
+ for (const header of Array.from(headers.keys())) {
20
+ if (header.startsWith("cf-") || header.startsWith("x-forwarded-")) {
21
+ headers.delete(header);
22
+ }
23
+ }
24
+ return headers;
25
+ };
26
+ export const proxyAuthRequest = (request, baseUrl) => {
27
+ const target = new URL(request.url);
28
+ const authUrl = new URL(baseUrl);
29
+ const init = {
30
+ headers: proxyHeaders(request),
31
+ method: request.method,
32
+ };
33
+ target.protocol = authUrl.protocol;
34
+ target.host = authUrl.host;
35
+ target.username = authUrl.username;
36
+ target.password = authUrl.password;
37
+ if (request.method !== "GET" && request.method !== "HEAD") {
38
+ init.body = request.body;
39
+ }
40
+ return fetch(new Request(target, init), { redirect: "manual" });
41
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.1.31",
3
+ "version": "0.1.33",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [