Skip to content

lib/auth/session

const sessionCookieName: "auth_session" = 'auth_session';

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:50

The name of the cookie used to store the authentication session.


const sessionExpTime: number;

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:28

The session expiration time in milliseconds. This value represents 14 days.

function createSession(token: string, userId: string): Promise<{
expiresAt: Date;
id: string;
userId: string;
}>

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:59

Creates a new session for a user.

string

The token used to create the session.

string

The ID of the user for whom the session is being created.

Promise<{ expiresAt: Date; id: string; userId: string; }>

A promise that resolves to the created session object.


function createUserSession(userId: string, context: APIContext): Promise<void>

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:174

Creates a new user session.

string

The ID of the user for whom the session is being created.

APIContext

The API context which includes request and response objects.

Promise<void>

A promise that resolves when the session has been successfully created.


function deleteSessionTokenCookie(context:
| APIContext<Record<string, any>, Record<string, undefined | string>>
| AstroGlobal<Record<string, any>, AstroComponentFactory, Record<string, undefined | string>>): void

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:140

Deletes the session token cookie by setting it with an empty value and a max age of 0.

The context in which the cookie is being set. This can be either an APIContext or AstroGlobal.

APIContext<Record<string, any>, Record<string, undefined | string>> | AstroGlobal<Record<string, any>, AstroComponentFactory, Record<string, undefined | string>>

void


function generateSessionToken(): string

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:17

Generates a session token.

This function creates a random 20-byte array and encodes it using base32 encoding without padding. The resulting string is used as a session token.

string

The generated session token.


function invalidateSession(sessionId: string): Promise<void>

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:114

Invalidates a session by deleting it from the database.

string

The unique identifier of the session to be invalidated.

Promise<void>

A promise that resolves when the session has been successfully deleted.


function makeExpirationDate(): Date

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:41

Generates a new expiration date for a session.

Date

The expiration date calculated by adding the session expiration time to the current date and time.


function setOAuthSessionTokenCookie(
context: APIContext,
key: string,
value: string): void

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:157

Sets an OAuth session token cookie in the given API context.

APIContext

The API context which contains the cookies object.

string

The name of the cookie to set.

string

The value of the cookie to set.

void


function setSessionTokenCookie(
context: APIContext,
token: string,
expiresAt: Date): void

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:125

Sets a session token cookie in the provided API context.

APIContext

The API context where the cookie will be set.

string

The session token to be stored in the cookie.

Date

The expiration date of the cookie.

void


function validateSessionToken(token: string): Promise<SessionValidationResult>

Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:78

Validates a session token by checking its existence and expiration in the database. If the session is valid but close to expiration, it extends the session expiration time. If the session is expired, it deletes the session from the database.

string

The session token to validate.

Promise<SessionValidationResult>

A promise that resolves to an object containing the session and user information. If the session is invalid or expired, both session and user will be null.