@krak-stack/auth 0.1.36 → 0.1.38
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/dist/components/auth-forms.js +27 -22
- package/dist/components/auth-provider.d.ts +19 -1
- package/dist/components/auth-provider.js +37 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +1 -1
- package/dist/config.js +6 -2
- package/dist/service.js +7 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
2
2
|
// @ts-nocheck
|
|
3
3
|
import { useAtomSuspense } from "@effect/atom-react";
|
|
4
4
|
import { Link, useNavigate, useRouterState } from "@tanstack/react-router";
|
|
5
|
+
import { Atom, AsyncResult } from "effect/unstable/reactivity";
|
|
5
6
|
import { REGEXP_ONLY_DIGITS } from "input-otp";
|
|
6
7
|
import { KeyRound, Mail } from "lucide-react";
|
|
7
8
|
import { useEffect, useState } from "react";
|
|
@@ -131,39 +132,43 @@ const useAuthFormOptions = ({ authClient, baseUrl, locale }) => {
|
|
|
131
132
|
authClient: resolvedAuthClient,
|
|
132
133
|
baseUrl: baseUrl ?? auth?.baseUrl,
|
|
133
134
|
labels: labels(locale ?? auth?.locale ?? "en"),
|
|
135
|
+
projectConfig: auth?.projectConfig,
|
|
134
136
|
};
|
|
135
137
|
};
|
|
136
|
-
const useAuthProjectConfig = (baseUrl, searchString) => {
|
|
138
|
+
const useAuthProjectConfig = (baseUrl, searchString, projectConfig) => {
|
|
137
139
|
const projectId = getSearchParam(searchString, "projectId");
|
|
138
140
|
const clientId = getSearchParam(searchString, "client_id");
|
|
139
141
|
const host = getBrowserAuthHost();
|
|
140
|
-
const
|
|
141
|
-
query
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
142
|
+
const atom = projectConfig === undefined
|
|
143
|
+
? authApiClient(baseUrl).query("authExtra", "getProjectPublicConfig", {
|
|
144
|
+
query: {
|
|
145
|
+
...(projectId ? { projectId } : {}),
|
|
146
|
+
...(clientId ? { clientId } : {}),
|
|
147
|
+
...(host ? { host } : {}),
|
|
148
|
+
},
|
|
149
|
+
timeToLive: "5 minutes",
|
|
150
|
+
reactivityKeys: [
|
|
151
|
+
"project-public-config",
|
|
152
|
+
...(projectId ? [`project:${projectId}`] : []),
|
|
153
|
+
...(clientId ? [`client:${clientId}`] : []),
|
|
154
|
+
...(host ? [`host:${host}`] : []),
|
|
155
|
+
],
|
|
156
|
+
serializationKey: `project-public-config:${projectId ?? ""}:${clientId ?? ""}:${host ?? ""}`,
|
|
157
|
+
})
|
|
158
|
+
: Atom.make(AsyncResult.success(projectConfig));
|
|
159
|
+
const result = useAtomSuspense(atom, { suspendOnWaiting: true });
|
|
155
160
|
return result.value;
|
|
156
161
|
};
|
|
157
162
|
const text = (value, params) => Object.entries(params ?? {}).reduce((current, [key, replacement]) => current.replace(`{${key}}`, replacement), value);
|
|
158
163
|
const authLinkClassName = cn(buttonVariants({ variant: "link" }), "h-auto px-0 align-baseline");
|
|
159
164
|
const searchObject = (searchString) => Object.fromEntries(new URLSearchParams(searchString));
|
|
160
165
|
export function Signin(props) {
|
|
161
|
-
const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
|
|
166
|
+
const { authClient, baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
162
167
|
const navigate = useNavigate();
|
|
163
168
|
const searchString = useRouterState({
|
|
164
169
|
select: (state) => state.location.searchStr,
|
|
165
170
|
});
|
|
166
|
-
const projectConfig = useAuthProjectConfig(baseUrl, searchString);
|
|
171
|
+
const projectConfig = useAuthProjectConfig(baseUrl, searchString, providedProjectConfig);
|
|
167
172
|
const redirectTarget = getRedirectTarget(searchString, projectConfig?.authDomain, projectConfig?.rootDomain);
|
|
168
173
|
const socialRedirectTarget = getSocialRedirectTarget(searchString, projectConfig?.authDomain, projectConfig?.rootDomain);
|
|
169
174
|
const oauthQuery = getOAuthQuery(searchString);
|
|
@@ -355,12 +360,12 @@ export function Signin(props) {
|
|
|
355
360
|
: 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(Link, { className: authLinkClassName, to: "/sign-up", search: searchObject(searchString), children: m.auth_sign_up })] })) : null] })] }));
|
|
356
361
|
}
|
|
357
362
|
export function Signup(props) {
|
|
358
|
-
const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
|
|
363
|
+
const { authClient, baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
359
364
|
const navigate = useNavigate();
|
|
360
365
|
const searchString = useRouterState({
|
|
361
366
|
select: (state) => state.location.searchStr,
|
|
362
367
|
});
|
|
363
|
-
const projectConfig = useAuthProjectConfig(baseUrl, searchString);
|
|
368
|
+
const projectConfig = useAuthProjectConfig(baseUrl, searchString, providedProjectConfig);
|
|
364
369
|
const redirectTarget = getRedirectTarget(searchString, projectConfig?.authDomain, projectConfig?.rootDomain);
|
|
365
370
|
const authOptions = projectConfig?.authOptions ?? {};
|
|
366
371
|
const onNavigate = (target) => navigate({ href: target });
|
|
@@ -479,12 +484,12 @@ export function ResetPassword(props) {
|
|
|
479
484
|
}, 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, {})] }) }) })] }));
|
|
480
485
|
}
|
|
481
486
|
export function TwoFactor(props) {
|
|
482
|
-
const { authClient, baseUrl, labels: m } = useAuthFormOptions(props);
|
|
487
|
+
const { authClient, baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
483
488
|
const navigate = useNavigate();
|
|
484
489
|
const searchString = useRouterState({
|
|
485
490
|
select: (state) => state.location.searchStr,
|
|
486
491
|
});
|
|
487
|
-
const projectConfig = useAuthProjectConfig(baseUrl, searchString);
|
|
492
|
+
const projectConfig = useAuthProjectConfig(baseUrl, searchString, providedProjectConfig);
|
|
488
493
|
const redirectTarget = getRedirectTarget(searchString, projectConfig?.authDomain, projectConfig?.rootDomain);
|
|
489
494
|
const oauthQuery = getOAuthQuery(searchString);
|
|
490
495
|
const onNavigate = (target) => navigateTarget(target, navigate);
|
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
2
|
import type { AuthUiClient } from "./auth-client";
|
|
3
|
+
import type { ExtraProjectPublicConfig } from "../extra/schema";
|
|
3
4
|
export type KrakstackAuthLocale = "en" | "fr";
|
|
4
5
|
export type KrakstackAuthProviderProps = {
|
|
5
6
|
children: ReactNode;
|
|
6
7
|
locale?: KrakstackAuthLocale | undefined;
|
|
7
8
|
baseUrl?: string | undefined;
|
|
9
|
+
projectId?: string | null | undefined;
|
|
8
10
|
authClient?: AuthUiClient | undefined;
|
|
9
11
|
};
|
|
10
12
|
export type KrakstackAuthContextValue = {
|
|
11
13
|
locale: KrakstackAuthLocale;
|
|
12
14
|
baseUrl?: string | undefined;
|
|
13
15
|
authClient: AuthUiClient;
|
|
16
|
+
projectConfig: ExtraProjectPublicConfig | null;
|
|
14
17
|
};
|
|
15
|
-
export declare function KrakstackAuthProvider({ children, locale, baseUrl, authClient, }: KrakstackAuthProviderProps): import("react").JSX.Element;
|
|
18
|
+
export declare function KrakstackAuthProvider({ children, locale, baseUrl, projectId, authClient, }: KrakstackAuthProviderProps): import("react").JSX.Element;
|
|
16
19
|
export declare const useKrakstackAuth: () => KrakstackAuthContextValue | null;
|
|
20
|
+
export declare const useKrakstackAuthProjectConfig: () => {
|
|
21
|
+
readonly projectKey: string;
|
|
22
|
+
readonly name: string | null;
|
|
23
|
+
readonly logoUrl: string | null;
|
|
24
|
+
readonly authDomain: string | null;
|
|
25
|
+
readonly rootDomain: string | null;
|
|
26
|
+
readonly themeCss: string | null;
|
|
27
|
+
readonly authOptions: {
|
|
28
|
+
readonly emailPassword: boolean;
|
|
29
|
+
readonly emailOtp: boolean;
|
|
30
|
+
readonly google: boolean;
|
|
31
|
+
readonly signUp: boolean;
|
|
32
|
+
readonly signUpName: boolean;
|
|
33
|
+
};
|
|
34
|
+
} | null;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { apiKeyClient } from "@better-auth/api-key/client";
|
|
3
|
+
import { useAtomSuspense } from "@effect/atom-react";
|
|
4
|
+
import { useRouterState } from "@tanstack/react-router";
|
|
3
5
|
import { emailOTPClient, genericOAuthClient, organizationClient, twoFactorClient, } from "better-auth/client/plugins";
|
|
4
6
|
import { createAuthClient } from "better-auth/react";
|
|
5
7
|
import { createContext, useContext, useMemo } from "react";
|
|
8
|
+
import { authApiClient } from "./auth-api-client";
|
|
6
9
|
const KrakstackAuthContext = createContext(null);
|
|
7
10
|
const createAuthUiClient = (baseUrl) => createAuthClient({
|
|
8
11
|
...(baseUrl ? { baseURL: baseUrl } : {}),
|
|
@@ -14,9 +17,41 @@ const createAuthUiClient = (baseUrl) => createAuthClient({
|
|
|
14
17
|
genericOAuthClient(),
|
|
15
18
|
],
|
|
16
19
|
});
|
|
17
|
-
|
|
20
|
+
const getSearchParam = (searchString, key) => {
|
|
21
|
+
if (!searchString)
|
|
22
|
+
return null;
|
|
23
|
+
return new URLSearchParams(searchString).get(key);
|
|
24
|
+
};
|
|
25
|
+
const getBrowserAuthHost = () => typeof window === "undefined" ? null : window.location.host;
|
|
26
|
+
const useProjectConfig = (baseUrl, providedProjectId) => {
|
|
27
|
+
const searchString = useRouterState({
|
|
28
|
+
select: (state) => state.location.searchStr,
|
|
29
|
+
});
|
|
30
|
+
const projectId = providedProjectId ?? getSearchParam(searchString, "projectId");
|
|
31
|
+
const clientId = getSearchParam(searchString, "client_id");
|
|
32
|
+
const host = getBrowserAuthHost();
|
|
33
|
+
const result = useAtomSuspense(authApiClient(baseUrl).query("authExtra", "getProjectPublicConfig", {
|
|
34
|
+
query: {
|
|
35
|
+
...(projectId ? { projectId } : {}),
|
|
36
|
+
...(clientId ? { clientId } : {}),
|
|
37
|
+
...(host ? { host } : {}),
|
|
38
|
+
},
|
|
39
|
+
timeToLive: "5 minutes",
|
|
40
|
+
reactivityKeys: [
|
|
41
|
+
"project-public-config",
|
|
42
|
+
...(projectId ? [`project:${projectId}`] : []),
|
|
43
|
+
...(clientId ? [`client:${clientId}`] : []),
|
|
44
|
+
...(host ? [`host:${host}`] : []),
|
|
45
|
+
],
|
|
46
|
+
serializationKey: `project-public-config:${projectId ?? ""}:${clientId ?? ""}:${host ?? ""}`,
|
|
47
|
+
}), { suspendOnWaiting: true });
|
|
48
|
+
return result.value;
|
|
49
|
+
};
|
|
50
|
+
export function KrakstackAuthProvider({ children, locale = "en", baseUrl, projectId, authClient, }) {
|
|
18
51
|
const resolvedAuthClient = useMemo(() => authClient ?? createAuthUiClient(baseUrl), [authClient, baseUrl]);
|
|
19
|
-
const
|
|
52
|
+
const projectConfig = useProjectConfig(baseUrl, projectId);
|
|
53
|
+
const value = useMemo(() => ({ authClient: resolvedAuthClient, baseUrl, locale, projectConfig }), [resolvedAuthClient, baseUrl, locale, projectConfig]);
|
|
20
54
|
return (_jsx(KrakstackAuthContext.Provider, { value: value, children: children }));
|
|
21
55
|
}
|
|
22
56
|
export const useKrakstackAuth = () => useContext(KrakstackAuthContext);
|
|
57
|
+
export const useKrakstackAuthProjectConfig = () => useKrakstackAuth()?.projectConfig ?? null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { KrakstackAuthProvider, useKrakstackAuth } from "./auth-provider";
|
|
1
|
+
export { KrakstackAuthProvider, useKrakstackAuth, useKrakstackAuthProjectConfig, } from "./auth-provider";
|
|
2
2
|
export { OrganizationSwitcher } from "./organization-switcher";
|
|
3
3
|
export { ResetPassword, Signin, Signup, TwoFactor } from "./auth-forms";
|
|
4
4
|
export { UserButton } from "./user-button";
|
package/dist/components/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { KrakstackAuthProvider, useKrakstackAuth } from "./auth-provider";
|
|
1
|
+
export { KrakstackAuthProvider, useKrakstackAuth, useKrakstackAuthProjectConfig, } from "./auth-provider";
|
|
2
2
|
export { OrganizationSwitcher } from "./organization-switcher";
|
|
3
3
|
export { ResetPassword, Signin, Signup, TwoFactor } from "./auth-forms";
|
|
4
4
|
export { UserButton } from "./user-button";
|
package/dist/config.js
CHANGED
|
@@ -2,11 +2,15 @@ import { Config, Context, Effect, Layer, Redacted } from "effect";
|
|
|
2
2
|
export const normalizeBaseUrl = (value) => value.replace(/\/$/, "");
|
|
3
3
|
export const defaultBaseUrl = () => {
|
|
4
4
|
const env = globalThis.process?.env;
|
|
5
|
-
return normalizeBaseUrl(env?.
|
|
5
|
+
return normalizeBaseUrl(env?.KRAKSTACK_AUTH_URL ??
|
|
6
|
+
env?.VITE_KRAKSTACK_AUTH_URL ??
|
|
7
|
+
"http://localhost:3000");
|
|
6
8
|
};
|
|
7
9
|
export class AuthClientConfig extends Context.Service()("@krak-stack/auth/AuthClientConfig", {
|
|
8
10
|
make: (options = {}) => Effect.gen(function* () {
|
|
9
|
-
const baseUrl = options.baseUrl ??
|
|
11
|
+
const baseUrl = options.baseUrl ??
|
|
12
|
+
process.env.KRAKSTACK_AUTH_URL ??
|
|
13
|
+
(yield* Config.string("VITE_KRAKSTACK_AUTH_URL"));
|
|
10
14
|
const apiKey = options.apiKey ??
|
|
11
15
|
(yield* Config.redacted("KRAKSTACK_AUTH_SERVICE_API_KEY"));
|
|
12
16
|
return {
|
package/dist/service.js
CHANGED
|
@@ -6,12 +6,18 @@ import { AuthClientConfig } from "./config";
|
|
|
6
6
|
const forwardedAuthHeaderNames = [
|
|
7
7
|
"accept-language",
|
|
8
8
|
"cookie",
|
|
9
|
+
"origin",
|
|
10
|
+
"referer",
|
|
9
11
|
"user-agent",
|
|
10
12
|
];
|
|
11
13
|
const forwardedAuthHeaders = (headers) => {
|
|
14
|
+
const normalizedHeaders = {};
|
|
12
15
|
const forwarded = {};
|
|
16
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
17
|
+
normalizedHeaders[key.toLowerCase()] = value;
|
|
18
|
+
}
|
|
13
19
|
for (const name of forwardedAuthHeaderNames) {
|
|
14
|
-
const value =
|
|
20
|
+
const value = normalizedHeaders[name];
|
|
15
21
|
if (value)
|
|
16
22
|
forwarded[name] = value;
|
|
17
23
|
}
|