@krak-stack/auth 0.1.38 → 0.1.39

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.
@@ -0,0 +1,40 @@
1
+ import { Cause, Effect, Layer, Redacted } from "effect";
2
+ import { HttpServerRequest } from "effect/unstable/http";
3
+ import { HttpApiError, HttpApiMiddleware, HttpApiSecurity, } from "effect/unstable/httpapi";
4
+ import { AuthService } from "../service";
5
+ export const ApiKeySecurity = HttpApiSecurity.apiKey({
6
+ key: "x-api-key",
7
+ in: "header",
8
+ });
9
+ export class AuthMiddleware extends HttpApiMiddleware.Service()("@krak-stack/auth/AuthMiddleware", {
10
+ error: HttpApiError.Unauthorized,
11
+ security: {
12
+ apiKey: ApiKeySecurity,
13
+ },
14
+ }) {
15
+ }
16
+ const isAuthServiceLayer = (value) => Layer.isLayer(value);
17
+ export const makeAuthenticationLive = (options = {}) => {
18
+ const legacyLayer = isAuthServiceLayer(options) ? options : undefined;
19
+ const authOptions = isAuthServiceLayer(options) ? {} : options;
20
+ const authLayer = legacyLayer ?? authOptions.authLayer;
21
+ return Layer.effect(AuthMiddleware, Effect.gen(function* () {
22
+ return {
23
+ apiKey: (httpEffect, { credential }) => Effect.gen(function* () {
24
+ const request = yield* HttpServerRequest.HttpServerRequest;
25
+ const apiKey = Redacted.value(credential).trim();
26
+ const headers = apiKey
27
+ ? { ...request.headers, "x-api-key": apiKey }
28
+ : request.headers;
29
+ const auth = yield* AuthService.pipe(Effect.provide(authLayer ??
30
+ AuthService.layer({
31
+ ...authOptions,
32
+ headers,
33
+ })), Effect.tapCause((cause) => Effect.logError("Authentication rejected: auth client initialization failed", Cause.pretty(cause))), Effect.mapError(() => new HttpApiError.Unauthorized({})));
34
+ return yield* httpEffect.pipe(Effect.provideService(AuthService, auth));
35
+ }).pipe(Effect.mapError((error) => error instanceof HttpApiError.Unauthorized
36
+ ? error
37
+ : new HttpApiError.Unauthorized({}))),
38
+ };
39
+ }));
40
+ };
@@ -31,6 +31,7 @@ export declare const ServerUsersResponse: Schema.Struct<{
31
31
  readonly image: Schema.NullOr<Schema.String>;
32
32
  readonly role: Schema.NullOr<Schema.String>;
33
33
  readonly banned: Schema.NullOr<Schema.Boolean>;
34
+ readonly lastLoginMethod: Schema.optional<Schema.NullOr<Schema.String>>;
34
35
  readonly createdAt: Schema.Date;
35
36
  readonly updatedAt: Schema.Date;
36
37
  }>>;
@@ -70,6 +71,7 @@ export declare const ServerMembersResponse: Schema.$Array<Schema.Struct<{
70
71
  readonly image: Schema.NullOr<Schema.String>;
71
72
  readonly role: Schema.NullOr<Schema.String>;
72
73
  readonly banned: Schema.NullOr<Schema.Boolean>;
74
+ readonly lastLoginMethod: Schema.optional<Schema.NullOr<Schema.String>>;
73
75
  readonly createdAt: Schema.Date;
74
76
  readonly updatedAt: Schema.Date;
75
77
  }>;
package/dist/service.d.ts CHANGED
@@ -1,13 +1,45 @@
1
1
  import { Context, Effect, Layer } from "effect";
2
2
  import { HttpClient } from "effect/unstable/http";
3
3
  import { HttpApiClient } from "effect/unstable/httpapi";
4
+ import { HttpApiError } from "effect/unstable/httpapi";
4
5
  import { AuthClientConfig, type ClientConfig } from "./config";
