lib/auth/session
Variables
'Read the “', Variables, '” section'sessionCookieName
'Read the “', sessionCookieName, '” section'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.
sessionExpTime
'Read the “', sessionExpTime, '” section'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.
Functions
'Read the “', Functions, '” section'createSession()
'Read the “', createSession(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.
createUserSession()
'Read the “', createUserSession(), '” section'function createUserSession(userId: string, context: APIContext): Promise<void>
Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:174
Creates a new user session.
Parameters
'Read the “', Parameters, '” section'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.
deleteSessionTokenCookie()
'Read the “', deleteSessionTokenCookie(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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
generateSessionToken()
'Read the “', generateSessionToken(), '” section'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.
invalidateSession()
'Read the “', invalidateSession(), '” section'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.
Parameters
'Read the “', Parameters, '” section'sessionId
'Read the “', sessionId, '” section'string
The unique identifier of the session to be invalidated.
Promise
<void
>
A promise that resolves when the session has been successfully deleted.
makeExpirationDate()
'Read the “', makeExpirationDate(), '” section'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.
setOAuthSessionTokenCookie()
'Read the “', setOAuthSessionTokenCookie(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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
setSessionTokenCookie()
'Read the “', setSessionTokenCookie(), '” section'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.
Parameters
'Read the “', Parameters, '” section'APIContext
The API context where the cookie will be set.
string
The session token to be stored in the cookie.
expiresAt
'Read the “', expiresAt, '” section'Date
The expiration date of the cookie.
void
validateSessionToken()
'Read the “', validateSessionToken(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.