UNPKG

@withstudiocms/auth-kit

Version:

Utilities for managing authentication

35 lines (34 loc) 3.5 kB
import { Effect } from '@withstudiocms/effect'; import type { APIContext, AstroGlobal } from 'astro'; import { SessionError } from '../errors.js'; import type { SessionConfig, SessionValidationResult } from '../types.js'; /** * Creates a session management module with the provided configuration. * * This factory function returns an object containing various session-related * effectful operations, such as generating session tokens, creating and validating * sessions, managing session cookies, and handling OAuth session tokens. * * @param config - The session configuration object. This is merged with the default session configuration. * @returns An Effect that yields an object with session management methods: * - `generateSessionToken`: Generates a secure random session token. * - `createSession`: Creates a new session for a user. * - `validateSessionToken`: Validates a session token, extends expiration if needed, and deletes expired sessions. * - `invalidateSession`: Deletes a session by its ID. * - `setSessionTokenCookie`: Sets a session token cookie in the provided context. * - `deleteSessionTokenCookie`: Deletes the session token cookie in the provided context. * - `setOAuthSessionTokenCookie`: Sets an OAuth session token cookie in the provided context. * - `createUserSession`: Creates a new user session and sets the session token cookie. * * @throws {SessionError} If required session tools are not provided in the configuration. */ export declare const Session: (config: SessionConfig) => Effect.Effect<{ readonly generateSessionToken: () => Effect.Effect.AsEffect<Effect.Effect<string, SessionError, never>>; readonly createSession: (token: string, userId: string) => Effect.Effect<import("../types.js").UserSession, SessionError, never>; readonly validateSessionToken: (token: string) => Effect.Effect<SessionValidationResult, SessionError, never>; readonly invalidateSession: (sessionId: string) => Effect.Effect.AsEffect<Effect.Effect<void, SessionError, never>>; readonly setSessionTokenCookie: (context: APIContext<Record<string, any>, Record<string, string | undefined>> | AstroGlobal<Record<string, any>, import("astro/runtime/server/index.js").AstroComponentFactory, Record<string, string | undefined>>, token: string, expiresAt: Date, secure?: boolean | undefined) => Effect.Effect.AsEffect<Effect.Effect<void, SessionError, never>>; readonly deleteSessionTokenCookie: (context: APIContext<Record<string, any>, Record<string, string | undefined>> | AstroGlobal<Record<string, any>, import("astro/runtime/server/index.js").AstroComponentFactory, Record<string, string | undefined>>, secure?: boolean | undefined) => Effect.Effect.AsEffect<Effect.Effect<void, SessionError, never>>; readonly setOAuthSessionTokenCookie: (context: APIContext<Record<string, any>, Record<string, string | undefined>> | AstroGlobal<Record<string, any>, import("astro/runtime/server/index.js").AstroComponentFactory, Record<string, string | undefined>>, key: string, value: string, secure?: boolean | undefined) => Effect.Effect.AsEffect<Effect.Effect<void, SessionError, never>>; readonly createUserSession: (userId: string, context: APIContext<Record<string, any>, Record<string, string | undefined>> | AstroGlobal<Record<string, any>, import("astro/runtime/server/index.js").AstroComponentFactory, Record<string, string | undefined>>, secure?: boolean | undefined) => Effect.Effect<void, SessionError, never>; }, SessionError, never>;