6
+ import { ExtraVerifiedApiKey } from "./extra/schema";
7
+ import type { Session, User } from "./schema";
8
+ type ExtraVerifiedApiKeyType = typeof ExtraVerifiedApiKey.Type;
9
+ export type AuthSession = {
10
+ readonly session: Session;
11
+ readonly user: User | undefined;
12
+ readonly isSuperAdminImpersonation: false;
13
+ readonly authMethod: {
14
+ readonly type: "cookie";
15
+ } | {
16
+ readonly type: "apiKey";
17
+ readonly apiKey: ExtraVerifiedApiKeyType;
18
+ };
19
+ };
20
+ export type AuthSessionWithOrganization = AuthSession & {
21
+ readonly session: Session & {
22
+ readonly activeOrganizationId: string;
23
+ };
24
+ };
25
+ export type AuthSessionWithUser = AuthSession & {
26
+ readonly user: User;
27
+ };
28
+ export type AuthSessionWithUserAndOrganization = AuthSessionWithUser & AuthSessionWithOrganization;
5
29
  export type AuthServiceLayerOptions = Partial<ClientConfig> & {
6
30
  readonly headers?: Record<string, string>;
31
+ readonly apiKeyConfigId?: "user" | "organization";
7
32
  };
8
33
  declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/auth/AuthService", {
34
+ getSession: () => Effect.Effect<AuthSession | null, HttpApiError.Unauthorized, never>;
35
+ requireSession: () => Effect.Effect<AuthSession, HttpApiError.Unauthorized>;
36
+ requireUser: () => Effect.Effect<AuthSessionWithUser, HttpApiError.Unauthorized>;
37
+ requireOrganization: () => Effect.Effect<AuthSessionWithOrganization, HttpApiError.Unauthorized>;
38
+ requireUserOrganization: () => Effect.Effect<AuthSessionWithUserAndOrganization, HttpApiError.Unauthorized>;
9
39
  auth: {
10
- getSession: () => Effect.Effect<{
40
+ readonly getSession: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: void | {
41
+ readonly responseMode?: Mode;
42
+ }) => Effect.Effect<HttpApiClient.Client.Response<{
11
43
  readonly session: {
12
44
  readonly id: string;
13
45
  readonly expiresAt: Date;
@@ -28,10 +60,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
28
60
  readonly image: string | null;
29
61
  readonly role: string | null;
30
62
  readonly banned: boolean | null;
63
+ readonly lastLoginMethod?: string | null | undefined;
31
64
  readonly createdAt: Date;
32
65
  readonly updatedAt: Date;
33
66
  };
34
- } | null, unknown, never>;
67
+ } | null, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.Forbidden | HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | import("./better-auth").TooManyRequests | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
35
68
  };
36
69
  authExtra: {
37
70
  readonly createApiKey: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -53,6 +86,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
53
86
  readonly projectId?: string | undefined;
54
87
  readonly clientId?: string | undefined;
55
88
  readonly host?: string | undefined;
89
+ readonly rootHost?: string | undefined;
56
90
  };
57
91
  readonly responseMode?: Mode;
58
92
  }) => Effect.Effect<HttpApiClient.Client.Response<{
@@ -69,7 +103,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
69
103
  readonly signUp: boolean;
70
104
  readonly signUpName: boolean;
71
105
  };
72
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError), [Mode] extends ["response-only"] ? never : never>;
106
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError), [Mode] extends ["response-only"] ? never : never>;
73
107
  readonly setPassword: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
