@shivaedev/identity 0.23.1 → 0.24.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/CHANGELOG.md +12 -0
- package/README.md +34 -0
- package/dist/accountPreferences.d.ts +112 -0
- package/dist/accountPreferences.d.ts.map +1 -0
- package/dist/accountPreferences.js +37 -0
- package/dist/accountPreferences.js.map +1 -0
- package/dist/server/AccountClock.d.ts +11 -0
- package/dist/server/AccountClock.d.ts.map +1 -0
- package/dist/server/AccountClock.js +14 -0
- package/dist/server/AccountClock.js.map +1 -0
- package/dist/server/AccountPreferencesStore.d.ts +25 -0
- package/dist/server/AccountPreferencesStore.d.ts.map +1 -0
- package/dist/server/AccountPreferencesStore.js +13 -0
- package/dist/server/AccountPreferencesStore.js.map +1 -0
- package/dist/server/account-clock/dayRange.d.ts +7 -0
- package/dist/server/account-clock/dayRange.d.ts.map +1 -0
- package/dist/server/account-clock/dayRange.js +11 -0
- package/dist/server/account-clock/dayRange.js.map +1 -0
- package/dist/server/account-clock/initialize.d.ts +7 -0
- package/dist/server/account-clock/initialize.d.ts.map +1 -0
- package/dist/server/account-clock/initialize.js +18 -0
- package/dist/server/account-clock/initialize.js.map +1 -0
- package/dist/server/account-clock/logicalDay.d.ts +4 -0
- package/dist/server/account-clock/logicalDay.d.ts.map +1 -0
- package/dist/server/account-clock/logicalDay.js +10 -0
- package/dist/server/account-clock/logicalDay.js.map +1 -0
- package/dist/server/account-clock/settings.d.ts +5 -0
- package/dist/server/account-clock/settings.d.ts.map +1 -0
- package/dist/server/account-clock/settings.js +8 -0
- package/dist/server/account-clock/settings.js.map +1 -0
- package/dist/server/account-preferences/capture.d.ts +8 -0
- package/dist/server/account-preferences/capture.d.ts.map +1 -0
- package/dist/server/account-preferences/capture.js +8 -0
- package/dist/server/account-preferences/capture.js.map +1 -0
- package/dist/server/account-preferences/get.d.ts +12 -0
- package/dist/server/account-preferences/get.d.ts.map +1 -0
- package/dist/server/account-preferences/get.js +8 -0
- package/dist/server/account-preferences/get.js.map +1 -0
- package/dist/server/account-preferences/initialize.d.ts +59 -0
- package/dist/server/account-preferences/initialize.d.ts.map +1 -0
- package/dist/server/account-preferences/initialize.js +16 -0
- package/dist/server/account-preferences/initialize.js.map +1 -0
- package/dist/server/account-preferences/set.d.ts +8 -0
- package/dist/server/account-preferences/set.d.ts.map +1 -0
- package/dist/server/account-preferences/set.js +10 -0
- package/dist/server/account-preferences/set.js.map +1 -0
- package/dist/server/accountPreferencesHandlers.d.ts +2 -0
- package/dist/server/accountPreferencesHandlers.d.ts.map +1 -0
- package/dist/server/accountPreferencesHandlers.js +19 -0
- package/dist/server/accountPreferencesHandlers.js.map +1 -0
- package/dist/server/adapters/accountPreferencesMigration.d.ts +4 -0
- package/dist/server/adapters/accountPreferencesMigration.d.ts.map +1 -0
- package/dist/server/adapters/accountPreferencesMigration.js +12 -0
- package/dist/server/adapters/accountPreferencesMigration.js.map +1 -0
- package/dist/server/better-auth/adapters/accountPreferenceOptions.d.ts +14 -0
- package/dist/server/better-auth/adapters/accountPreferenceOptions.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/accountPreferenceOptions.js +32 -0
- package/dist/server/better-auth/adapters/accountPreferenceOptions.js.map +1 -0
- package/dist/server/better-auth/adapters/mount.d.ts +45 -41
- package/dist/server/better-auth/adapters/mount.d.ts.map +1 -1
- package/dist/server/better-auth/adapters/mount.js +8 -25
- package/dist/server/better-auth/adapters/mount.js.map +1 -1
- package/dist/server/better-auth/adapters/mountSettings.d.ts +51 -0
- package/dist/server/better-auth/adapters/mountSettings.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/mountSettings.js +26 -0
- package/dist/server/better-auth/adapters/mountSettings.js.map +1 -0
- package/dist/server/better-auth/adapters/preferencesClient.d.ts +3 -0
- package/dist/server/better-auth/adapters/preferencesClient.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/preferencesClient.js +24 -0
- package/dist/server/better-auth/adapters/preferencesClient.js.map +1 -0
- package/package.json +16 -15
- package/src/accountPreferences.ts +42 -0
- package/src/server/AccountClock.ts +14 -0
- package/src/server/AccountPreferencesStore.ts +13 -0
- package/src/server/account-clock/dayRange.ts +12 -0
- package/src/server/account-clock/initialize.ts +20 -0
- package/src/server/account-clock/logicalDay.ts +11 -0
- package/src/server/account-clock/settings.ts +15 -0
- package/src/server/account-preferences/capture.ts +10 -0
- package/src/server/account-preferences/get.ts +13 -0
- package/src/server/account-preferences/initialize.ts +18 -0
- package/src/server/account-preferences/set.ts +12 -0
- package/src/server/accountPreferencesHandlers.ts +22 -0
- package/src/server/adapters/accountPreferencesMigration.ts +12 -0
- package/src/server/better-auth/adapters/accountPreferenceOptions.ts +33 -0
- package/src/server/better-auth/adapters/mount.ts +10 -27
- package/src/server/better-auth/adapters/mountSettings.ts +28 -0
- package/src/server/better-auth/adapters/preferencesClient.ts +25 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineService } from "@shivaedev/service/defineService.ts";
|
|
2
|
+
import { AccountPreferencesStore } from "#server/AccountPreferencesStore.ts";
|
|
3
|
+
import { dayRange } from "#server/account-clock/dayRange.ts";
|
|
4
|
+
import { initialize } from "#server/account-clock/initialize.ts";
|
|
5
|
+
import { logicalDay } from "#server/account-clock/logicalDay.ts";
|
|
6
|
+
import { AccountDayStart } from "#server/account-clock/settings.ts";
|
|
7
|
+
import { Subject } from "#subject.ts";
|
|
8
|
+
|
|
9
|
+
export const AccountClock = defineService({
|
|
10
|
+
id: "@shivaedev/identity/AccountClock",
|
|
11
|
+
initialize,
|
|
12
|
+
methods: (state) => ({ dayRange: dayRange(state), logicalDay: logicalDay(state) }),
|
|
13
|
+
requires: [AccountPreferencesStore, Subject, AccountDayStart],
|
|
14
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SqlClient } from "effect/sql";
|
|
2
|
+
import { defineService } from "@shivaedev/service/defineService.ts";
|
|
3
|
+
import { capture } from "#server/account-preferences/capture.ts";
|
|
4
|
+
import { get } from "#server/account-preferences/get.ts";
|
|
5
|
+
import { initialize } from "#server/account-preferences/initialize.ts";
|
|
6
|
+
import { set } from "#server/account-preferences/set.ts";
|
|
7
|
+
|
|
8
|
+
export const AccountPreferencesStore = defineService({
|
|
9
|
+
id: "@shivaedev/identity/AccountPreferencesStore",
|
|
10
|
+
initialize,
|
|
11
|
+
methods: (state) => ({ capture: capture(state), get: get(state), set: set(state) }),
|
|
12
|
+
requires: [SqlClient.SqlClient],
|
|
13
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DateTime, Effect, Schema } from "effect";
|
|
2
|
+
import type { initialize } from "./initialize.ts";
|
|
3
|
+
import { LogicalDay } from "./settings.ts";
|
|
4
|
+
|
|
5
|
+
export function dayRange({ boundary }: Effect.Success<typeof initialize>) {
|
|
6
|
+
const decodeDay = Schema.decodeUnknownEffect(LogicalDay);
|
|
7
|
+
return Effect.fnUntraced(function* (day: string) {
|
|
8
|
+
yield* decodeDay(day);
|
|
9
|
+
const next = DateTime.makeUnsafe(day).pipe(DateTime.add({ days: 1 }), DateTime.formatIsoDateUtc);
|
|
10
|
+
return { end: boundary(next), start: boundary(day) };
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DateTime, Effect, Schema } from "effect";
|
|
2
|
+
import { AccountPreferencesStore } from "#server/AccountPreferencesStore.ts";
|
|
3
|
+
import { Subject } from "#subject.ts";
|
|
4
|
+
import { AccountDayStart, DayStart } from "./settings.ts";
|
|
5
|
+
|
|
6
|
+
export const initialize = Effect.gen(function* () {
|
|
7
|
+
const { id } = yield* Subject;
|
|
8
|
+
const { timezone } = yield* (yield* AccountPreferencesStore).get(id);
|
|
9
|
+
const minutes = yield* Schema.decodeUnknownEffect(DayStart)(yield* AccountDayStart);
|
|
10
|
+
function boundary(day: string) {
|
|
11
|
+
return DateTime.toUtc(
|
|
12
|
+
DateTime.makeZonedUnsafe(`${day}T00:00:00.000Z`, {
|
|
13
|
+
adjustForTimeZone: true,
|
|
14
|
+
disambiguation: "compatible",
|
|
15
|
+
timeZone: timezone,
|
|
16
|
+
}).pipe(DateTime.setParts({ hour: Math.floor(minutes / 60), minute: minutes % 60 })),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return { boundary, timezone };
|
|
20
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DateTime, Effect } from "effect";
|
|
2
|
+
import type { initialize } from "./initialize.ts";
|
|
3
|
+
|
|
4
|
+
export function logicalDay({ boundary, timezone }: Effect.Success<typeof initialize>) {
|
|
5
|
+
return Effect.fnUntraced(function* (instant?: DateTime.Utc) {
|
|
6
|
+
const now = instant ?? (yield* DateTime.now);
|
|
7
|
+
const local = DateTime.setZoneNamedUnsafe(now, timezone);
|
|
8
|
+
const day = DateTime.formatIsoDate(local);
|
|
9
|
+
return DateTime.isLessThan(now, boundary(day)) ? DateTime.makeUnsafe(day).pipe(DateTime.subtract({ days: 1 }), DateTime.formatIsoDateUtc) : day;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Context, DateTime, Option, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export const LogicalDay = Schema.String.check(
|
|
4
|
+
Schema.isPattern(/^\d{4}-\d{2}-\d{2}$/u),
|
|
5
|
+
Schema.makeFilter(
|
|
6
|
+
(value) => {
|
|
7
|
+
const parsed = DateTime.make(value);
|
|
8
|
+
return Option.isSome(parsed) && DateTime.formatIsoDateUtc(parsed.value) === value;
|
|
9
|
+
},
|
|
10
|
+
{ expected: "an ISO calendar date" },
|
|
11
|
+
),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export const AccountDayStart = Context.Reference<number>("@shivaedev/identity/AccountDayStart", { defaultValue: () => 0 });
|
|
15
|
+
export const DayStart = Schema.Int.check(Schema.isBetween({ maximum: 1439, minimum: 0 }));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { AccountPreferenceValues } from "#accountPreferences.ts";
|
|
3
|
+
import { providerFailure } from "#server/providerFailure.ts";
|
|
4
|
+
import type { PreferencesRepository } from "./initialize.ts";
|
|
5
|
+
|
|
6
|
+
export function capture(repository: PreferencesRepository) {
|
|
7
|
+
return Effect.fnUntraced(function* (id: string, values: typeof AccountPreferenceValues.Type) {
|
|
8
|
+
return yield* repository.insertOrIgnore({ id, ...values }).pipe(Effect.asVoid, Effect.catch(providerFailure));
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { providerFailure } from "#server/providerFailure.ts";
|
|
3
|
+
import type { PreferencesRepository } from "./initialize.ts";
|
|
4
|
+
|
|
5
|
+
export function get(repository: PreferencesRepository) {
|
|
6
|
+
return Effect.fnUntraced(function* (id: string) {
|
|
7
|
+
return yield* repository.findById(id).pipe(
|
|
8
|
+
Effect.map(({ locale, timezone, units }) => ({ locale, timezone, units })),
|
|
9
|
+
Effect.catchTag("NoSuchElementError", () => Effect.succeed({ locale: "en", timezone: "UTC", units: "metric" as const })),
|
|
10
|
+
Effect.catch(providerFailure),
|
|
11
|
+
);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Effect, Schema } from "effect";
|
|
2
|
+
import { Model } from "effect/schema";
|
|
3
|
+
import { makeRepository } from "@shivaedev/sql/repository.ts";
|
|
4
|
+
import { AccountPreferenceValues } from "#accountPreferences.ts";
|
|
5
|
+
|
|
6
|
+
class StoredPreferences extends Model.Class<StoredPreferences>("AccountPreferences")({
|
|
7
|
+
id: Schema.String,
|
|
8
|
+
...AccountPreferenceValues.fields,
|
|
9
|
+
}) {}
|
|
10
|
+
|
|
11
|
+
export const initialize = makeRepository(StoredPreferences, {
|
|
12
|
+
conflictColumns: ["id"],
|
|
13
|
+
idColumn: "id",
|
|
14
|
+
spanPrefix: "AccountPreferences",
|
|
15
|
+
tableName: "identity_account_preferences",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export type PreferencesRepository = Effect.Success<typeof initialize>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { AccountPreferenceValues } from "#accountPreferences.ts";
|
|
3
|
+
import { providerFailure } from "#server/providerFailure.ts";
|
|
4
|
+
import type { PreferencesRepository } from "./initialize.ts";
|
|
5
|
+
|
|
6
|
+
export function set(repository: PreferencesRepository) {
|
|
7
|
+
return Effect.fnUntraced(function* (id: string, values: typeof AccountPreferenceValues.Type) {
|
|
8
|
+
return yield* repository
|
|
9
|
+
.upsert({ id, ...values }, { updateColumns: ["locale", "timezone", "units"] })
|
|
10
|
+
.pipe(Effect.asVoid, Effect.catch(providerFailure));
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { AccountPreferences } from "#accountPreferences.ts";
|
|
3
|
+
import { AccountPreferencesStore } from "#server/AccountPreferencesStore.ts";
|
|
4
|
+
import { Subject } from "#subject.ts";
|
|
5
|
+
|
|
6
|
+
export const accountPreferencesHandlers = AccountPreferences.application.toLayer({
|
|
7
|
+
"identity.accountPreferences.capture": (values) =>
|
|
8
|
+
Effect.gen(function* () {
|
|
9
|
+
const { id } = yield* Subject;
|
|
10
|
+
return yield* (yield* AccountPreferencesStore).capture(id, values);
|
|
11
|
+
}),
|
|
12
|
+
"identity.accountPreferences.get": () =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
const { id } = yield* Subject;
|
|
15
|
+
return yield* (yield* AccountPreferencesStore).get(id);
|
|
16
|
+
}),
|
|
17
|
+
"identity.accountPreferences.set": (values) =>
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const { id } = yield* Subject;
|
|
20
|
+
return yield* (yield* AccountPreferencesStore).set(id, values);
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
|
|
4
|
+
export const accountPreferencesMigration = Effect.gen(function* () {
|
|
5
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
6
|
+
yield* sql`create table identity_account_preferences (
|
|
7
|
+
id text primary key,
|
|
8
|
+
locale text not null,
|
|
9
|
+
timezone text not null,
|
|
10
|
+
units text not null
|
|
11
|
+
)`;
|
|
12
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { BetterAuthOptions, BetterAuthPlugin } from "better-auth";
|
|
2
|
+
import { createAuthMiddleware } from "better-auth/api";
|
|
3
|
+
import { Effect, Option, Schema } from "effect";
|
|
4
|
+
import { AccountPreferenceValues } from "#accountPreferences.ts";
|
|
5
|
+
import { AccountPreferencesStore } from "#server/AccountPreferencesStore.ts";
|
|
6
|
+
|
|
7
|
+
export const accountPreferenceOptions = Effect.fnUntraced(function* <TOptions extends BetterAuthOptions>(options: TOptions) {
|
|
8
|
+
const store = yield* AccountPreferencesStore;
|
|
9
|
+
const services = yield* Effect.context<never>();
|
|
10
|
+
const capture = createAuthMiddleware(async (context) => {
|
|
11
|
+
const session = context.context.newSession ?? context.context.session;
|
|
12
|
+
const values = Schema.decodeUnknownOption(AccountPreferenceValues)({
|
|
13
|
+
locale: context.headers?.get("x-platform-account-locale"),
|
|
14
|
+
timezone: context.headers?.get("x-platform-account-timezone"),
|
|
15
|
+
units: "metric",
|
|
16
|
+
});
|
|
17
|
+
if (session !== null && session !== undefined && Option.isSome(values)) {
|
|
18
|
+
await Effect.runPromiseWith(services)(store.capture(session.user.id, values.value), { signal: context.request?.signal });
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
const plugin = {
|
|
22
|
+
hooks: {
|
|
23
|
+
after: [
|
|
24
|
+
{
|
|
25
|
+
handler: capture,
|
|
26
|
+
matcher: (context) => context.path === "/get-session" || (context.context.newSession !== null && context.context.newSession !== undefined),
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
id: "identity-account-preferences",
|
|
31
|
+
} satisfies BetterAuthPlugin;
|
|
32
|
+
return { ...options, plugins: [...(options.plugins ?? []), plugin] } satisfies BetterAuthOptions;
|
|
33
|
+
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
-
import {
|
|
2
|
+
import { Effect, Layer, type Scope } from "effect";
|
|
3
3
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
4
4
|
import type { Store } from "@shivaedev/rate-limit/Store.ts";
|
|
5
|
+
import { AccountPreferencesStore } from "#server/AccountPreferencesStore.ts";
|
|
5
6
|
import type { AccountRoleSeed } from "#server/better-auth/accountRoleSeed.ts";
|
|
6
7
|
import { type BearerSessionsOptions, withBearerSessions } from "#server/better-auth/adapters/bearerSessions.ts";
|
|
7
|
-
import { PEER_IP_HEADER } from "#server/better-auth/adapters/clientIp.ts";
|
|
8
8
|
import { closeRoleEndpoints } from "#server/better-auth/adapters/closeRoleEndpoints.ts";
|
|
9
9
|
import { effectHookOptions } from "#server/better-auth/adapters/effectHookOptions.ts";
|
|
10
10
|
import { emailOptions } from "#server/better-auth/adapters/emailOptions.ts";
|
|
11
11
|
import { type BetterAuthHardeningOptions, withHardening } from "#server/better-auth/adapters/hardening.ts";
|
|
12
12
|
import { hookRuntime } from "#server/better-auth/adapters/hookRuntime.ts";
|
|
13
13
|
import { mountedSessions } from "#server/better-auth/adapters/mountedSessions.ts";
|
|
14
|
+
import { mountSettings } from "#server/better-auth/adapters/mountSettings.ts";
|
|
14
15
|
import { passwordResetOptions } from "#server/better-auth/adapters/passwordResetOptions.ts";
|
|
15
16
|
import { providerOnlyOptions } from "#server/better-auth/adapters/providerOnlyOptions.ts";
|
|
16
17
|
import { rateLimitStorage } from "#server/better-auth/adapters/rateLimitStorage.ts";
|
|
@@ -26,10 +27,12 @@ import { revokeUnadmitted } from "#server/better-auth/revokeUnadmitted.ts";
|
|
|
26
27
|
import { fromStorage, type PolicyStorage, type SignInPolicy, signInGate } from "#server/better-auth/signInGate.ts";
|
|
27
28
|
import { UndeclaredDefaultRole } from "#server/better-auth/UndeclaredDefaultRole.ts";
|
|
28
29
|
import { providerFailure } from "#server/providerFailure.ts";
|
|
30
|
+
import { accountPreferenceOptions } from "./accountPreferenceOptions.ts";
|
|
29
31
|
import { acquireAuth } from "./acquire.ts";
|
|
30
32
|
import { authRouteHandler } from "./routeHandler.ts";
|
|
31
33
|
|
|
32
34
|
export type BetterAuthMountOptions<E = never, R = never> = {
|
|
35
|
+
readonly accountPreferences?: boolean;
|
|
33
36
|
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
34
37
|
readonly databaseHooks?: BetterAuthDatabaseHooks<R> | undefined;
|
|
35
38
|
readonly emails?: BetterAuthEmails<R> | undefined;
|
|
@@ -50,19 +53,16 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
50
53
|
if (mount.roles !== undefined) {
|
|
51
54
|
yield* mount.roles.resolve(mount.defaultRole).pipe(Effect.mapError(() => new UndeclaredDefaultRole({ role: mount.defaultRole })));
|
|
52
55
|
}
|
|
53
|
-
const
|
|
54
|
-
const publicUrl = yield* Config.URL("PUBLIC_URL").pipe(Config.option);
|
|
55
|
-
const origin = Option.map(publicUrl, (url) => url.origin);
|
|
56
|
-
const trustedProxy = Option.getOrUndefined(yield* Config.String("TRUSTED_PROXY").pipe(Config.option));
|
|
57
|
-
const clientIpHeader = yield* Config.String("AUTH_CLIENT_IP_HEADER").pipe(Config.withDefault("x-forwarded-for"));
|
|
58
|
-
const rateLimitEnabled = yield* Config.Boolean("AUTH_RATE_LIMIT_ENABLED").pipe(Config.withDefault(true));
|
|
56
|
+
const { authOptions, clientIpHeader, rateLimitEnabled, trustedProxy } = yield* mountSettings(options);
|
|
59
57
|
const policy = mount.signIn ?? {};
|
|
60
58
|
if (policy.accounts !== undefined) {
|
|
61
59
|
yield* policy.accounts;
|
|
62
60
|
}
|
|
63
61
|
const gate = yield* signInGate(policy);
|
|
64
62
|
const runtime = yield* hookRuntime<R>();
|
|
65
|
-
const
|
|
63
|
+
const preferences =
|
|
64
|
+
mount.accountPreferences === true ? yield* accountPreferenceOptions(options).pipe(Effect.provide(AccountPreferencesStore.layer)) : options;
|
|
65
|
+
const reset = passwordResetOptions(preferences, mount.onPasswordReset, runtime);
|
|
66
66
|
const hooked = effectHookOptions(yield* emailOptions(reset, mount.emails, runtime), mount.databaseHooks, runtime);
|
|
67
67
|
const configured = mount.roles === undefined ? hooked : storedRoleOptions(closeRoleEndpoints(hooked), mount);
|
|
68
68
|
const gated = yield* signInPolicyOptions(configured, gate, (): Promise<PolicyStorage> => auth.$context);
|
|
@@ -76,29 +76,12 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
76
76
|
const customStorage = yield* rateLimitStorage;
|
|
77
77
|
const { auth, profiles } = yield* acquireAuth({
|
|
78
78
|
...withHardening(withBearerSessions(preset, mount.bearerSessions !== undefined), mount.hardening),
|
|
79
|
-
|
|
80
|
-
useSecureCookies: Option.isSome(publicUrl) && publicUrl.value.protocol === "https:",
|
|
81
|
-
...options.advanced,
|
|
82
|
-
ipAddress: {
|
|
83
|
-
...options.advanced?.ipAddress,
|
|
84
|
-
ipAddressHeaders: [PEER_IP_HEADER],
|
|
85
|
-
trustedProxies: [],
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
"baseURL":
|
|
89
|
-
options.baseURL
|
|
90
|
-
?? Option.getOrElse(origin, () => ({
|
|
91
|
-
allowedHosts: ["127.0.0.1:*", "localhost:*"],
|
|
92
|
-
protocol: "http" as const,
|
|
93
|
-
})),
|
|
79
|
+
...authOptions,
|
|
94
80
|
rateLimit: {
|
|
95
81
|
...rateLimit,
|
|
96
82
|
customStorage,
|
|
97
83
|
enabled: rateLimitEnabled,
|
|
98
84
|
},
|
|
99
|
-
secret,
|
|
100
|
-
trustedOrigins:
|
|
101
|
-
options.trustedOrigins ?? Option.match(origin, { onNone: () => ["http://127.0.0.1:*", "http://localhost:*"], onSome: (url) => [url] }),
|
|
102
85
|
user: gate.active ? gated.user : configured.user,
|
|
103
86
|
});
|
|
104
87
|
const authRoutes = mountRoutes.pipe(
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import { Config, Effect, Option, Redacted } from "effect";
|
|
3
|
+
import { PEER_IP_HEADER } from "#server/better-auth/adapters/clientIp.ts";
|
|
4
|
+
|
|
5
|
+
export const mountSettings = Effect.fnUntraced(function* (options: BetterAuthOptions) {
|
|
6
|
+
const secret = options.secret ?? Redacted.value(yield* Config.Redacted("BETTER_AUTH_SECRET"));
|
|
7
|
+
const publicUrl = yield* Config.URL("PUBLIC_URL").pipe(Config.option);
|
|
8
|
+
const origin = Option.map(publicUrl, (url) => url.origin);
|
|
9
|
+
const trustedProxy = Option.getOrUndefined(yield* Config.String("TRUSTED_PROXY").pipe(Config.option));
|
|
10
|
+
const clientIpHeader = yield* Config.String("AUTH_CLIENT_IP_HEADER").pipe(Config.withDefault("x-forwarded-for"));
|
|
11
|
+
const rateLimitEnabled = yield* Config.Boolean("AUTH_RATE_LIMIT_ENABLED").pipe(Config.withDefault(true));
|
|
12
|
+
return {
|
|
13
|
+
authOptions: {
|
|
14
|
+
advanced: {
|
|
15
|
+
useSecureCookies: Option.isSome(publicUrl) && publicUrl.value.protocol === "https:",
|
|
16
|
+
...options.advanced,
|
|
17
|
+
ipAddress: { ...options.advanced?.ipAddress, ipAddressHeaders: [PEER_IP_HEADER], trustedProxies: [] },
|
|
18
|
+
},
|
|
19
|
+
"baseURL": options.baseURL ?? Option.getOrElse(origin, () => ({ allowedHosts: ["127.0.0.1:*", "localhost:*"], protocol: "http" as const })),
|
|
20
|
+
secret,
|
|
21
|
+
trustedOrigins:
|
|
22
|
+
options.trustedOrigins ?? Option.match(origin, { onNone: () => ["http://127.0.0.1:*", "http://localhost:*"], onSome: (url) => [url] }),
|
|
23
|
+
} satisfies BetterAuthOptions,
|
|
24
|
+
clientIpHeader,
|
|
25
|
+
rateLimitEnabled,
|
|
26
|
+
trustedProxy,
|
|
27
|
+
};
|
|
28
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { BetterAuthClientPlugin } from "better-auth/client";
|
|
2
|
+
|
|
3
|
+
export function preferencesClient(): Pick<BetterAuthClientPlugin, "id" | "fetchPlugins"> {
|
|
4
|
+
return {
|
|
5
|
+
fetchPlugins: [
|
|
6
|
+
{
|
|
7
|
+
hooks: {
|
|
8
|
+
onRequest(context) {
|
|
9
|
+
if (!("window" in globalThis)) {
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
const { locale, timeZone } = new Intl.DateTimeFormat().resolvedOptions();
|
|
13
|
+
const headers = new Headers(context.headers);
|
|
14
|
+
headers.set("x-platform-account-timezone", timeZone);
|
|
15
|
+
headers.set("x-platform-account-locale", locale);
|
|
16
|
+
return { ...context, headers };
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
id: "identity-account-preferences",
|
|
20
|
+
name: "identity-account-preferences",
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
id: "identity-account-preferences",
|
|
24
|
+
};
|
|
25
|
+
}
|