@krak-stack/auth 0.1.71 → 0.2.1
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/README.md +4 -3
- package/dist/access.d.ts +5 -7
- package/dist/access.js +11 -7
- package/dist/admin/api.group.d.ts +53 -53
- package/dist/admin/api.group.js +15 -15
- package/dist/admin/schema.d.ts +18 -18
- package/dist/admin/schema.js +3 -3
- package/dist/api.d.ts +1995 -533
- package/dist/api.js +7 -7
- package/dist/auth/api.group.d.ts +793 -0
- package/dist/auth/api.group.js +241 -0
- package/dist/auth/schema.d.ts +808 -0
- package/dist/auth/schema.js +628 -0
- package/dist/components/admin-organization-form.d.ts +1 -1
- package/dist/components/admin-organization-form.js +17 -18
- package/dist/components/admin-organizations.d.ts +4 -5
- package/dist/components/admin-organizations.js +83 -73
- package/dist/components/admin-users.d.ts +5 -6
- package/dist/components/admin-users.js +160 -181
- package/dist/components/api-key-edit-form.d.ts +4 -3
- package/dist/components/api-key-edit-form.js +33 -27
- package/dist/components/api-key-permissions.d.ts +1 -1
- package/dist/components/api-key-rate-limit.d.ts +1 -1
- package/dist/components/api-key-referrers.d.ts +3 -2
- package/dist/components/api-key-referrers.js +2 -3
- package/dist/components/api-key.d.ts +0 -9
- package/dist/components/api-key.js +0 -39
- package/dist/components/auth-atoms.d.ts +178 -0
- package/dist/components/auth-atoms.js +46 -0
- package/dist/components/auth-client-api.d.ts +1371 -556
- package/dist/components/auth-client-api.js +22 -14
- package/dist/components/auth-forms.d.ts +1 -3
- package/dist/components/auth-forms.js +173 -195
- package/dist/components/auth-provider.d.ts +16 -20
- package/dist/components/auth-provider.js +56 -30
- package/dist/components/has-permission.js +1 -1
- package/dist/components/index.d.ts +19 -20
- package/dist/components/index.js +13 -13
- package/dist/components/member-required.d.ts +1 -3
- package/dist/components/member-required.js +102 -78
- package/dist/components/organization-switcher.d.ts +4 -6
- package/dist/components/organization-switcher.js +539 -390
- package/dist/components/project-access-matrix.d.ts +1 -1
- package/dist/components/project-access-matrix.js +9 -13
- package/dist/components/user-button.d.ts +5 -3
- package/dist/components/user-button.js +291 -336
- package/dist/components/utils.d.ts +3 -1
- package/dist/components/utils.js +4 -11
- package/dist/extra/api.group.d.ts +22 -22
- package/dist/extra/api.group.js +9 -9
- package/dist/extra/schema.d.ts +1 -2
- package/dist/extra/schema.js +2 -3
- package/dist/index.d.ts +11 -12
- package/dist/index.js +11 -12
- package/dist/query.d.ts +2 -2
- package/dist/query.js +5 -5
- package/dist/roles.d.ts +5 -4
- package/dist/roles.js +11 -7
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +26 -19
- package/dist/server/actor.d.ts +20 -22
- package/dist/server/actor.js +14 -17
- package/dist/server/api.group.d.ts +29 -29
- package/dist/server/api.group.js +14 -14
- package/dist/server/index.d.ts +14 -14
- package/dist/server/index.js +8 -8
- package/dist/server/middleware.d.ts +1 -1
- package/dist/server/middleware.js +1 -1
- package/dist/server/schema.js +1 -1
- package/dist/service.d.ts +1997 -583
- package/dist/service.js +23 -16
- package/package.json +10 -9
- package/dist/better-auth/api.group.d.ts +0 -62
- package/dist/better-auth/api.group.js +0 -39
- package/dist/better-auth/schema.d.ts +0 -72
- package/dist/better-auth/schema.js +0 -110
- package/dist/components/auth-client.d.ts +0 -50
- package/dist/components/auth-client.js +0 -46
|
@@ -1,22 +1,30 @@
|
|
|
1
|
-
import { Effect
|
|
1
|
+
import { Effect } from "effect";
|
|
2
2
|
import { FetchHttpClient, HttpClient } from "effect/unstable/http";
|
|
3
3
|
import { AtomHttpApi } from "effect/unstable/reactivity";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
4
|
+
import { HttpApi, HttpApiClient } from "effect/unstable/httpapi";
|
|
5
|
+
import { AuthClientApi } from "../api.js";
|
|
6
|
+
import { defaultBaseUrl } from "../config.js";
|
|
7
7
|
const authClientApiUrl = (baseUrl) => {
|
|
8
|
-
const url = baseUrl?.trim() ||
|
|
9
|
-
(typeof window === "undefined" ? defaultBaseUrl() : window.location.origin);
|
|
8
|
+
const url = baseUrl?.trim() || globalThis.window?.location.origin || defaultBaseUrl();
|
|
10
9
|
return url.replace(/\/$/, "");
|
|
11
10
|
};
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
const withCredentials = (client) => HttpClient.makeWith((request) => client.postprocess(request).pipe(Effect.provideService(FetchHttpClient.RequestInit, {
|
|
12
|
+
credentials: "include",
|
|
13
|
+
})), client.preprocess);
|
|
14
|
+
export const authHttpClient = (baseUrl) => HttpApiClient.make(AuthClientApi, {
|
|
15
|
+
baseUrl: authClientApiUrl(baseUrl),
|
|
16
|
+
transformClient: withCredentials,
|
|
17
|
+
}).pipe(Effect.provide(FetchHttpClient.layer));
|
|
18
|
+
const createAuthClientApiClient = (key) => {
|
|
19
|
+
class AuthClientApiClient extends AtomHttpApi.Service()(`AuthClientApi:${key}`, {
|
|
20
|
+
api: AuthClientApi,
|
|
21
|
+
baseUrl: key,
|
|
22
|
+
httpClient: FetchHttpClient.layer,
|
|
23
|
+
transformClient: withCredentials,
|
|
24
|
+
}) {
|
|
25
|
+
}
|
|
26
|
+
return AuthClientApiClient;
|
|
27
|
+
};
|
|
20
28
|
const authClientApis = new Map();
|
|
21
29
|
export const authClientApi = (baseUrl) => {
|
|
22
30
|
const key = authClientApiUrl(baseUrl);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type KrakstackAuthLocale } from "./auth-provider";
|
|
1
|
+
import { type KrakstackAuthLocale } from "./auth-provider.js";
|
|
3
2
|
type AuthFormProps = {
|
|
4
|
-
authClient?: AuthUiClient;
|
|
5
3
|
baseUrl?: string | undefined;
|
|
6
4
|
locale?: KrakstackAuthLocale | undefined;
|
|
7
5
|
};
|
|
@@ -12,10 +12,11 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/co
|
|
|
12
12
|
import { CheckboxField, ErrorMessage, SubmitError, TextField, effectFormMessages, } from "@krak-stack/registry/effect-form";
|
|
13
13
|
import { Field, FieldLabel } from "@/components/ui/field";
|
|
14
14
|
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
15
|
+
import { getLastUsedLoginMethod, notifyAuthChange } from "./auth-atoms.js";
|
|
16
|
+
import { authClientApi, authHttpClient } from "./auth-client-api.js";
|
|
17
|
+
import { useKrakstackAuth } from "./auth-provider.js";
|
|
18
|
+
import { cn } from "./utils.js";
|
|
19
|
+
import { ExtraProjectPublicConfig as ExtraProjectPublicConfigSchema, } from "../extra/schema.js";
|
|
19
20
|
const EMAIL_OTP_RESEND_COOLDOWN_SECONDS = 60;
|
|
20
21
|
const signinFormBuilder = FormBuilder.empty
|
|
21
22
|
.addField("email", Schema.String)
|
|
@@ -142,14 +143,9 @@ const defaultMessages = {
|
|
|
142
143
|
const labels = (locale) => ({
|
|
143
144
|
...defaultMessages[locale],
|
|
144
145
|
});
|
|
145
|
-
const useAuthFormOptions = ({
|
|
146
|
+
const useAuthFormOptions = ({ baseUrl, locale }) => {
|
|
146
147
|
const auth = useKrakstackAuth();
|
|
147
|
-
const resolvedAuthClient = authClient ?? auth?.authClient;
|
|
148
|
-
if (!resolvedAuthClient) {
|
|
149
|
-
throw new Error("Krakstack auth components require an authClient prop or KrakstackAuthProvider.");
|
|
150
|
-
}
|
|
151
148
|
return {
|
|
152
|
-
authClient: resolvedAuthClient,
|
|
153
149
|
baseUrl: baseUrl ?? auth?.baseUrl,
|
|
154
150
|
labels: labels(locale ?? auth?.locale ?? "en"),
|
|
155
151
|
projectConfig: auth?.projectConfig,
|
|
@@ -159,13 +155,16 @@ const useAuthProjectConfig = (baseUrl, searchString, projectConfig) => {
|
|
|
159
155
|
const projectId = getSearchParam(searchString, "projectId");
|
|
160
156
|
const clientId = getSearchParam(searchString, "client_id");
|
|
161
157
|
const host = getBrowserAuthHost();
|
|
158
|
+
let query = {};
|
|
159
|
+
if (projectId)
|
|
160
|
+
query = { ...query, projectId };
|
|
161
|
+
if (clientId)
|
|
162
|
+
query = { ...query, clientId };
|
|
163
|
+
if (host)
|
|
164
|
+
query = { ...query, host };
|
|
162
165
|
const atom = projectConfig === undefined
|
|
163
166
|
? authClientApi(baseUrl).query("authExtra", "getProjectPublicConfig", {
|
|
164
|
-
query
|
|
165
|
-
...(projectId ? { projectId } : {}),
|
|
166
|
-
...(clientId ? { clientId } : {}),
|
|
167
|
-
...(host ? { host } : {}),
|
|
168
|
-
},
|
|
167
|
+
query,
|
|
169
168
|
timeToLive: "5 minutes",
|
|
170
169
|
reactivityKeys: [
|
|
171
170
|
"project-public-config",
|
|
@@ -205,7 +204,7 @@ export const resolveInitialAuthMethod = ({ requestedMethod, lastLoginMethod, ema
|
|
|
205
204
|
return emailOtp ? "emailOtp" : "password";
|
|
206
205
|
};
|
|
207
206
|
export function Signin(props) {
|
|
208
|
-
const {
|
|
207
|
+
const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
209
208
|
const navigate = useNavigate();
|
|
210
209
|
const searchString = useRouterState({
|
|
211
210
|
select: (state) => state.location.searchStr,
|
|
@@ -226,7 +225,7 @@ export function Signin(props) {
|
|
|
226
225
|
const requestedMethod = getSearchParam(searchString, "method");
|
|
227
226
|
const [initialAuthMethod] = useState(() => resolveInitialAuthMethod({
|
|
228
227
|
requestedMethod,
|
|
229
|
-
lastLoginMethod:
|
|
228
|
+
lastLoginMethod: getLastUsedLoginMethod(),
|
|
230
229
|
emailPassword: options.emailPassword,
|
|
231
230
|
emailOtp: options.emailOtp,
|
|
232
231
|
}));
|
|
@@ -259,108 +258,108 @@ export function Signin(props) {
|
|
|
259
258
|
return () => window.clearInterval(interval);
|
|
260
259
|
}, [emailOtpResendAvailableAt]);
|
|
261
260
|
const [form] = useState(() => FormReact.make(signinFormBuilder, {
|
|
261
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
262
262
|
fields: { email: TextField, password: TextField, otp: OtpField },
|
|
263
263
|
mode: { validation: "onSubmit" },
|
|
264
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
264
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
265
|
+
const email = value.email.trim();
|
|
266
|
+
const sendEmailOtp = authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
|
|
267
|
+
payload: { email, type: "sign-in" },
|
|
268
|
+
})), Effect.tap(() => Effect.sync(() => {
|
|
269
|
+
setAuthMethod("emailOtp");
|
|
270
|
+
setEmailSubmitted(true);
|
|
271
|
+
setOtpSentTo(email);
|
|
272
|
+
navigate({
|
|
273
|
+
to: "/sign-in",
|
|
274
|
+
search: authMethodSearch(searchString, "emailOtp", email),
|
|
275
|
+
replace: true,
|
|
276
|
+
});
|
|
277
|
+
setEmailOtpResendAvailableAt(Date.now() + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
278
|
+
})));
|
|
279
|
+
if (action.type === "sendEmailOtp") {
|
|
280
|
+
yield* sendEmailOtp;
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!action.emailSubmitted) {
|
|
284
|
+
if (action.method === "emailOtp")
|
|
285
|
+
yield* sendEmailOtp;
|
|
286
|
+
else {
|
|
276
287
|
setEmailSubmitted(true);
|
|
277
|
-
setOtpSentTo(email);
|
|
278
288
|
navigate({
|
|
279
289
|
to: "/sign-in",
|
|
280
|
-
search: authMethodSearch(searchString, "
|
|
290
|
+
search: authMethodSearch(searchString, "password", email),
|
|
281
291
|
replace: true,
|
|
282
292
|
});
|
|
283
|
-
setEmailOtpResendAvailableAt(Date.now() + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
284
|
-
};
|
|
285
|
-
if (action.type === "sendEmailOtp") {
|
|
286
|
-
await sendEmailOtp();
|
|
287
|
-
return;
|
|
288
293
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
navigate({
|
|
295
|
-
to: "/sign-in",
|
|
296
|
-
search: authMethodSearch(searchString, "password", email),
|
|
297
|
-
replace: true,
|
|
298
|
-
});
|
|
299
|
-
}
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (action.method === "emailOtp") {
|
|
297
|
+
if (!action.otpSent) {
|
|
298
|
+
yield* sendEmailOtp;
|
|
300
299
|
return;
|
|
301
300
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
await sendEmailOtp();
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
const result = await authClient.signIn.emailOtp({
|
|
301
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmailOtp({
|
|
302
|
+
payload: {
|
|
308
303
|
email,
|
|
309
304
|
otp: value.otp.trim(),
|
|
310
305
|
name: nameFromEmail(email),
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
306
|
+
},
|
|
307
|
+
})));
|
|
308
|
+
notifyAuthChange();
|
|
309
|
+
onNavigate(redirectTarget);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const payload = oauthQuery
|
|
313
|
+
? {
|
|
319
314
|
email,
|
|
320
315
|
password: value.password,
|
|
321
316
|
callbackURL: redirectTarget,
|
|
322
|
-
|
|
323
|
-
});
|
|
324
|
-
if (result.error) {
|
|
325
|
-
throw new Error(result.error.message ?? m.sign_in_error);
|
|
326
|
-
}
|
|
327
|
-
if (hasTwoFactorRedirect(result.data)) {
|
|
328
|
-
onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
|
|
329
|
-
return;
|
|
317
|
+
oauth_query: oauthQuery,
|
|
330
318
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
})
|
|
319
|
+
: {
|
|
320
|
+
email,
|
|
321
|
+
password: value.password,
|
|
322
|
+
callbackURL: redirectTarget,
|
|
323
|
+
};
|
|
324
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmail({ payload })));
|
|
325
|
+
if (hasTwoFactorRedirect(result)) {
|
|
326
|
+
onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
notifyAuthChange();
|
|
330
|
+
onNavigate(result.url ?? redirectTarget);
|
|
331
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
332
|
+
? cause
|
|
333
|
+
: new Error(action.type === "sendEmailOtp" ||
|
|
334
|
+
(action.type === "submit" &&
|
|
335
|
+
action.method === "emailOtp" &&
|
|
336
|
+
(!action.emailSubmitted || !action.otpSent))
|
|
337
|
+
? m.sign_in_email_otp_send_error
|
|
338
|
+
: action.type === "submit" && action.method === "emailOtp"
|
|
339
|
+
? m.sign_in_email_otp_error
|
|
340
|
+
: m.sign_in_error))),
|
|
344
341
|
}));
|
|
345
342
|
const submit = useAtomSet(form.submit);
|
|
346
343
|
const submitResult = useAtomValue(form.submit);
|
|
347
|
-
const [socialSignIn] = useState(() =>
|
|
348
|
-
|
|
349
|
-
|
|
344
|
+
const [socialSignIn] = useState(() => authClientApi(baseUrl).runtime.fn((provider) => Effect.gen(function* () {
|
|
345
|
+
const payload = oauthQuery
|
|
346
|
+
? {
|
|
350
347
|
provider,
|
|
351
348
|
callbackURL: socialRedirectTarget,
|
|
352
349
|
errorCallbackURL: "/sign-in",
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
});
|
|
356
|
-
if (result.error) {
|
|
357
|
-
throw new Error(result.error.message ?? m.sign_in_error);
|
|
350
|
+
additionalData: { query: oauthQuery },
|
|
351
|
+
oauth_query: oauthQuery,
|
|
358
352
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
353
|
+
: {
|
|
354
|
+
provider,
|
|
355
|
+
callbackURL: socialRedirectTarget,
|
|
356
|
+
errorCallbackURL: "/sign-in",
|
|
357
|
+
};
|
|
358
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInSocial({ payload })));
|
|
359
|
+
notifyAuthChange();
|
|
360
|
+
if (result.url)
|
|
361
|
+
onNavigate(result.url);
|
|
362
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.sign_in_error)))));
|
|
364
363
|
const startSocialSignIn = useAtomSet(socialSignIn);
|
|
365
364
|
const socialSignInResult = useAtomValue(socialSignIn);
|
|
366
365
|
const formValues = Option.getOrElse(useAtomValue(form.values), () => ({
|
|
@@ -433,7 +432,7 @@ export function Signin(props) {
|
|
|
433
432
|
: 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] })] }));
|
|
434
433
|
}
|
|
435
434
|
export function VerifyEmail(props) {
|
|
436
|
-
const {
|
|
435
|
+
const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
437
436
|
const navigate = useNavigate();
|
|
438
437
|
const searchString = useRouterState({
|
|
439
438
|
select: (state) => state.location.searchStr,
|
|
@@ -443,36 +442,33 @@ export function VerifyEmail(props) {
|
|
|
443
442
|
const onNavigate = (target) => navigateTarget(target, navigate);
|
|
444
443
|
const [resent, setResent] = useState(false);
|
|
445
444
|
const [form] = useState(() => FormReact.make(verifyEmailFormBuilder, {
|
|
445
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
446
446
|
fields: { email: TextField, otp: OtpField },
|
|
447
447
|
mode: { validation: "onSubmit" },
|
|
448
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
448
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
449
|
+
if (action === "resend") {
|
|
450
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
|
|
451
|
+
payload: {
|
|
452
452
|
email: value.email.trim(),
|
|
453
453
|
type: "email-verification",
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const result = await authClient.emailOtp.verifyEmail({
|
|
454
|
+
},
|
|
455
|
+
})));
|
|
456
|
+
setResent(true);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.verifyEmailOtp({
|
|
460
|
+
payload: {
|
|
462
461
|
email: value.email.trim(),
|
|
463
462
|
otp: value.otp.trim(),
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
?
|
|
472
|
-
:
|
|
473
|
-
? m.verify_email_resend_error
|
|
474
|
-
: m.verify_email_error),
|
|
475
|
-
}),
|
|
463
|
+
},
|
|
464
|
+
})));
|
|
465
|
+
notifyAuthChange();
|
|
466
|
+
onNavigate(redirectTarget);
|
|
467
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
468
|
+
? cause
|
|
469
|
+
: new Error(action === "resend"
|
|
470
|
+
? m.verify_email_resend_error
|
|
471
|
+
: m.verify_email_error))),
|
|
476
472
|
}));
|
|
477
473
|
const submit = useAtomSet(form.submit);
|
|
478
474
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -487,29 +483,26 @@ export function VerifyEmail(props) {
|
|
|
487
483
|
}, 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 }) })] })] }));
|
|
488
484
|
}
|
|
489
485
|
export function ForgotPassword(props) {
|
|
490
|
-
const {
|
|
486
|
+
const { baseUrl, labels: m } = useAuthFormOptions(props);
|
|
491
487
|
const searchString = useRouterState({
|
|
492
488
|
select: (state) => state.location.searchStr,
|
|
493
489
|
});
|
|
494
490
|
const initialEmail = getSearchParam(searchString, "email")?.trim() ?? "";
|
|
495
491
|
const [submitted, setSubmitted] = useState(false);
|
|
496
492
|
const [form] = useState(() => FormReact.make(forgotPasswordFormBuilder, {
|
|
493
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
497
494
|
fields: { email: TextField },
|
|
498
495
|
mode: { validation: "onSubmit" },
|
|
499
|
-
onSubmit: (_, { decoded: value }) => Effect.
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
496
|
+
onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
|
|
497
|
+
setSubmitted(false);
|
|
498
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.requestPasswordReset({
|
|
499
|
+
payload: {
|
|
503
500
|
email: value.email.trim(),
|
|
504
501
|
redirectTo: `${window.location.origin}/reset-password`,
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
setSubmitted(true);
|
|
510
|
-
},
|
|
511
|
-
catch: (cause) => cause instanceof Error ? cause : new Error(m.forgot_password_error),
|
|
512
|
-
}),
|
|
502
|
+
},
|
|
503
|
+
})));
|
|
504
|
+
setSubmitted(true);
|
|
505
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.forgot_password_error))),
|
|
513
506
|
}));
|
|
514
507
|
const submit = useAtomSet(form.submit);
|
|
515
508
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -527,25 +520,22 @@ export function ResetPassword(props) {
|
|
|
527
520
|
return token ? (_jsx(ResetPasswordForm, { ...props, token: token })) : (_jsx(ForgotPassword, { ...props }));
|
|
528
521
|
}
|
|
529
522
|
function ResetPasswordForm({ token, ...props }) {
|
|
530
|
-
const {
|
|
523
|
+
const { baseUrl, labels: m } = useAuthFormOptions(props);
|
|
531
524
|
const navigate = useNavigate();
|
|
532
525
|
const onSuccess = () => navigate({ to: "/sign-in" });
|
|
533
526
|
const [form] = useState(() => FormReact.make(resetPasswordFormBuilder, {
|
|
527
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
534
528
|
fields: { password: TextField },
|
|
535
529
|
mode: { validation: "onSubmit" },
|
|
536
|
-
onSubmit: (_, { decoded: value }) => Effect.
|
|
537
|
-
|
|
538
|
-
|
|
530
|
+
onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
|
|
531
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.resetPassword({
|
|
532
|
+
payload: {
|
|
539
533
|
newPassword: value.password,
|
|
540
534
|
token,
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
onSuccess();
|
|
546
|
-
},
|
|
547
|
-
catch: (cause) => cause instanceof Error ? cause : new Error(m.reset_password_error),
|
|
548
|
-
}),
|
|
535
|
+
},
|
|
536
|
+
})));
|
|
537
|
+
onSuccess();
|
|
538
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.reset_password_error))),
|
|
549
539
|
}));
|
|
550
540
|
const submit = useAtomSet(form.submit);
|
|
551
541
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -556,7 +546,7 @@ function ResetPasswordForm({ token, ...props }) {
|
|
|
556
546
|
}, 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] })] }) }) })] }));
|
|
557
547
|
}
|
|
558
548
|
export function TwoFactor(props) {
|
|
559
|
-
const {
|
|
549
|
+
const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
560
550
|
const navigate = useNavigate();
|
|
561
551
|
const searchString = useRouterState({
|
|
562
552
|
select: (state) => state.location.searchStr,
|
|
@@ -568,48 +558,42 @@ export function TwoFactor(props) {
|
|
|
568
558
|
const [mode, setMode] = useState("totp");
|
|
569
559
|
const [emailCodeSent, setEmailCodeSent] = useState(false);
|
|
570
560
|
const [form] = useState(() => FormReact.make(twoFactorFormBuilder, {
|
|
561
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
571
562
|
fields: { code: AuthCodeField, trustDevice: CheckboxField },
|
|
572
563
|
mode: { validation: "onSubmit" },
|
|
573
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
564
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
565
|
+
if (action.type === "sendEmailCode") {
|
|
566
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorSendOtp({ payload: {} })));
|
|
567
|
+
setEmailCodeSent(true);
|
|
568
|
+
setMode("email");
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const code = value.code.trim();
|
|
572
|
+
const verification = oauthQuery
|
|
573
|
+
? {
|
|
574
|
+
code,
|
|
575
|
+
trustDevice: value.trustDevice,
|
|
576
|
+
oauth_query: oauthQuery,
|
|
583
577
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
578
|
+
: { code, trustDevice: value.trustDevice };
|
|
579
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => action.mode === "backup"
|
|
580
|
+
? client.auth.twoFactorVerifyBackupCode({
|
|
581
|
+
payload: verification,
|
|
582
|
+
})
|
|
583
|
+
: action.mode === "email"
|
|
584
|
+
? client.auth.twoFactorVerifyOtp({
|
|
585
|
+
payload: verification,
|
|
590
586
|
})
|
|
591
|
-
:
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
});
|
|
602
|
-
if (result.error) {
|
|
603
|
-
throw new Error(result.error.message ?? m.two_factor_verify_error);
|
|
604
|
-
}
|
|
605
|
-
onNavigate(getResultRedirectUrl(result.data) ?? redirectTarget);
|
|
606
|
-
},
|
|
607
|
-
catch: (cause) => cause instanceof Error
|
|
608
|
-
? cause
|
|
609
|
-
: new Error(action.type === "sendEmailCode"
|
|
610
|
-
? m.two_factor_send_email_code_error
|
|
611
|
-
: m.two_factor_verify_error),
|
|
612
|
-
}),
|
|
587
|
+
: client.auth.twoFactorVerifyTotp({
|
|
588
|
+
payload: verification,
|
|
589
|
+
})));
|
|
590
|
+
notifyAuthChange();
|
|
591
|
+
onNavigate(getResultRedirectUrl(result) ?? redirectTarget);
|
|
592
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
593
|
+
? cause
|
|
594
|
+
: new Error(action.type === "sendEmailCode"
|
|
595
|
+
? m.two_factor_send_email_code_error
|
|
596
|
+
: m.two_factor_verify_error))),
|
|
613
597
|
}));
|
|
614
598
|
const submit = useAtomSet(form.submit);
|
|
615
599
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -643,16 +627,10 @@ const AuthCodeField = ({ field, props }) => {
|
|
|
643
627
|
};
|
|
644
628
|
const Divider = ({ label }) => (_jsxs("div", { className: "text-muted-foreground flex items-center gap-3 text-xs", children: [_jsx("span", { className: "bg-border h-px flex-1" }), label, _jsx("span", { className: "bg-border h-px flex-1" })] }));
|
|
645
629
|
const GoogleLogo = () => (_jsxs("svg", { "aria-hidden": "true", className: "size-[18px]", viewBox: "0 0 18 18", xmlns: "http://www.w3.org/2000/svg", children: [_jsx("path", { d: "M17.64 9.204c0-.638-.057-1.252-.164-1.84H9v3.481h4.844a4.14 4.14 0 0 1-1.796 2.716v2.259h2.908c1.702-1.567 2.684-3.874 2.684-6.616Z", fill: "#4285F4" }), _jsx("path", { d: "M9 18c2.43 0 4.467-.806 5.956-2.18l-2.908-2.259c-.806.54-1.837.86-3.048.86-2.344 0-4.328-1.583-5.036-3.711H.957v2.332A8.997 8.997 0 0 0 9 18Z", fill: "#34A853" }), _jsx("path", { d: "M3.964 10.71A5.41 5.41 0 0 1 3.682 9c0-.593.102-1.17.282-1.71V4.958H.957A8.996 8.996 0 0 0 0 9c0 1.452.348 2.827.957 4.042l3.007-2.332Z", fill: "#FBBC05" }), _jsx("path", { d: "M9 3.58c1.321 0 2.508.454 3.44 1.346l2.582-2.58C13.463.891 11.426 0 9 0A8.997 8.997 0 0 0 .957 4.958L3.964 7.29C4.672 5.162 6.656 3.58 9 3.58Z", fill: "#EA4335" })] }));
|
|
646
|
-
const
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
const getResultRedirectUrl = (data) => {
|
|
651
|
-
if (typeof data !== "object" || data === null || !("url" in data))
|
|
652
|
-
return null;
|
|
653
|
-
const url = data.url;
|
|
654
|
-
return typeof url === "string" ? url : null;
|
|
655
|
-
};
|
|
630
|
+
const TwoFactorRedirect = Schema.Struct({ twoFactorRedirect: Schema.Boolean });
|
|
631
|
+
const RedirectResult = Schema.Struct({ url: Schema.String });
|
|
632
|
+
const hasTwoFactorRedirect = (data) => Option.exists(Schema.decodeUnknownOption(TwoFactorRedirect)(data), (result) => result.twoFactorRedirect);
|
|
633
|
+
const getResultRedirectUrl = (data) => Option.map(Schema.decodeUnknownOption(RedirectResult)(data), (result) => result.url).pipe(Option.getOrNull);
|
|
656
634
|
const getRedirectTarget = (searchString, projectAuthDomain, projectRootDomain) => {
|
|
657
635
|
const oauthTarget = getOAuthAuthorizeTarget(searchString);
|
|
658
636
|
if (oauthTarget)
|
|
@@ -718,7 +696,7 @@ const getDefaultAuthRedirectTarget = (_projectAuthDomain, _projectRootDomain) =>
|
|
|
718
696
|
return "/admin";
|
|
719
697
|
};
|
|
720
698
|
const getSearchParam = (searchString, key) => new URLSearchParams(searchString).get(key);
|
|
721
|
-
const getBrowserAuthHost = () =>
|
|
699
|
+
const getBrowserAuthHost = () => globalThis.window?.location.host ?? null;
|
|
722
700
|
const navigateTarget = (target, navigate) => {
|
|
723
701
|
if (shouldUseDocumentRedirect(target)) {
|
|
724
702
|
navigate({ href: target });
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import type { ProjectAccessCatalog, ProjectAccessLabelCatalog } from "../access";
|
|
2
|
+
import type { ExtraProjectPublicConfig } from "../extra/schema.js";
|
|
3
|
+
import type { ProjectAccessCatalog, ProjectAccessLabelCatalog } from "../access.js";
|
|
5
4
|
export type KrakstackAuthLocale = "en" | "fr";
|
|
6
5
|
export type KrakstackAuthProviderProps = {
|
|
7
6
|
children: ReactNode;
|
|
8
7
|
locale?: KrakstackAuthLocale | undefined;
|
|
9
8
|
baseUrl?: string | undefined;
|
|
10
9
|
projectId?: string | null | undefined;
|
|
11
|
-
authClient?: AuthUiClient | undefined;
|
|
12
10
|
access?: ProjectAccessCatalog | undefined;
|
|
13
11
|
accessLabels?: ProjectAccessLabelCatalog | undefined;
|
|
14
12
|
};
|
|
@@ -16,7 +14,6 @@ export type KrakstackAuthContextValue = {
|
|
|
16
14
|
locale: KrakstackAuthLocale;
|
|
17
15
|
baseUrl?: string | undefined;
|
|
18
16
|
projectId?: string | null | undefined;
|
|
19
|
-
authClient: AuthUiClient;
|
|
20
17
|
projectConfig: ExtraProjectPublicConfig | null;
|
|
21
18
|
access: ProjectAccessCatalog | null;
|
|
22
19
|
accessLabels: ProjectAccessLabelCatalog | null;
|
|
@@ -24,22 +21,21 @@ export type KrakstackAuthContextValue = {
|
|
|
24
21
|
refreshAuth: () => void;
|
|
25
22
|
};
|
|
26
23
|
export declare const listenForSessionRevalidation: (target: EventTarget, revalidate: () => void) => () => void;
|
|
27
|
-
export declare function KrakstackAuthProvider({ children, locale, baseUrl, projectId,
|
|
24
|
+
export declare function KrakstackAuthProvider({ children, locale, baseUrl, projectId, access, accessLabels, }: KrakstackAuthProviderProps): import("react").JSX.Element;
|
|
28
25
|
export declare const useKrakstackAuth: () => KrakstackAuthContextValue | null;
|
|
29
|
-
export declare const
|
|
30
|
-
|
|
31
|
-
readonly
|
|
32
|
-
readonly
|
|
33
|
-
readonly
|
|
34
|
-
readonly
|
|
35
|
-
readonly
|
|
36
|
-
readonly
|
|
37
|
-
|
|
38
|
-
readonly
|
|
39
|
-
readonly
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}, "Type"> | null;
|
|
26
|
+
export declare const useKrakstackAuthProjectConfig: () => {
|
|
27
|
+
readonly projectKey: string;
|
|
28
|
+
readonly name: string | null;
|
|
29
|
+
readonly logoUrl: string | null;
|
|
30
|
+
readonly authDomain: string | null;
|
|
31
|
+
readonly rootDomain: string | null;
|
|
32
|
+
readonly themeCss: string | null;
|
|
33
|
+
readonly authOptions: {
|
|
34
|
+
readonly emailPassword: boolean;
|
|
35
|
+
readonly emailOtp: boolean;
|
|
36
|
+
readonly google: boolean;
|
|
37
|
+
};
|
|
38
|
+
} | null;
|
|
43
39
|
export declare const usePermissions: () => {
|
|
44
40
|
loading: boolean;
|
|
45
41
|
permissions: Set<string>;
|