74
108
  readonly payload: {
75
109
  readonly newPassword: string;
@@ -83,13 +117,13 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
83
117
  readonly responseMode?: Mode;
84
118
  }) => Effect.Effect<HttpApiClient.Client.Response<{
85
119
  readonly url: string;
86
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").Forbidden | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
120
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.Forbidden | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
87
121
  readonly uploadUserImage: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
88
122
  readonly payload: FormData;
89
123
  readonly responseMode?: Mode;
90
124
  }) => Effect.Effect<HttpApiClient.Client.Response<{
91
125
  readonly url: string;
92
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("./extra").ExtraBadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
126
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("./extra").ExtraBadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
93
127
  readonly verifyApiKey: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
94
128
  readonly payload: {
95
129
  readonly key: string;
@@ -152,7 +186,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
152
186
  readonly active: boolean;
153
187
  readonly createdAt: Date;
154
188
  readonly updatedAt: Date;
155
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
189
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
156
190
  readonly deleteDomain: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
157
191
  readonly params: {
158
192
  readonly id: string;
@@ -169,7 +203,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
169
203
  readonly active: boolean;
170
204
  readonly createdAt: Date;
171
205
  readonly updatedAt: Date;
172
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
206
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
173
207
  readonly getDomain: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
174
208
  readonly params: {
175
209
  readonly id: string;
@@ -186,7 +220,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
186
220
  readonly active: boolean;
187
221
  readonly createdAt: Date;
188
222
  readonly updatedAt: Date;
189
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
223
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
190
224
  readonly getDomainByHost: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
191
225
  readonly params: {
192
226
  readonly hostname: string;
@@ -203,7 +237,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
203
237
  readonly active: boolean;
204
238
  readonly createdAt: Date;
205
239
  readonly updatedAt: Date;
206
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
240
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
207
241
  readonly getDomainRecords: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
208
242
  readonly params: {
209
243
  readonly id: string;
@@ -215,7 +249,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
215
249
  readonly type: string;
216
250
  readonly name: string;
217
251
  readonly value: string;
218
- }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
252
+ }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
219
253
  };
220
254
  organizations: {
221
255
  readonly getActiveMember: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -238,10 +272,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
238
272
  readonly image: string | null;
239
273
  readonly role: string | null;
240
274
  readonly banned: boolean | null;
275
+ readonly lastLoginMethod?: string | null | undefined;
241
276
  readonly createdAt: Date;
242
277
  readonly updatedAt: Date;
243
278
  };
244
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
279
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
245
280
  readonly getOrganization: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
246
281
  readonly params: {
247
282
  readonly id: string;
@@ -263,7 +298,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
263
298
  }[];
264
299
  } | null;
265
300
  readonly createdAt: Date;
266
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
301
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
267
302
  readonly listOrganizationMembers: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
268
303
  readonly params: {
269
304
  readonly organizationId: string;
@@ -283,10 +318,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
283
318
  readonly image: string | null;
284
319
  readonly role: string | null;
285
320
  readonly banned: boolean | null;
321
+ readonly lastLoginMethod?: string | null | undefined;
286
322
  readonly createdAt: Date;
287
323
  readonly updatedAt: Date;
288
324
  };
289
- }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
325
+ }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
290
326
  readonly listOrganizations: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
291
327
  readonly query: {
292
328
  readonly ids?: string | undefined;
@@ -312,7 +348,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
312
348
  readonly createdAt: Date;
313
349
  }[];
314
350
  readonly missingIds: readonly string[];
315
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
351
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
316
352
  };
317
353
  users: {
318
354
  readonly getUser: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -328,9 +364,10 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
328
364
  readonly image: string | null;
329
365
  readonly role: string | null;
330
366
  readonly banned: boolean | null;
367
+ readonly lastLoginMethod?: string | null | undefined;
331
368
  readonly createdAt: Date;
332
369
  readonly updatedAt: Date;
333
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
370
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
334
371
  readonly getUserActiveOrganization: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
335
372
  readonly params: {
336
373
  readonly userId: string;
@@ -338,7 +375,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
338
375
  readonly responseMode?: Mode;
339
376
  }) => Effect.Effect<HttpApiClient.Client.Response<{
340
377
  readonly id: string | null;
341
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
378
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
342
379
  readonly listUsersByIds: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
343
380
  readonly query: {
344
381
  readonly ids: string;
@@ -353,16 +390,24 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
353
390
  readonly image: string | null;
354
391
  readonly role: string | null;
355
392
  readonly banned: boolean | null;
393
+ readonly lastLoginMethod?: string | null | undefined;
356
394
  readonly createdAt: Date;
357
395
  readonly updatedAt: Date;
358
396
  }[];
359
397
  readonly missingIds: readonly string[];
360
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
398
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
361
399
  };
