@shivaedev/identity 0.20.0 → 0.21.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.21.0 - 2026-11-03
4
+
5
+ ### Breaking
6
+
7
+ - Move `signedInUser`, `signInAgain`, `passwordUser` and `storedProviderAccounts` from `@shivaedev/identity/server/better-auth/testing.ts` to `@shivaedev/test-kits/identity/adapters/betterAuth.ts`. Move `testUserEmail` and `signedSessionToken` from `@shivaedev/identity/server/better-auth/adapters/testingCredentials.ts` to `@shivaedev/test-kits/identity/adapters/testingCredentials.ts`. Add test-kits as a development dependency.
8
+
3
9
  ## 0.20.0 - 2026-11-03
4
10
 
5
11
  ### Added
package/README.md CHANGED
@@ -747,7 +747,7 @@ Use `authTestDatabase` from `@shivaedev/identity/server/better-auth/testDatabase
747
747
 
748
748
  ```ts
749
749
  import { betterAuthMount } from "@shivaedev/identity/server/better-auth/adapters/mount.ts";
750
- import { signedInUser } from "@shivaedev/identity/server/better-auth/testing.ts";
750
+ import { signedInUser } from "@shivaedev/test-kits/identity/adapters/betterAuth.ts";
751
751
 
752
752
  const mounted = yield* betterAuthMount(options, {
753
753
  defaultRole: "member",
@@ -768,7 +768,7 @@ The Effect returns Better Auth's login result: the user, session, signed request
768
768
 
769
769
  `passwordUser(auth, { email, passwordHash, ...fields })` saves a user and a credential account with a precomputed hash, without hashing or minting a session. Generate the hash once with the application's configured password hasher and keep it as a test fixture alongside its matching password. Then sign in through `/api/auth/sign-in/email` with that password: Better Auth still runs real password verification. A wrong password is refused. Identity's native-session stories use this precondition. The auth instance needs no testing plugin. The existing `passwordUser(auth, { email, password, ...fields })` form hashes with the configured hasher; choose either `passwordHash` or `password`, never both.
770
770
 
771
- Call `yield* signInAgain(auth, userId)` from `server/better-auth/testing.ts` to mint a fresh session for an existing user after a long story clock jump. It returns the same login shape as `signedInUser` and sets expiry from the caller’s Effect Clock and the configured session lifetime. Replace the visitor’s request headers or seed the returned browser cookies.
771
+ Call `yield* signInAgain(auth, userId)` from `@shivaedev/test-kits/identity/adapters/betterAuth.ts` to mint a fresh session for an existing user after a long story clock jump. It returns the same login shape as `signedInUser` and sets expiry from the caller’s Effect Clock and the configured session lifetime. Replace the visitor’s request headers or seed the returned browser cookies.
772
772
 
773
773
  Use `storedProviderAccounts(auth, userId)` to observe credential storage through Better Auth's internal adapter. It returns account and provider IDs with `hasAccessToken`, `hasRefreshToken`, `hasIdToken` and `hasPassword` booleans. It never returns token values, passwords or password hashes. A DOM story kit that exposes its test auth instance and a session readout can use both helpers:
774
774
 
@@ -776,7 +776,7 @@ Use `storedProviderAccounts(auth, userId)` to observe credential storage through
776
776
  import { Effect } from "effect";
777
777
  import { expect } from "vitest";
778
778
  import { seedCookies } from "@shivaedev/test-story/server/adapters/seedCookies.ts";
779
- import { signedInUser, storedProviderAccounts } from "@shivaedev/identity/server/better-auth/testing.ts";
779
+ import { signedInUser, storedProviderAccounts } from "@shivaedev/test-kits/identity/adapters/betterAuth.ts";
780
780
 
781
781
  const member = app.trait("account", "a member starts signed in with GitHub", function* ({ auth }) {
782
782
  const { cookies } = yield* signedInUser(auth, {
@@ -868,7 +868,6 @@ Import the module that defines the name; there is no package-root entry. Paths b
868
868
  | `server/better-auth/sweepExpiredSessions.ts` | `sweepExpiredSessions`: delete expired sessions through the application's SQL client and return the number removed. |
869
869
  | `server/better-auth/userReference.ts` | `userReference`: the default auth user table and id column for application foreign keys. |
870
870
  | `server/better-auth/testDatabase.ts` | `authTestDatabase`: test-only identity/session readouts and faults for the default PostgreSQL auth schema. |
871
- | `server/better-auth/testing.ts` | `signedInUser(auth, fields)`, `passwordUser(auth, fields)`, `storedProviderAccounts(auth, userId)`: test-only users, minted sessions and credential storage observations. |
872
871
 
873
872
  ### Install, setup and limits
874
873
 
@@ -71,7 +71,9 @@ export declare const Sessions: import("@shivaedev/contract/contract.ts").Contrac
71
71
  readonly AuthUnavailable: typeof AuthUnavailable;
72
72
  readonly Forbidden: typeof Forbidden;
73
73
  readonly NotFound: typeof NotFound;
74
- }>>], import("effect/rpc/Rpc").Rpc<"identity.sessions.$changes", Schema.Struct<{
74
+ }>>], import("effect/rpc/Rpc").Rpc<"identity.sessions.revoke", Schema.Struct<{
75
+ id: Schema.String;
76
+ }>, Schema.Void, Schema.Union<readonly (typeof NotFound | typeof Forbidden | typeof AuthUnavailable)[]>, typeof SessionsPermission, never> | import("effect/rpc/Rpc").Rpc<"identity.sessions.$changes", Schema.Struct<{
75
77
  queries: Schema.$Array<Schema.Union<readonly Schema.Struct<{
76
78
  query: Schema.Literal<"list">;
77
79
  payload: Schema.Struct<{
@@ -138,7 +140,5 @@ export declare const Sessions: import("@shivaedev/contract/contract.ts").Contrac
138
140
  "~ClientError": never;
139
141
  error: Schema.Union<readonly [typeof import("@shivaedev/contract/readPolicyErrors.ts").MissingReadPolicy, typeof import("@shivaedev/contract/readPolicyErrors.ts").QueryReadDenied, typeof import("@shivaedev/contract/WebSocketRequired.ts").WebSocketRequired]>;
140
142
  requiredForClient: false;
141
- }), never> | import("effect/rpc/Rpc").Rpc<"identity.sessions.revoke", Schema.Struct<{
142
- id: Schema.String;
143
- }>, Schema.Void, Schema.Union<readonly (typeof NotFound | typeof Forbidden | typeof AuthUnavailable)[]>, typeof SessionsPermission, never>, readonly [], undefined>;
143
+ }), never>, readonly [], undefined>;
144
144
  //# sourceMappingURL=sessions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../src/sessions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIxC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAI5D,eAAO,MAAM,eAAe;;;;;;;;;;EAM1B,CAAC;AAEH,eAAO,MAAM,aAAa;;;EAAqF,CAAC;AAChH,eAAO,MAAM,gBAAgB;;;;;;EAG3B,CAAC;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;EAA2G,CAAC;AAEpI,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mKAiBY,CAAC"}
1
+ {"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../src/sessions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIxC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAI5D,eAAO,MAAM,eAAe;;;;;;;;;;EAM1B,CAAC;AAEH,eAAO,MAAM,aAAa;;;EAAqF,CAAC;AAChH,eAAO,MAAM,gBAAgB;;;;;;EAG3B,CAAC;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;EAA2G,CAAC;AAEpI,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mCAiBY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/identity",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "Request identity, sessions, transport trust and permission enforcement for Effect RPC servers",
5
5
  "keywords": [
6
6
  "effect"
@@ -45,22 +45,23 @@
45
45
  "README.md"
46
46
  ],
47
47
  "dependencies": {
48
- "@shivaedev/access": "0.4.0",
48
+ "@shivaedev/access": "0.4.1",
49
49
  "@shivaedev/changes": "0.9.0",
50
- "@shivaedev/contract": "0.32.0",
51
- "@shivaedev/errors": "0.6.0",
52
- "@shivaedev/rate-limit": "0.3.0",
53
- "@shivaedev/sql": "0.34.0"
50
+ "@shivaedev/contract": "0.33.0",
51
+ "@shivaedev/errors": "0.7.0",
52
+ "@shivaedev/rate-limit": "0.3.1",
53
+ "@shivaedev/sql": "0.35.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@better-auth/oauth-provider": "1.7.14",
57
57
  "@effect/platform-node": "4.0.6",
58
58
  "@effect/sql-pg": "4.0.6",
59
- "@shivaedev/admin": "0.19.0",
60
- "@shivaedev/quality": "0.57.0",
61
- "@shivaedev/server": "0.20.0",
62
- "@shivaedev/test-sql": "0.17.0",
63
- "@shivaedev/test-story": "0.35.0",
59
+ "@shivaedev/admin": "0.19.1",
60
+ "@shivaedev/quality": "0.57.1",
61
+ "@shivaedev/server": "0.20.1",
62
+ "@shivaedev/test-kits": "0.1.0",
63
+ "@shivaedev/test-sql": "0.18.0",
64
+ "@shivaedev/test-story": "0.36.0",
64
65
  "@types/node": "24.19.2",
65
66
  "better-auth": "1.7.14",
66
67
  "effect": "4.0.6",
@@ -1,4 +0,0 @@
1
- import { Effect } from "effect";
2
- export declare const testUserEmail: Effect.Effect<string, never, never>;
3
- export declare const signedSessionToken: (token: string, secret: string) => Effect.Effect<string, never, never>;
4
- //# sourceMappingURL=testingCredentials.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testingCredentials.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/testingCredentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,aAAa,qCAAmE,CAAC;AAE9F,eAAO,MAAM,kBAAkB,wEAG7B,CAAC"}
@@ -1,8 +0,0 @@
1
- import { generateRandomString, makeSignature } from "better-auth/crypto";
2
- import { Effect } from "effect";
3
- export const testUserEmail = Effect.sync(() => `test-${generateRandomString(8)}@example.com`);
4
- export const signedSessionToken = Effect.fnUntraced(function* (token, secret) {
5
- const signature = yield* Effect.promise(() => makeSignature(token, secret));
6
- return `${token}.${signature}`;
7
- });
8
- //# sourceMappingURL=testingCredentials.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testingCredentials.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/testingCredentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,oBAAoB,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;AAE9F,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,KAAa,EAAE,MAAc;IAC3F,MAAM,SAAS,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IAC5E,OAAO,GAAG,KAAK,IAAI,SAAS,EAAE,CAAC;AAChC,CAAC,CAAC,CAAC"}
@@ -1,64 +0,0 @@
1
- import type { AuthContext } from "better-auth";
2
- import { Effect } from "effect";
3
- type AccountContext = Pick<AuthContext, "internalAdapter">;
4
- type SessionContext = AccountContext & Pick<AuthContext, "sessionConfig" | "secret" | "authCookies" | "baseURL">;
5
- export declare const signedInUser: (auth: {
6
- readonly $context: Promise<SessionContext>;
7
- }, args_1: Partial<{
8
- id: string;
9
- createdAt: Date;
10
- updatedAt: Date;
11
- email: string;
12
- emailVerified: boolean;
13
- name: string;
14
- image?: string | null | undefined;
15
- }> & Record<string, unknown> & {
16
- readonly providerAccounts?: readonly {
17
- readonly providerId: string;
18
- readonly accountId: string;
19
- }[];
20
- }) => Effect.Effect<import("better-auth/plugins").LoginResult, never, never>;
21
- export declare const signInAgain: (auth: {
22
- readonly $context: Promise<SessionContext>;
23
- }, userId: string) => Effect.Effect<import("better-auth/plugins").LoginResult, never, never>;
24
- export declare function storedProviderAccounts(auth: {
25
- readonly $context: Promise<AccountContext>;
26
- }, userId: string): Promise<{
27
- accountId: string;
28
- hasAccessToken: boolean;
29
- hasIdToken: boolean;
30
- hasPassword: boolean;
31
- hasRefreshToken: boolean;
32
- providerId: string;
33
- }[]>;
34
- export declare const passwordUser: (auth: {
35
- readonly $context: Promise<AccountContext & {
36
- readonly password: Pick<AuthContext["password"], "hash">;
37
- }>;
38
- }, args_1: Partial<{
39
- id: string;
40
- createdAt: Date;
41
- updatedAt: Date;
42
- email: string;
43
- emailVerified: boolean;
44
- name: string;
45
- image?: string | null | undefined;
46
- }> & Record<string, unknown> & ({
47
- readonly email: string;
48
- } & ({
49
- readonly password: string;
50
- readonly passwordHash?: never;
51
- } | {
52
- readonly passwordHash: string;
53
- readonly password?: never;
54
- }))) => Effect.Effect<Record<string, any> & {
55
- id: string;
56
- createdAt: Date;
57
- updatedAt: Date;
58
- email: string;
59
- emailVerified: boolean;
60
- name: string;
61
- image?: string | null | undefined;
62
- }, never, never>;
63
- export {};
64
- //# sourceMappingURL=testing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/testing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAS,MAAM,EAAE,MAAM,QAAQ,CAAC;AAMvC,KAAK,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;AAC3D,KAAK,cAAc,GAAG,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,eAAe,GAAG,QAAQ,GAAG,aAAa,GAAG,SAAS,CAAC,CAAC;AAqBjH,eAAO,MAAM,YAAY;uBACG,OAAO,CAAC,cAAc,CAAC;;;;;;;;;;gCAKrB,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE;4EASlG,CAAC;AAEH,eAAO,MAAM,WAAW;uBACI,OAAO,CAAC,cAAc,CAAC;4FA2BjD,CAAC;AAEH,wBAAsB,sBAAsB,CAAC,IAAI,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CAAE,EAAE,MAAM,EAAE,MAAM;;;;;;;KAWhH;AAED,eAAO,MAAM,YAAY;uBACG,OAAO,CAAC,cAAc,GAAG;QAAE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;;;;;;;;;;oBAK/E,MAAM;;uBACf,MAAM;4BAA0B,KAAK;;2BACjC,MAAM;wBAAsB,KAAK;;;;;;;;;gBAU7D,CAAC"}
@@ -1,69 +0,0 @@
1
- import { Clock, Effect } from "effect";
2
- import { signedSessionToken, testUserEmail } from "#server/better-auth/adapters/testingCredentials.ts";
3
- function saveUser(context, fields) {
4
- return Effect.flatMap(testUserEmail, (email) => Effect.promise(() => context.internalAdapter.createUser({ email, emailVerified: true, image: null, name: "Test User", ...fields }, { method: "test" })));
5
- }
6
- function cookieSameSite(value) {
7
- switch (value?.toLowerCase()) {
8
- case "strict":
9
- return "Strict";
10
- case "none":
11
- return "None";
12
- default:
13
- return "Lax";
14
- }
15
- }
16
- export const signedInUser = Effect.fnUntraced(function* (auth, { providerAccounts = [], ...fields }) {
17
- const context = yield* Effect.promise(() => auth.$context);
18
- const user = yield* saveUser(context, fields);
19
- for (const { providerId, accountId } of providerAccounts) {
20
- yield* Effect.promise(() => context.internalAdapter.createAccount({ accountId, providerId, userId: user.id }));
21
- }
22
- return yield* signInAgain(auth, user.id);
23
- });
24
- export const signInAgain = Effect.fnUntraced(function* (auth, userId) {
25
- const { internalAdapter, sessionConfig, authCookies, secret, baseURL } = yield* Effect.promise(() => auth.$context);
26
- const now = yield* Clock.currentTimeMillis;
27
- const session = yield* Effect.promise(() => internalAdapter.createSession(userId, false, {}, true));
28
- const expiresAt = new Date(now + sessionConfig.expiresIn * 1000);
29
- yield* Effect.promise(() => internalAdapter.updateSession(session.token, { expiresAt }));
30
- const user = yield* Effect.promise(() => internalAdapter.findUserById(userId));
31
- if (user === null) {
32
- return yield* Effect.die(`User not found: ${userId}`);
33
- }
34
- const value = yield* signedSessionToken(session.token, secret);
35
- const { name, attributes } = authCookies.sessionToken;
36
- const cookies = [
37
- {
38
- domain: URL.canParse(baseURL) ? new URL(baseURL).hostname : "localhost",
39
- httpOnly: attributes.httpOnly ?? true,
40
- name,
41
- path: attributes.path || "/",
42
- sameSite: cookieSameSite(attributes.sameSite),
43
- secure: attributes.secure ?? false,
44
- value,
45
- ...(attributes.maxAge ? { expires: Math.floor(now / 1000) + attributes.maxAge } : {}),
46
- },
47
- ];
48
- return { cookies, headers: new Headers({ cookie: `${name}=${value}` }), session: { ...session, expiresAt }, token: session.token, user };
49
- });
50
- export async function storedProviderAccounts(auth, userId) {
51
- const { internalAdapter } = await auth.$context;
52
- const accounts = await internalAdapter.findAccounts(userId);
53
- return accounts.map(({ providerId, accountId, accessToken, refreshToken, idToken, password }) => ({
54
- accountId,
55
- hasAccessToken: Boolean(accessToken),
56
- hasIdToken: Boolean(idToken),
57
- hasPassword: Boolean(password),
58
- hasRefreshToken: Boolean(refreshToken),
59
- providerId,
60
- }));
61
- }
62
- export const passwordUser = Effect.fnUntraced(function* (auth, { password, passwordHash, ...fields }) {
63
- const context = yield* Effect.promise(() => auth.$context);
64
- const user = yield* saveUser(context, fields);
65
- const hash = passwordHash ?? (yield* Effect.promise(() => context.password.hash(password)));
66
- yield* Effect.promise(() => context.internalAdapter.createAccount({ accountId: user.id, password: hash, providerId: "credential", userId: user.id }));
67
- return user;
68
- });
69
- //#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CAAC,EAAE,EAAE,CAAC,CACxH,CAAC;IACF,OAAO,IAAI,CAAC;AACb,CAAC,CAAC,CAAC"}
@@ -1,9 +0,0 @@
1
- import { generateRandomString, makeSignature } from "better-auth/crypto";
2
- import { Effect } from "effect";
3
-
4
- export const testUserEmail = Effect.sync(() => `test-${generateRandomString(8)}@example.com`);
5
-
6
- export const signedSessionToken = Effect.fnUntraced(function* (token: string, secret: string) {
7
- const signature = yield* Effect.promise(() => makeSignature(token, secret));
8
- return `${token}.${signature}`;
9
- });
@@ -1,109 +0,0 @@
1
- import type { AuthContext } from "better-auth";
2
- import type { testUtils } from "better-auth/plugins";
3
- import { Clock, Effect } from "effect";
4
- import { signedSessionToken, testUserEmail } from "#server/better-auth/adapters/testingCredentials.ts";
5
-
6
- type TestContext = ReturnType<ReturnType<typeof testUtils>["init"]>["context"];
7
- type UserFields = NonNullable<Parameters<TestContext["test"]["createUser"]>[0]>;
8
- type Login = Awaited<ReturnType<TestContext["test"]["login"]>>;
9
- type AccountContext = Pick<AuthContext, "internalAdapter">;
10
- type SessionContext = AccountContext & Pick<AuthContext, "sessionConfig" | "secret" | "authCookies" | "baseURL">;
11
-
12
- function saveUser(context: AccountContext, fields: UserFields) {
13
- return Effect.flatMap(testUserEmail, (email) =>
14
- Effect.promise(() =>
15
- context.internalAdapter.createUser({ email, emailVerified: true, image: null, name: "Test User", ...fields }, { method: "test" }),
16
- ),
17
- );
18
- }
19
-
20
- function cookieSameSite(value: string | undefined) {
21
- switch (value?.toLowerCase()) {
22
- case "strict":
23
- return "Strict";
24
- case "none":
25
- return "None";
26
- default:
27
- return "Lax";
28
- }
29
- }
30
-
31
- export const signedInUser = Effect.fnUntraced(function* (
32
- auth: { readonly $context: Promise<SessionContext> },
33
- {
34
- providerAccounts = [],
35
- ...fields
36
- }: UserFields & {
37
- readonly providerAccounts?: readonly { readonly providerId: string; readonly accountId: string }[];
38
- },
39
- ): Effect.fn.Return<Login> {
40
- const context = yield* Effect.promise(() => auth.$context);
41
- const user = yield* saveUser(context, fields);
42
- for (const { providerId, accountId } of providerAccounts) {
43
- yield* Effect.promise(() => context.internalAdapter.createAccount({ accountId, providerId, userId: user.id }));
44
- }
45
- return yield* signInAgain(auth, user.id);
46
- });
47
-
48
- export const signInAgain = Effect.fnUntraced(function* (
49
- auth: { readonly $context: Promise<SessionContext> },
50
- userId: string,
51
- ): Effect.fn.Return<Login> {
52
- const { internalAdapter, sessionConfig, authCookies, secret, baseURL } = yield* Effect.promise(() => auth.$context);
53
- const now = yield* Clock.currentTimeMillis;
54
- const session = yield* Effect.promise(() => internalAdapter.createSession(userId, false, {}, true));
55
- const expiresAt = new Date(now + sessionConfig.expiresIn * 1000);
56
- yield* Effect.promise(() => internalAdapter.updateSession(session.token, { expiresAt }));
57
- const user = yield* Effect.promise(() => internalAdapter.findUserById(userId));
58
- if (user === null) {
59
- return yield* Effect.die(`User not found: ${userId}`);
60
- }
61
- const value = yield* signedSessionToken(session.token, secret);
62
- const { name, attributes } = authCookies.sessionToken;
63
- const cookies: Login["cookies"] = [
64
- {
65
- domain: URL.canParse(baseURL) ? new URL(baseURL).hostname : "localhost",
66
- httpOnly: attributes.httpOnly ?? true,
67
- name,
68
- path: attributes.path || "/",
69
- sameSite: cookieSameSite(attributes.sameSite),
70
- secure: attributes.secure ?? false,
71
- value,
72
- ...(attributes.maxAge ? { expires: Math.floor(now / 1000) + attributes.maxAge } : {}),
73
- },
74
- ];
75
- return { cookies, headers: new Headers({ cookie: `${name}=${value}` }), session: { ...session, expiresAt }, token: session.token, user };
76
- });
77
-
78
- export async function storedProviderAccounts(auth: { readonly $context: Promise<AccountContext> }, userId: string) {
79
- const { internalAdapter } = await auth.$context;
80
- const accounts = await internalAdapter.findAccounts(userId);
81
- return accounts.map(({ providerId, accountId, accessToken, refreshToken, idToken, password }) => ({
82
- accountId,
83
- hasAccessToken: Boolean(accessToken),
84
- hasIdToken: Boolean(idToken),
85
- hasPassword: Boolean(password),
86
- hasRefreshToken: Boolean(refreshToken),
87
- providerId,
88
- }));
89
- }
90
-
91
- export const passwordUser = Effect.fnUntraced(function* (
92
- auth: { readonly $context: Promise<AccountContext & { readonly password: Pick<AuthContext["password"], "hash"> }> },
93
- {
94
- password,
95
- passwordHash,
96
- ...fields
97
- }: UserFields & { readonly email: string } & (
98
- | { readonly password: string; readonly passwordHash?: never }
99
- | { readonly passwordHash: string; readonly password?: never }
100
- ),
101
- ) {
102
- const context = yield* Effect.promise(() => auth.$context);
103
- const user = yield* saveUser(context, fields);
104
- const hash = passwordHash ?? (yield* Effect.promise(() => context.password.hash(password)));
105
- yield* Effect.promise(() =>
106
- context.internalAdapter.createAccount({ accountId: user.id, password: hash, providerId: "credential", userId: user.id }),
107
- );
108
- return user;
109
- });