Open History to see the last commit
1import { AuthClientMetadataDeviceType, AuthSessionId } from "@t3tools/contracts";
2import { Option, Schema, Context } from "effect";
3import type { Effect } from "effect";
4
5import type { AuthSessionRepositoryError } from "../Errors.ts";
6
7export const AuthSessionClientMetadataRecord = Schema.Struct({
8 label: Schema.NullOr(Schema.String),
9 ipAddress: Schema.NullOr(Schema.String),
10 userAgent: Schema.NullOr(Schema.String),
11 deviceType: AuthClientMetadataDeviceType,
12 os: Schema.NullOr(Schema.String),
13 browser: Schema.NullOr(Schema.String),
14});
15export type AuthSessionClientMetadataRecord = typeof AuthSessionClientMetadataRecord.Type;
16
17export const AuthSessionRecord = Schema.Struct({
18 sessionId: AuthSessionId,
19 subject: Schema.String,
20 role: Schema.Literals(["owner", "client"]),
21 method: Schema.Literals(["browser-session-cookie", "bearer-session-token"]),
22 client: AuthSessionClientMetadataRecord,
23 issuedAt: Schema.DateTimeUtcFromString,
24 expiresAt: Schema.DateTimeUtcFromString,
25 lastConnectedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
26 revokedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
27});
28export type AuthSessionRecord = typeof AuthSessionRecord.Type;
29
30export const CreateAuthSessionInput = Schema.Struct({
31 sessionId: AuthSessionId,
32 subject: Schema.String,
33 role: Schema.Literals(["owner", "client"]),
34 method: Schema.Literals(["browser-session-cookie", "bearer-session-token"]),
35 client: AuthSessionClientMetadataRecord,
36 issuedAt: Schema.DateTimeUtcFromString,
37 expiresAt: Schema.DateTimeUtcFromString,
38});
39export type CreateAuthSessionInput = typeof CreateAuthSessionInput.Type;
40
41export const GetAuthSessionByIdInput = Schema.Struct({
42 sessionId: AuthSessionId,
43});
44export type GetAuthSessionByIdInput = typeof GetAuthSessionByIdInput.Type;
45
46export const ListActiveAuthSessionsInput = Schema.Struct({
47 now: Schema.DateTimeUtcFromString,
48});
49export type ListActiveAuthSessionsInput = typeof ListActiveAuthSessionsInput.Type;
50
51export const RevokeAuthSessionInput = Schema.Struct({
52 sessionId: AuthSessionId,
53 revokedAt: Schema.DateTimeUtcFromString,
54});
55export type RevokeAuthSessionInput = typeof RevokeAuthSessionInput.Type;
56
57export const RevokeOtherAuthSessionsInput = Schema.Struct({
58 currentSessionId: AuthSessionId,
59 revokedAt: Schema.DateTimeUtcFromString,
60});
61export type RevokeOtherAuthSessionsInput = typeof RevokeOtherAuthSessionsInput.Type;
62
63export const SetAuthSessionLastConnectedAtInput = Schema.Struct({
64 sessionId: AuthSessionId,
65 lastConnectedAt: Schema.DateTimeUtcFromString,
66});
67export type SetAuthSessionLastConnectedAtInput = typeof SetAuthSessionLastConnectedAtInput.Type;
68
69export interface AuthSessionRepositoryShape {
70 readonly create: (
71 input: CreateAuthSessionInput,
72 ) => Effect.Effect<void, AuthSessionRepositoryError>;
73 readonly getById: (
74 input: GetAuthSessionByIdInput,
75 ) => Effect.Effect<Option.Option<AuthSessionRecord>, AuthSessionRepositoryError>;
76 readonly listActive: (
77 input: ListActiveAuthSessionsInput,
78 ) => Effect.Effect<ReadonlyArray<AuthSessionRecord>, AuthSessionRepositoryError>;
79 readonly revoke: (
80 input: RevokeAuthSessionInput,
81 ) => Effect.Effect<boolean, AuthSessionRepositoryError>;
82 readonly revokeAllExcept: (
83 input: RevokeOtherAuthSessionsInput,
84 ) => Effect.Effect<ReadonlyArray<AuthSessionId>, AuthSessionRepositoryError>;
85 readonly setLastConnectedAt: (
86 input: SetAuthSessionLastConnectedAtInput,
87 ) => Effect.Effect<void, AuthSessionRepositoryError>;
88}
89
90export class AuthSessionRepository extends Context.Service<
91 AuthSessionRepository,
92 AuthSessionRepositoryShape
93>()("t3/persistence/Services/AuthSessions/AuthSessionRepository") {}