362
400
  }> & {
363
- readonly make: (headers?: Record<string, string>) => Effect.Effect<{
401
+ readonly make: (options?: AuthServiceLayerOptions) => Effect.Effect<{
402
+ getSession: () => Effect.Effect<AuthSession | null, HttpApiError.Unauthorized, never>;
403
+ requireSession: () => Effect.Effect<AuthSession, HttpApiError.Unauthorized>;
404
+ requireUser: () => Effect.Effect<AuthSessionWithUser, HttpApiError.Unauthorized>;
405
+ requireOrganization: () => Effect.Effect<AuthSessionWithOrganization, HttpApiError.Unauthorized>;
406
+ requireUserOrganization: () => Effect.Effect<AuthSessionWithUserAndOrganization, HttpApiError.Unauthorized>;
364
407
  auth: {
365
- getSession: () => Effect.Effect<{
408
+ readonly getSession: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: void | {
409
+ readonly responseMode?: Mode;
410
+ }) => Effect.Effect<HttpApiClient.Client.Response<{
366
411
  readonly session: {
367
412
  readonly id: string;
368
413
  readonly expiresAt: Date;
@@ -383,10 +428,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
383
428
  readonly image: string | null;
384
429
  readonly role: string | null;
385
430
  readonly banned: boolean | null;
431
+ readonly lastLoginMethod?: string | null | undefined;
386
432
  readonly createdAt: Date;
387
433
  readonly updatedAt: Date;
388
434
  };
389
- } | null, unknown, never>;
435
+ } | null, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.Forbidden | HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | import("./better-auth").TooManyRequests | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
390
436
  };
391
437
  authExtra: {
392
438
  readonly createApiKey: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -408,6 +454,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
408
454
  readonly projectId?: string | undefined;
409
455
  readonly clientId?: string | undefined;
410
456
  readonly host?: string | undefined;
457
+ readonly rootHost?: string | undefined;
411
458
  };
412
459
  readonly responseMode?: Mode;
413
460
  }) => Effect.Effect<HttpApiClient.Client.Response<{
@@ -424,7 +471,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
424
471
  readonly signUp: boolean;
425
472
  readonly signUpName: boolean;
426
473
  };
427
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError), [Mode] extends ["response-only"] ? never : never>;
474
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError), [Mode] extends ["response-only"] ? never : never>;
428
475
  readonly setPassword: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
