lib/auth/session
Esta página aún no está disponible en tu idioma.
Variables
Section titled “Variables”sessionCookieName
Section titled “sessionCookieName”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.
Constant
Section titled “Constant”sessionExpTime
Section titled “sessionExpTime”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
Section titled “Functions”createSession()
Section titled “createSession()”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
Section titled “Parameters”string
The token used to create the session.
userId
Section titled “userId”string
The ID of the user for whom the session is being created.
Returns
Section titled “Returns”Promise
<{
expiresAt
: Date
;
id
: string
;
userId
: string
;
}>
A promise that resolves to the created session object.
createUserSession()
Section titled “createUserSession()”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
Section titled “Parameters”userId
Section titled “userId”string
The ID of the user for whom the session is being created.
context
Section titled “context”APIContext
The API context which includes request and response objects.
Returns
Section titled “Returns”Promise
<void
>
A promise that resolves when the session has been successfully created.
deleteSessionTokenCookie()
Section titled “deleteSessionTokenCookie()”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
Section titled “Parameters”context
Section titled “context”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
>>
Returns
Section titled “Returns”void
generateSessionToken()
Section titled “generateSessionToken()”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.
Returns
Section titled “Returns”string
The generated session token.
invalidateSession()
Section titled “invalidateSession()”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
Section titled “Parameters”sessionId
Section titled “sessionId”string
The unique identifier of the session to be invalidated.
Returns
Section titled “Returns”Promise
<void
>
A promise that resolves when the session has been successfully deleted.
makeExpirationDate()
Section titled “makeExpirationDate()”function makeExpirationDate(): Date
Defined in: studiocms/packages/studiocms/src/lib/auth/session.ts:41
Generates a new expiration date for a session.
Returns
Section titled “Returns”Date
The expiration date calculated by adding the session expiration time to the current date and time.
setOAuthSessionTokenCookie()
Section titled “setOAuthSessionTokenCookie()”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
Section titled “Parameters”context
Section titled “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.
Returns
Section titled “Returns”void
setSessionTokenCookie()
Section titled “setSessionTokenCookie()”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
Section titled “Parameters”context
Section titled “context”APIContext
The API context where the cookie will be set.
string
The session token to be stored in the cookie.
expiresAt
Section titled “expiresAt”Date
The expiration date of the cookie.
Returns
Section titled “Returns”void
validateSessionToken()
Section titled “validateSessionToken()”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
Section titled “Parameters”string
The session token to validate.
Returns
Section titled “Returns”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.