Session.ts overview
Sessions over WorkOS: a stateless sealed cookie. It holds the refresh token, the claims of the last access token (verified against the environment’s key set when it arrived), the user and the sign-in’s attributes, sealed with AES-256-GCM. A request whose access token is still fresh costs no API call; an expired one is refreshed once (grant_type=refresh_token), and the new cookie goes back through validateSession’s reissued.
Since v0.1.0
Exports Grouped by Category
constants
SessionAttribute
The attribute keys this package writes on a Session, under workos..
Signature
declare const SessionAttribute: {
readonly organizationId: "workos.organizationId"
readonly role: "workos.role"
readonly roles: "workos.roles"
readonly permissions: "workos.permissions"
readonly authenticationMethod: "workos.authenticationMethod"
readonly impersonator: "workos.impersonator"
}
Since v0.1.0
layers
layer
SessionService, WorkOSSessions and WorkOSAuthenticate over the WorkOSClient. Reads RevokedSessions when it is provided.
Signature
declare const layer: Layer.Layer<
WorkOSSessions | WorkOSAuthenticate | Auth.SessionService.SessionService,
never,
WorkOSClient | Auth.Hooks.Hooks | HttpClient.HttpClient
>
Since v0.1.0
models
WorkOSIssued (interface)
A session just issued from a WorkOS authentication, with its user.
Signature
export interface WorkOSIssued extends Auth.Types.IssuedSession {
readonly user: Auth.Types.User
}
Since v0.1.0
schemas
AccessTokenClaims
The verified claims of a WorkOS user access token this package reads.
Signature
declare const AccessTokenClaims: Schema.Struct<{
readonly sub: Schema.String
readonly sid: Schema.String
readonly iss: Schema.String
readonly exp: Schema.Finite
readonly iat: Schema.Finite
readonly auth_time: Schema.optional<Schema.Finite>
readonly org_id: Schema.optional<Schema.String>
readonly role: Schema.optional<Schema.String>
readonly roles: Schema.optional<Schema.$Array<Schema.String>>
readonly permissions: Schema.optional<Schema.$Array<Schema.String>>
readonly sub_profile: Schema.optional<Schema.String>
readonly sub_type: Schema.optional<Schema.String>
}>
Since v0.1.0
services
RevokedSessions (class)
Sessions WorkOS has revoked, so a cookie whose access token is still fresh stops working before it expires. The session.revoked webhook and sign-out write it; validateSession reads it. Without it a revoked session lasts until its access token expires (minutes).
Signature
declare class RevokedSessions
Since v0.1.0
WorkOSSessions (class)
The sealed-cookie session machinery the flows and the SessionService share.
Signature
declare class WorkOSSessions
Since v0.1.0
utils
AccessTokenClaims (type alias)
Signature
type AccessTokenClaims = typeof AccessTokenClaims.Type
Since v0.1.0
WorkOSAuthenticate (class)
The pieces make builds, for the flows of this package.
Signature
declare class WorkOSAuthenticate
make
Signature
declare const make: () => Effect.Effect<
{
sessions: {
readonly issue: (
auth: AuthenticationResponse,
attributes?: Auth.Types.Attributes
) => Effect.Effect<WorkOSIssued, Auth.Errors.AuthError | Auth.Hooks.HookError>
readonly switchOrganization: (
token: string,
organizationId: string
) => Effect.Effect<
WorkOSIssued,
| Auth.Errors.SessionNotFoundError
| Auth.Errors.SessionExpiredError
| Auth.Errors.AuthError
| Auth.Hooks.HookError
>
readonly sessionIdOf: (token: string) => Effect.Effect<Option.Option<string>>
readonly logoutUrl: (options: { readonly sessionId: string; readonly returnTo?: string }) => string
}
service: {
readonly createSession: (
userId: string,
attributes?: Auth.Types.Attributes
) => Effect.Effect<Auth.Types.IssuedSession, Auth.Errors.AuthError | Auth.Hooks.HookError>
readonly validateSession: (
token: string
) => Effect.Effect<
Auth.Types.ValidatedSession,
| Auth.Errors.SessionNotFoundError
| Auth.Errors.SessionExpiredError
| Auth.Errors.UserNotFoundError
| Auth.Errors.UserBannedError
| Auth.Hooks.HookError
>
readonly invalidateSession: (token: string) => Effect.Effect<void, Auth.Errors.AuthError | Auth.Hooks.HookError>
readonly invalidateUserSessions: (userId: string) => Effect.Effect<void, Auth.Errors.AuthError>
readonly listSessions: (userId: string) => Effect.Effect<ReadonlyArray<Auth.Types.Session>, Auth.Errors.AuthError>
readonly revokeSessionById: (
userId: string,
storedSessionId: string
) => Effect.Effect<boolean, Auth.Errors.AuthError>
readonly revokeOtherSessions: (
userId: string,
keepSessionId: string
) => Effect.Effect<void, Auth.Errors.AuthError>
readonly setSessionAttributes: (
storedSessionId: string,
attributes: Auth.Types.Attributes
) => Effect.Effect<Auth.Types.Session, Auth.Errors.SessionNotFoundError | Auth.Errors.AuthError>
}
authenticate: (
body: Readonly<Record<string, string>>
) => Effect.Effect<
{
readonly user: {
readonly id: string
readonly created_at: string
readonly email: string
readonly email_verified: boolean
readonly updated_at: string
readonly first_name?: string | null | undefined
readonly last_name?: string | null | undefined
readonly name?: string | null | undefined
readonly profile_picture_url?: string | null | undefined
readonly last_sign_in_at?: string | null | undefined
readonly external_id?: string | null | undefined
readonly metadata?: { readonly [x: string]: string } | null | undefined
}
readonly access_token: string
readonly refresh_token: string
readonly organization_id?: string | null | undefined
readonly authentication_method?: string | null | undefined
readonly impersonator?:
{ readonly email: string; readonly reason?: string | null | undefined } | null | undefined
},
WorkOSError,
never
>
revokeAtWorkOS: (sessionId: string) => Effect.Effect<void, Auth.Errors.AuthError, never>
},
never,
WorkOSClient | Auth.Hooks.Hooks | HttpClient.HttpClient | Scope
>