429
476
  readonly payload: {
430
477
  readonly newPassword: string;
@@ -438,13 +485,13 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
438
485
  readonly responseMode?: Mode;
439
486
  }) => Effect.Effect<HttpApiClient.Client.Response<{
440
487
  readonly url: string;
441
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").Forbidden | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
488
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.Forbidden | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
442
489
  readonly uploadUserImage: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
443
490
  readonly payload: FormData;
444
491
  readonly responseMode?: Mode;
445
492
  }) => Effect.Effect<HttpApiClient.Client.Response<{
446
493
  readonly url: string;
447
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("./extra").ExtraBadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
494
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("./extra").ExtraBadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
448
495
  readonly verifyApiKey: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
449
496
  readonly payload: {
450
497
  readonly key: string;
@@ -507,7 +554,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
507
554
  readonly active: boolean;
508
555
  readonly createdAt: Date;
509
556
  readonly updatedAt: Date;
510
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
557
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
511
558
  readonly deleteDomain: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
512
559
  readonly params: {
513
560
  readonly id: string;
@@ -524,7 +571,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
524
571
  readonly active: boolean;
525
572
  readonly createdAt: Date;
526
573
  readonly updatedAt: Date;
527
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
574
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
528
575
  readonly getDomain: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
529
576
  readonly params: {
530
577
  readonly id: string;
@@ -541,7 +588,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
541
588
  readonly active: boolean;
542
589
  readonly createdAt: Date;
543
590
  readonly updatedAt: Date;
544
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
591
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
545
592
  readonly getDomainByHost: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
546
593
  readonly params: {
547
594
  readonly hostname: string;
@@ -558,7 +605,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
558
605
  readonly active: boolean;
559
606
  readonly createdAt: Date;
560
607
  readonly updatedAt: Date;
561
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
608
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
562
609
  readonly getDomainRecords: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
563
610
  readonly params: {
564
611
  readonly id: string;
@@ -570,7 +617,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
570
617
  readonly type: string;
571
618
  readonly name: string;
572
619
  readonly value: string;
573
- }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
620
+ }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
574
621
  };
575
622
  organizations: {
576
623
  readonly getActiveMember: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -593,10 +640,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
593
640
  readonly image: string | null;
594
641
  readonly role: string | null;
595
642
  readonly banned: boolean | null;
643
+ readonly lastLoginMethod?: string | null | undefined;
596
644
  readonly createdAt: Date;
597
645
  readonly updatedAt: Date;
598
646
  };
599
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
647
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
600
648
  readonly getOrganization: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
601
649
  readonly params: {
602
650
  readonly id: string;
@@ -618,7 +666,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
618
666
  }[];
619
667
  } | null;
620
668
  readonly createdAt: Date;
621
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
669
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
622
670
  readonly listOrganizationMembers: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
623
671
  readonly params: {
624
672
  readonly organizationId: string;
@@ -638,10 +686,11 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
638
686
  readonly image: string | null;
639
687
  readonly role: string | null;
640
688
  readonly banned: boolean | null;
689
+ readonly lastLoginMethod?: string | null | undefined;
641
690
  readonly createdAt: Date;
642
691
  readonly updatedAt: Date;
643
692
  };
644
- }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
693
+ }[], Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
645
694
  readonly listOrganizations: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
646
695
  readonly query: {
647
696
  readonly ids?: string | undefined;
@@ -667,7 +716,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
667
716
  readonly createdAt: Date;
668
717
  }[];
669
718
  readonly missingIds: readonly string[];
670
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").BadRequest | import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
719
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.BadRequest | HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
671
720
  };
672
721
  users: {
673
722
  readonly getUser: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
@@ -683,9 +732,10 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
683
732
  readonly image: string | null;
684
733
  readonly role: string | null;
685
734
  readonly banned: boolean | null;
735
+ readonly lastLoginMethod?: string | null | undefined;
686
736
  readonly createdAt: Date;
687
737
  readonly updatedAt: Date;
688
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/unstable/httpapi/HttpApiError").NotFound | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
738
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | HttpApiError.NotFound | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
689
739
  readonly getUserActiveOrganization: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
690
740
  readonly params: {
691
741
  readonly userId: string;
@@ -693,7 +743,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
693
743
  readonly responseMode?: Mode;
694
744
  }) => Effect.Effect<HttpApiClient.Client.Response<{
695
745
  readonly id: string | null;
696
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
746
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
697
747
  readonly listUsersByIds: <Mode extends HttpApiClient.Client.ResponseMode = import("effect/unstable/httpapi/HttpApiEndpoint").ClientResponseMode>(request: {
698
748
  readonly query: {
699
749
  readonly ids: string;
@@ -708,11 +758,12 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
708
758
  readonly image: string | null;
709
759
  readonly role: string | null;
710
760
  readonly banned: boolean | null;
761
+ readonly lastLoginMethod?: string | null | undefined;
711
762
  readonly createdAt: Date;
712
763
  readonly updatedAt: Date;
713
764
  }[];
714
765
  readonly missingIds: readonly string[];
715
- }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : import("effect/unstable/httpapi/HttpApiError").InternalServerError | import("effect/Schema").SchemaError | import("effect/unstable/httpapi/HttpApiError").Unauthorized), [Mode] extends ["response-only"] ? never : never>;
766
+ }, Mode>, import("effect/unstable/http/HttpClientError").HttpClientError | ([Mode] extends ["response-only"] ? never : HttpApiError.InternalServerError | import("effect/Schema").SchemaError | HttpApiError.Unauthorized), [Mode] extends ["response-only"] ? never : never>;
716
767
  };
