Open History to see the last commit
1import type {
2 AuthClientMetadata,
3 AuthClientSession,
4 AuthSessionId,
5 ServerAuthSessionMethod,
6} from "@t3tools/contracts";
7import { Data, DateTime, Duration, Context } from "effect";
8import type { Effect, Stream } from "effect";
9
10export type SessionRole = "owner" | "client";
11
12export interface IssuedSession {
13 readonly sessionId: AuthSessionId;
14 readonly token: string;
15 readonly method: ServerAuthSessionMethod;
16 readonly client: AuthClientMetadata;
17 readonly expiresAt: DateTime.DateTime;
18 readonly role: SessionRole;
19}
20
21export interface VerifiedSession {
22 readonly sessionId: AuthSessionId;
23 readonly token: string;
24 readonly method: ServerAuthSessionMethod;
25 readonly client: AuthClientMetadata;
26 readonly expiresAt?: DateTime.DateTime;
27 readonly subject: string;
28 readonly role: SessionRole;
29}
30
31export type SessionCredentialChange =
32 | {
33 readonly type: "clientUpserted";
34 readonly clientSession: AuthClientSession;
35 }
36 | {
37 readonly type: "clientRemoved";
38 readonly sessionId: AuthSessionId;
39 };
40
41export class SessionCredentialError extends Data.TaggedError("SessionCredentialError")<{
42 readonly message: string;
43 readonly cause?: unknown;
44}> {}
45
46export interface SessionCredentialServiceShape {
47 readonly cookieName: string;
48 readonly issue: (input?: {
49 readonly ttl?: Duration.Duration;
50 readonly subject?: string;
51 readonly method?: ServerAuthSessionMethod;
52 readonly role?: SessionRole;
53 readonly client?: AuthClientMetadata;
54 }) => Effect.Effect<IssuedSession, SessionCredentialError>;
55 readonly verify: (token: string) => Effect.Effect<VerifiedSession, SessionCredentialError>;
56 readonly issueWebSocketToken: (
57 sessionId: AuthSessionId,
58 input?: {
59 readonly ttl?: Duration.Duration;
60 },
61 ) => Effect.Effect<
62 {
63 readonly token: string;
64 readonly expiresAt: DateTime.DateTime;
65 },
66 SessionCredentialError
67 >;
68 readonly verifyWebSocketToken: (
69 token: string,
70 ) => Effect.Effect<VerifiedSession, SessionCredentialError>;
71 readonly listActive: () => Effect.Effect<
72 ReadonlyArray<AuthClientSession>,
73 SessionCredentialError
74 >;
75 readonly streamChanges: Stream.Stream<SessionCredentialChange>;
76 readonly revoke: (sessionId: AuthSessionId) => Effect.Effect<boolean, SessionCredentialError>;
77 readonly revokeAllExcept: (
78 sessionId: AuthSessionId,
79 ) => Effect.Effect<number, SessionCredentialError>;
80 readonly markConnected: (sessionId: AuthSessionId) => Effect.Effect<void, never>;
81 readonly markDisconnected: (sessionId: AuthSessionId) => Effect.Effect<void, never>;
82}
83
84export class SessionCredentialService extends Context.Service<
85 SessionCredentialService,
86 SessionCredentialServiceShape
87>()("t3/auth/Services/SessionCredentialService") {}