Skip to content

lib/auth/user

const LinkNewOAuthCookieName: "link-new-o-auth" = 'link-new-o-auth';

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:127

The name of the cookie used for linking a new OAuth account. This constant is used to identify the specific cookie that handles the linking process for new OAuth accounts.


const permissionRanksMap: Record<AvailablePermissionRanks, string[]>;

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:278

A mapping of permission ranks to their respective allowed roles.

This map defines the hierarchy of permissions, where each rank includes all the roles of the ranks below it. For example, an ‘admin’ has the roles of both ‘owner’ and ‘admin’, while an ‘editor’ has the roles of ‘owner’, ‘admin’, and ‘editor’.

function createLocalUser(
name: string,
username: string,
email: string,
password: string): Promise<{
avatar: null | string;
createdAt: null | Date;
email: null | string;
id: string;
name: string;
password: null | string;
updatedAt: null | Date;
url: null | string;
username: string;
}>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:72

Creates a new local user with the provided details.

string

The full name of the user.

string

The username for the user.

string

The email address of the user.

string

The password for the user.

Promise<{ avatar: null | string; createdAt: null | Date; email: null | string; id: string; name: string; password: null | string; updatedAt: null | Date; url: null | string; username: string; }>

A promise that resolves to the newly created user record.


function createOAuthUser(userFields: {
avatar: null | string;
createdAt: null | Date;
email: null | string;
id: string;
name: string;
password: null | string;
updatedAt: null | Date;
url: null | string;
username: string;
}, oAuthFields: {
provider: string;
providerUserId: string;
}): Promise<
| {
avatar: null | string;
createdAt: null | Date;
email: null | string;
id: string;
name: string;
password: null | string;
updatedAt: null | Date;
url: null | string;
username: string;
}
| {
error: string;
}>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:102

Creates a new user with OAuth credentials.

The fields required to create a new user.

null | string

null | Date

null | string

string

string

null | string

null | Date

null | string

string

The OAuth provider information, including the provider name and provider user ID.

string

string

Promise< | { avatar: null | string; createdAt: null | Date; email: null | string; id: string; name: string; password: null | string; updatedAt: null | Date; url: null | string; username: string; } | { error: string; }>

The newly created user object or an error object if the creation fails.


function createUserAvatar(email: string): Promise<string>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:48

Creates a user avatar URL based on the provided email.

This function takes an email address, processes it to generate a unique hash, and returns a URL for the user’s avatar using the Libravatar service.

string

The email address of the user.

Promise<string>

A promise that resolves to the URL of the user’s avatar.


function getUserData(Astro:
| APIContext<Record<string, any>, Record<string, undefined | string>>
| AstroGlobal<Record<string, any>, AstroComponentFactory, Record<string, undefined | string>>): Promise<UserSessionData>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:191

Retrieves user session data based on the provided Astro context.

The Astro global object or API context containing cookies.

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

Promise<UserSessionData>

A promise that resolves to the user session data.

The function performs the following steps:

  1. Extracts the session token from cookies.
  2. If no session token is found, returns an object indicating the user is not logged in.
  3. Validates the session token.
  4. If the session is invalid, deletes the session token cookie and returns an object indicating the user is not logged in.
  5. If the user is not found, returns an object indicating the user is not logged in.
  6. Retrieves the user’s permission level from the database.
  7. Returns an object containing the user’s login status, user information, and permission level.

function getUserFromEmail(email: string): Promise<
| null
| {
avatar: null | string;
createdAt: null | Date;
email: null | string;
id: string;
name: string;
password: null | string;
updatedAt: null | Date;
url: null | string;
username: string;
}>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:172

Retrieves a user from the database based on their email address.

string

The email address of the user to retrieve.

Promise< | null | { avatar: null | string; createdAt: null | Date; email: null | string; id: string; name: string; password: null | string; updatedAt: null | Date; url: null | string; username: string; }>

A promise that resolves to the user data if found, or null if no user is found with the given email.


function getUserPasswordHash(userId: string): Promise<string>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:152

Retrieves the password hash for a given user by their user ID.

string

The unique identifier of the user whose password hash is to be retrieved.

Promise<string>

A promise that resolves to the password hash of the user.

Will throw an error if the user is not found or if the user does not have a password.


function updateUserPassword(userId: string, password: string): Promise<void>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:139

Updates the password for a user.

This function hashes the provided password and updates the user’s password in the database with the hashed password.

string

The unique identifier of the user whose password is to be updated.

string

The new password to be set for the user.

Promise<void>

A promise that resolves when the password has been successfully updated.


function verifyUsernameInput(username: string): boolean

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:20

Verifies if the provided username meets the required criteria.

The username must:

  • Be between 3 and 32 characters in length.
  • Contain only lowercase letters, numbers, hyphens (-), and underscores (_).
  • Not be considered unsafe.

string

The username to verify.

boolean

true if the username is valid, false otherwise.


function verifyUserPermissionLevel(userData: UserSessionData, requiredPermission: "unknown" | "visitor" | "editor" | "admin" | "owner"): Promise<boolean>

Defined in: studiocms/packages/studiocms/src/lib/auth/user.ts:293

Verifies if the user’s permission level meets the required permission rank.

UserSessionData

The session data of the user, which includes their permission level.

The required permission rank to be verified against the user’s permission level.

"unknown" | "visitor" | "editor" | "admin" | "owner"

Promise<boolean>

A promise that resolves to a boolean indicating whether the user’s permission level meets the required rank.