717
768
  }, never, AuthClientConfig | HttpClient.HttpClient>;
718
769
  };
package/dist/service.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { Cache, Context, Duration, Effect, Exit, Layer, Redacted, } from "effect";
2
2
  import { FetchHttpClient, HttpClient, HttpClientRequest, } from "effect/unstable/http";
3
3
  import { HttpApiClient } from "effect/unstable/httpapi";
4
+ import { HttpApiError } from "effect/unstable/httpapi";
4
5
  import { AuthApi } from "./api";
5
6
  import { AuthClientConfig } from "./config";
7
+ import { ExtraVerifiedApiKey } from "./extra/schema";
6
8
  const forwardedAuthHeaderNames = [
7
9
  "accept-language",
8
10
  "cookie",
@@ -28,10 +30,21 @@ const authHttpClientLayer = (headers) => Layer.effect(HttpClient.HttpClient, Eff
28
30
  return HttpClient.mapRequest(http, (request) => HttpClientRequest.setHeaders(request, forwardedAuthHeaders(headers)));
29
31
  })).pipe(Layer.provide(FetchHttpClient.layer));
30
32
  const getSessionCacheKey = (headers) => JSON.stringify(forwardedAuthHeaders(headers));
31
- const clientConfigOptions = ({ baseUrl, apiKey }) => ({
32
- ...(baseUrl ? { baseUrl } : {}),
33
- ...(apiKey ? { apiKey } : {}),
34
- });
33
+ const headerValue = (headers, name) => {
34
+ const lowerName = name.toLowerCase();
35
+ const entry = Object.entries(headers).find(([key]) => key.toLowerCase() === lowerName);
36
+ return entry?.[1];
37
+ };
38
+ const unauthorized = () => new HttpApiError.Unauthorized({});
39
+ const withRequiredOrganization = (authSession) => {
40
+ const activeOrganizationId = authSession.session.activeOrganizationId;
41
+ return activeOrganizationId
42
+ ? Effect.succeed({
43
+ ...authSession,
44
+ session: { ...authSession.session, activeOrganizationId },
45
+ })
46
+ : Effect.fail(unauthorized());
47
+ };
35
48
  const isBetterAuthSessionRequest = (request) => request.url.endsWith("/api/auth/get-session");
36
49
  class SessionLookup extends Context.Service()("@krak-stack/auth/SessionLookup") {
37
50
  }
@@ -44,7 +57,9 @@ const sessionCache = Effect.runSync(Cache.makeWith(() => Effect.gen(function* ()
44
57
  requireServicesAt: "lookup",
45
58
  }));
