@krak-stack/auth 0.1.70 → 0.2.0
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 +4 -4
- package/dist/access.js +10 -4
- package/dist/admin/api.group.d.ts +17 -17
- package/dist/admin/api.group.js +2 -2
- package/dist/admin/schema.d.ts +18 -18
- package/dist/admin/schema.js +3 -3
- package/dist/api.d.ts +2093 -631
- 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 +800 -0
- package/dist/auth/schema.js +625 -0
- package/dist/components/admin-organization-form.d.ts +1 -1
- package/dist/components/admin-organization-form.js +16 -18
- package/dist/components/admin-organizations.js +52 -46
- package/dist/components/admin-users.js +115 -138
- package/dist/components/api-key-edit-form.d.ts +4 -3
- package/dist/components/api-key-edit-form.js +32 -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 +2474 -142
- package/dist/components/auth-client-api.js +14 -10
- package/dist/components/auth-forms.d.ts +1 -3
- package/dist/components/auth-forms.js +168 -195
- package/dist/components/auth-provider.d.ts +3 -7
- 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 +469 -324
- 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 +265 -316
- package/dist/components/utils.d.ts +3 -1
- package/dist/components/utils.js +4 -11
- package/dist/extra/api.group.d.ts +3 -3
- package/dist/extra/api.group.js +6 -6
- package/dist/extra/schema.js +1 -1
- package/dist/index.d.ts +11 -12
- package/dist/index.js +11 -12
- package/dist/query.d.ts +2 -2
- 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 +30 -32
- package/dist/server/actor.js +39 -22
- package/dist/server/api.group.js +2 -2
- package/dist/server/index.d.ts +14 -14
- package/dist/server/index.js +8 -8
- package/dist/server/middleware.d.ts +3 -3
- package/dist/server/middleware.js +10 -5
- package/dist/server/schema.js +1 -1
- package/dist/service.d.ts +2053 -201
- package/dist/service.js +32 -21
- 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,21 +1,25 @@
|
|
|
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 { 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
|
};
|
|
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));
|
|
12
18
|
const createAuthClientApiClient = (key) => AtomHttpApi.Service()(`AuthClientApi:${key}`, {
|
|
13
19
|
api: AuthClientApi,
|
|
14
20
|
baseUrl: key,
|
|
15
|
-
httpClient:
|
|
16
|
-
transformClient:
|
|
17
|
-
credentials: "include",
|
|
18
|
-
})), client.preprocess),
|
|
21
|
+
httpClient: FetchHttpClient.layer,
|
|
22
|
+
transformClient: withCredentials,
|
|
19
23
|
});
|
|
20
24
|
const authClientApis = new Map();
|
|
21
25
|
export const authClientApi = (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
|
}));
|
|
@@ -261,106 +260,105 @@ export function Signin(props) {
|
|
|
261
260
|
const [form] = useState(() => FormReact.make(signinFormBuilder, {
|
|
262
261
|
fields: { email: TextField, password: TextField, otp: OtpField },
|
|
263
262
|
mode: { validation: "onSubmit" },
|
|
264
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
263
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
264
|
+
const email = value.email.trim();
|
|
265
|
+
const sendEmailOtp = authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
|
|
266
|
+
payload: { email, type: "sign-in" },
|
|
267
|
+
})), Effect.tap(() => Effect.sync(() => {
|
|
268
|
+
setAuthMethod("emailOtp");
|
|
269
|
+
setEmailSubmitted(true);
|
|
270
|
+
setOtpSentTo(email);
|
|
271
|
+
navigate({
|
|
272
|
+
to: "/sign-in",
|
|
273
|
+
search: authMethodSearch(searchString, "emailOtp", email),
|
|
274
|
+
replace: true,
|
|
275
|
+
});
|
|
276
|
+
setEmailOtpResendAvailableAt(Date.now() + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
277
|
+
})));
|
|
278
|
+
if (action.type === "sendEmailOtp") {
|
|
279
|
+
yield* sendEmailOtp;
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (!action.emailSubmitted) {
|
|
283
|
+
if (action.method === "emailOtp")
|
|
284
|
+
yield* sendEmailOtp;
|
|
285
|
+
else {
|
|
276
286
|
setEmailSubmitted(true);
|
|
277
|
-
setOtpSentTo(email);
|
|
278
287
|
navigate({
|
|
279
288
|
to: "/sign-in",
|
|
280
|
-
search: authMethodSearch(searchString, "
|
|
289
|
+
search: authMethodSearch(searchString, "password", email),
|
|
281
290
|
replace: true,
|
|
282
291
|
});
|
|
283
|
-
setEmailOtpResendAvailableAt(Date.now() + EMAIL_OTP_RESEND_COOLDOWN_SECONDS * 1000);
|
|
284
|
-
};
|
|
285
|
-
if (action.type === "sendEmailOtp") {
|
|
286
|
-
await sendEmailOtp();
|
|
287
|
-
return;
|
|
288
292
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
navigate({
|
|
295
|
-
to: "/sign-in",
|
|
296
|
-
search: authMethodSearch(searchString, "password", email),
|
|
297
|
-
replace: true,
|
|
298
|
-
});
|
|
299
|
-
}
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (action.method === "emailOtp") {
|
|
296
|
+
if (!action.otpSent) {
|
|
297
|
+
yield* sendEmailOtp;
|
|
300
298
|
return;
|
|
301
299
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
await sendEmailOtp();
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
const result = await authClient.signIn.emailOtp({
|
|
300
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmailOtp({
|
|
301
|
+
payload: {
|
|
308
302
|
email,
|
|
309
303
|
otp: value.otp.trim(),
|
|
310
304
|
name: nameFromEmail(email),
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
305
|
+
},
|
|
306
|
+
})));
|
|
307
|
+
notifyAuthChange();
|
|
308
|
+
onNavigate(redirectTarget);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const payload = oauthQuery
|
|
312
|
+
? {
|
|
319
313
|
email,
|
|
320
314
|
password: value.password,
|
|
321
315
|
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;
|
|
316
|
+
oauth_query: oauthQuery,
|
|
330
317
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
})
|
|
318
|
+
: {
|
|
319
|
+
email,
|
|
320
|
+
password: value.password,
|
|
321
|
+
callbackURL: redirectTarget,
|
|
322
|
+
};
|
|
323
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInEmail({ payload })));
|
|
324
|
+
if (hasTwoFactorRedirect(result)) {
|
|
325
|
+
onTwoFactorRedirect(`/2fa${oauthQuery ? `?${oauthQuery}` : `?callbackURL=${encodeURIComponent(redirectTarget)}`}`);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
notifyAuthChange();
|
|
329
|
+
onNavigate(result.url ?? redirectTarget);
|
|
330
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
331
|
+
? cause
|
|
332
|
+
: new Error(action.type === "sendEmailOtp" ||
|
|
333
|
+
(action.type === "submit" &&
|
|
334
|
+
action.method === "emailOtp" &&
|
|
335
|
+
(!action.emailSubmitted || !action.otpSent))
|
|
336
|
+
? m.sign_in_email_otp_send_error
|
|
337
|
+
: action.type === "submit" && action.method === "emailOtp"
|
|
338
|
+
? m.sign_in_email_otp_error
|
|
339
|
+
: m.sign_in_error))),
|
|
344
340
|
}));
|
|
345
341
|
const submit = useAtomSet(form.submit);
|
|
346
342
|
const submitResult = useAtomValue(form.submit);
|
|
347
|
-
const [socialSignIn] = useState(() => Atom.fn((provider) => Effect.
|
|
348
|
-
|
|
349
|
-
|
|
343
|
+
const [socialSignIn] = useState(() => Atom.fn((provider) => Effect.gen(function* () {
|
|
344
|
+
const payload = oauthQuery
|
|
345
|
+
? {
|
|
350
346
|
provider,
|
|
351
347
|
callbackURL: socialRedirectTarget,
|
|
352
348
|
errorCallbackURL: "/sign-in",
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
});
|
|
356
|
-
if (result.error) {
|
|
357
|
-
throw new Error(result.error.message ?? m.sign_in_error);
|
|
349
|
+
additionalData: { query: oauthQuery },
|
|
350
|
+
oauth_query: oauthQuery,
|
|
358
351
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
352
|
+
: {
|
|
353
|
+
provider,
|
|
354
|
+
callbackURL: socialRedirectTarget,
|
|
355
|
+
errorCallbackURL: "/sign-in",
|
|
356
|
+
};
|
|
357
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signInSocial({ payload })));
|
|
358
|
+
notifyAuthChange();
|
|
359
|
+
if (result.url)
|
|
360
|
+
onNavigate(result.url);
|
|
361
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.sign_in_error)))));
|
|
364
362
|
const startSocialSignIn = useAtomSet(socialSignIn);
|
|
365
363
|
const socialSignInResult = useAtomValue(socialSignIn);
|
|
366
364
|
const formValues = Option.getOrElse(useAtomValue(form.values), () => ({
|
|
@@ -433,7 +431,7 @@ export function Signin(props) {
|
|
|
433
431
|
: 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
432
|
}
|
|
435
433
|
export function VerifyEmail(props) {
|
|
436
|
-
const {
|
|
434
|
+
const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
437
435
|
const navigate = useNavigate();
|
|
438
436
|
const searchString = useRouterState({
|
|
439
437
|
select: (state) => state.location.searchStr,
|
|
@@ -445,34 +443,30 @@ export function VerifyEmail(props) {
|
|
|
445
443
|
const [form] = useState(() => FormReact.make(verifyEmailFormBuilder, {
|
|
446
444
|
fields: { email: TextField, otp: OtpField },
|
|
447
445
|
mode: { validation: "onSubmit" },
|
|
448
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
446
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
447
|
+
if (action === "resend") {
|
|
448
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.sendVerificationOtp({
|
|
449
|
+
payload: {
|
|
452
450
|
email: value.email.trim(),
|
|
453
451
|
type: "email-verification",
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const result = await authClient.emailOtp.verifyEmail({
|
|
452
|
+
},
|
|
453
|
+
})));
|
|
454
|
+
setResent(true);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.verifyEmailOtp({
|
|
458
|
+
payload: {
|
|
462
459
|
email: value.email.trim(),
|
|
463
460
|
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
|
-
}),
|
|
461
|
+
},
|
|
462
|
+
})));
|
|
463
|
+
notifyAuthChange();
|
|
464
|
+
onNavigate(redirectTarget);
|
|
465
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
466
|
+
? cause
|
|
467
|
+
: new Error(action === "resend"
|
|
468
|
+
? m.verify_email_resend_error
|
|
469
|
+
: m.verify_email_error))),
|
|
476
470
|
}));
|
|
477
471
|
const submit = useAtomSet(form.submit);
|
|
478
472
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -487,7 +481,7 @@ export function VerifyEmail(props) {
|
|
|
487
481
|
}, 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
482
|
}
|
|
489
483
|
export function ForgotPassword(props) {
|
|
490
|
-
const {
|
|
484
|
+
const { baseUrl, labels: m } = useAuthFormOptions(props);
|
|
491
485
|
const searchString = useRouterState({
|
|
492
486
|
select: (state) => state.location.searchStr,
|
|
493
487
|
});
|
|
@@ -496,20 +490,16 @@ export function ForgotPassword(props) {
|
|
|
496
490
|
const [form] = useState(() => FormReact.make(forgotPasswordFormBuilder, {
|
|
497
491
|
fields: { email: TextField },
|
|
498
492
|
mode: { validation: "onSubmit" },
|
|
499
|
-
onSubmit: (_, { decoded: value }) => Effect.
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
493
|
+
onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
|
|
494
|
+
setSubmitted(false);
|
|
495
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.requestPasswordReset({
|
|
496
|
+
payload: {
|
|
503
497
|
email: value.email.trim(),
|
|
504
498
|
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
|
-
}),
|
|
499
|
+
},
|
|
500
|
+
})));
|
|
501
|
+
setSubmitted(true);
|
|
502
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.forgot_password_error))),
|
|
513
503
|
}));
|
|
514
504
|
const submit = useAtomSet(form.submit);
|
|
515
505
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -527,25 +517,21 @@ export function ResetPassword(props) {
|
|
|
527
517
|
return token ? (_jsx(ResetPasswordForm, { ...props, token: token })) : (_jsx(ForgotPassword, { ...props }));
|
|
528
518
|
}
|
|
529
519
|
function ResetPasswordForm({ token, ...props }) {
|
|
530
|
-
const {
|
|
520
|
+
const { baseUrl, labels: m } = useAuthFormOptions(props);
|
|
531
521
|
const navigate = useNavigate();
|
|
532
522
|
const onSuccess = () => navigate({ to: "/sign-in" });
|
|
533
523
|
const [form] = useState(() => FormReact.make(resetPasswordFormBuilder, {
|
|
534
524
|
fields: { password: TextField },
|
|
535
525
|
mode: { validation: "onSubmit" },
|
|
536
|
-
onSubmit: (_, { decoded: value }) => Effect.
|
|
537
|
-
|
|
538
|
-
|
|
526
|
+
onSubmit: (_, { decoded: value }) => Effect.gen(function* () {
|
|
527
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.resetPassword({
|
|
528
|
+
payload: {
|
|
539
529
|
newPassword: value.password,
|
|
540
530
|
token,
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
onSuccess();
|
|
546
|
-
},
|
|
547
|
-
catch: (cause) => cause instanceof Error ? cause : new Error(m.reset_password_error),
|
|
548
|
-
}),
|
|
531
|
+
},
|
|
532
|
+
})));
|
|
533
|
+
onSuccess();
|
|
534
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error ? cause : new Error(m.reset_password_error))),
|
|
549
535
|
}));
|
|
550
536
|
const submit = useAtomSet(form.submit);
|
|
551
537
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -556,7 +542,7 @@ function ResetPasswordForm({ token, ...props }) {
|
|
|
556
542
|
}, 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
543
|
}
|
|
558
544
|
export function TwoFactor(props) {
|
|
559
|
-
const {
|
|
545
|
+
const { baseUrl, labels: m, projectConfig: providedProjectConfig, } = useAuthFormOptions(props);
|
|
560
546
|
const navigate = useNavigate();
|
|
561
547
|
const searchString = useRouterState({
|
|
562
548
|
select: (state) => state.location.searchStr,
|
|
@@ -570,46 +556,39 @@ export function TwoFactor(props) {
|
|
|
570
556
|
const [form] = useState(() => FormReact.make(twoFactorFormBuilder, {
|
|
571
557
|
fields: { code: AuthCodeField, trustDevice: CheckboxField },
|
|
572
558
|
mode: { validation: "onSubmit" },
|
|
573
|
-
onSubmit: (action, { decoded: value }) => Effect.
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
559
|
+
onSubmit: (action, { decoded: value }) => Effect.gen(function* () {
|
|
560
|
+
if (action.type === "sendEmailCode") {
|
|
561
|
+
yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorSendOtp({ payload: {} })));
|
|
562
|
+
setEmailCodeSent(true);
|
|
563
|
+
setMode("email");
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const code = value.code.trim();
|
|
567
|
+
const verification = oauthQuery
|
|
568
|
+
? {
|
|
569
|
+
code,
|
|
570
|
+
trustDevice: value.trustDevice,
|
|
571
|
+
oauth_query: oauthQuery,
|
|
583
572
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
573
|
+
: { code, trustDevice: value.trustDevice };
|
|
574
|
+
const result = yield* authHttpClient(baseUrl).pipe(Effect.flatMap((client) => action.mode === "backup"
|
|
575
|
+
? client.auth.twoFactorVerifyBackupCode({
|
|
576
|
+
payload: verification,
|
|
577
|
+
})
|
|
578
|
+
: action.mode === "email"
|
|
579
|
+
? client.auth.twoFactorVerifyOtp({
|
|
580
|
+
payload: verification,
|
|
590
581
|
})
|
|
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
|
-
}),
|
|
582
|
+
: client.auth.twoFactorVerifyTotp({
|
|
583
|
+
payload: verification,
|
|
584
|
+
})));
|
|
585
|
+
notifyAuthChange();
|
|
586
|
+
onNavigate(getResultRedirectUrl(result) ?? redirectTarget);
|
|
587
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
588
|
+
? cause
|
|
589
|
+
: new Error(action.type === "sendEmailCode"
|
|
590
|
+
? m.two_factor_send_email_code_error
|
|
591
|
+
: m.two_factor_verify_error))),
|
|
613
592
|
}));
|
|
614
593
|
const submit = useAtomSet(form.submit);
|
|
615
594
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -643,16 +622,10 @@ const AuthCodeField = ({ field, props }) => {
|
|
|
643
622
|
};
|
|
644
623
|
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
624
|
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
|
-
};
|
|
625
|
+
const TwoFactorRedirect = Schema.Struct({ twoFactorRedirect: Schema.Boolean });
|
|
626
|
+
const RedirectResult = Schema.Struct({ url: Schema.String });
|
|
627
|
+
const hasTwoFactorRedirect = (data) => Option.exists(Schema.decodeUnknownOption(TwoFactorRedirect)(data), (result) => result.twoFactorRedirect);
|
|
628
|
+
const getResultRedirectUrl = (data) => Option.map(Schema.decodeUnknownOption(RedirectResult)(data), (result) => result.url).pipe(Option.getOrNull);
|
|
656
629
|
const getRedirectTarget = (searchString, projectAuthDomain, projectRootDomain) => {
|
|
657
630
|
const oauthTarget = getOAuthAuthorizeTarget(searchString);
|
|
658
631
|
if (oauthTarget)
|
|
@@ -718,7 +691,7 @@ const getDefaultAuthRedirectTarget = (_projectAuthDomain, _projectRootDomain) =>
|
|
|
718
691
|
return "/admin";
|
|
719
692
|
};
|
|
720
693
|
const getSearchParam = (searchString, key) => new URLSearchParams(searchString).get(key);
|
|
721
|
-
const getBrowserAuthHost = () =>
|
|
694
|
+
const getBrowserAuthHost = () => globalThis.window?.location.host ?? null;
|
|
722
695
|
const navigateTarget = (target, navigate) => {
|
|
723
696
|
if (shouldUseDocumentRedirect(target)) {
|
|
724
697
|
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,9 +21,8 @@ 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 useAuthClient: () => AuthUiClient;
|
|
30
26
|
export declare const useKrakstackAuthProjectConfig: () => import("effect/Schema").Struct.ReadonlySide<{
|
|
31
27
|
readonly projectKey: import("effect/Schema").String;
|
|
32
28
|
readonly name: import("effect/Schema").NullOr<import("effect/Schema").String>;
|