46
59
  export class AuthService extends Context.Service()("@krak-stack/auth/AuthService", {
47
- make: (headers = {}) => Effect.gen(function* () {
60
+ make: (options = {}) => Effect.gen(function* () {
61
+ const headers = options.headers ?? {};
62
+ const apiKeyConfigId = options.apiKeyConfigId ?? "user";
48
63
  const config = yield* AuthClientConfig;
49
64
  const http = yield* HttpClient.HttpClient;
50
65
  const httpClient = HttpClient.mapRequest(http, (request) => isBetterAuthSessionRequest(request)
@@ -55,19 +70,88 @@ export class AuthService extends Context.Service()("@krak-stack/auth/AuthService
55
70
  httpClient,
56
71
  });
57
72
  const sessionCacheKey = getSessionCacheKey(headers);
73
+ const apiKey = headerValue(headers, "x-api-key")?.trim();
74
+ const getCookieSession = () => Cache.get(sessionCache, sessionCacheKey).pipe(Effect.provideService(SessionLookup, {
75
+ getSession: api.auth.getSession,
76
+ }), Effect.mapError(unauthorized), Effect.map((authSession) => authSession
77
+ ? {
78
+ ...authSession,
79
+ isSuperAdminImpersonation: false,
80
+ authMethod: { type: "cookie" },
81
+ }
82
+ : null));
83
+ const userForApiKey = (verifiedKey) => {
84
+ if (verifiedKey.configId === "organization") {
85
+ return Effect.succeed(undefined);
86
+ }
87
+ return api.users
88
+ .getUser({ params: { id: verifiedKey.referenceId } })
89
+ .pipe(Effect.mapError(unauthorized));
90
+ };
91
+ const activeOrganizationIdForApiKey = (verifiedKey, user) => {
92
+ if (verifiedKey.configId === "organization") {
93
+ return Effect.succeed({ id: verifiedKey.referenceId });
94
+ }
95
+ if (!user)
96
+ return Effect.succeed({ id: null });
97
+ return api.users
98
+ .getUserActiveOrganization({ params: { userId: user.id } })
99
+ .pipe(Effect.catch(() => Effect.succeed({ id: null })));
100
+ };
101
+ const getApiKeySession = (key) => Effect.gen(function* () {
102
+ const verified = yield* api.authExtra
103
+ .verifyApiKey({
104
+ payload: { key, configId: apiKeyConfigId },
105
+ })
106
+ .pipe(Effect.mapError(unauthorized));
107
+ if (!verified.valid || !verified.key) {
108
+ return yield* Effect.fail(unauthorized());
109
+ }
110
+ const user = yield* userForApiKey(verified.key);
111
+ const activeOrganization = yield* activeOrganizationIdForApiKey(verified.key, user);
112
+ const now = new Date();
113
+ return {
114
+ session: {
115
+ id: `api-key:${verified.key.id}`,
116
+ token: verified.key.id,
117
+ userId: user?.id ?? `api-key:${verified.key.id}`,
118
+ activeOrganizationId: activeOrganization.id,
119
+ createdAt: now,
120
+ updatedAt: now,
121
+ expiresAt: verified.key.expiresAt ?? new Date(now.getTime() + 3600000),
122
+ },
123
+ user,
124
+ isSuperAdminImpersonation: false,
125
+ authMethod: {
126
+ type: "apiKey",
127
+ apiKey: verified.key,
128
+ },
129
+ };
130
+ });
131
+ const getSession = () => apiKey ? getApiKeySession(apiKey) : getCookieSession();
132
+ const requireSession = () => getSession().pipe(Effect.flatMap((authSession) => authSession
133
+ ? Effect.succeed(authSession)
134
+ : Effect.fail(unauthorized())));
135
+ const requireUser = () => requireSession().pipe(Effect.flatMap((authSession) => authSession.user
136
+ ? Effect.succeed({ ...authSession, user: authSession.user })
137
+ : Effect.fail(unauthorized())));
138
+ const requireOrganization = () => requireSession().pipe(Effect.flatMap(withRequiredOrganization));
139
+ const requireUserOrganization = () => requireUser().pipe(Effect.flatMap(withRequiredOrganization));
58
140
  return {
59
141
  ...api,
60
- auth: {
61
- ...api.auth,
62
- getSession: () => Cache.get(sessionCache, sessionCacheKey).pipe(Effect.provideService(SessionLookup, {
63
- getSession: api.auth.getSession,
64
- })),
65
- },
142
+ getSession,
143
+ requireSession,
144
+ requireUser,
145
+ requireOrganization,
146
+ requireUserOrganization,
66
147
  };
67
148
  }),
68
149
  }) {
69
150
  static layer = (options = {}) => {
70
151
  const headers = options.headers ?? {};
71
- return Layer.effect(this, this.make(headers)).pipe(Layer.provide(AuthClientConfig.layer(clientConfigOptions(options))), Layer.provide(authHttpClientLayer(headers)));
152
+ return Layer.effect(this, this.make({ ...options, headers })).pipe(Layer.provide(AuthClientConfig.layer({
153
+ ...(options.baseUrl ? { baseUrl: options.baseUrl } : {}),
154
+ ...(options.apiKey ? { apiKey: options.apiKey } : {}),
155
+ })), Layer.provide(authHttpClientLayer(headers)));
72
156
  };
73
157
  }