lib/auth/user
Variables
'Read the “', Variables, '” section'LinkNewOAuthCookieName
'Read the “', LinkNewOAuthCookieName, '” section'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.
permissionRanksMap
'Read the “', permissionRanksMap, '” section'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’.
Functions
'Read the “', Functions, '” section'createLocalUser()
'Read the “', createLocalUser(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.
createOAuthUser()
'Read the “', createOAuthUser(), '” section'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.
Parameters
'Read the “', Parameters, '” section'userFields
'Read the “', userFields, '” section'The fields required to create a new user.
null
| string
createdAt?
'Read the “', createdAt?, '” section'null
| Date
null
| string
string
string
password?
'Read the “', password?, '” section'null
| string
updatedAt?
'Read the “', updatedAt?, '” section'null
| Date
null
| string
string
oAuthFields
'Read the “', oAuthFields, '” section'The OAuth provider information, including the provider name and provider user ID.
string
providerUserId
'Read the “', providerUserId, '” section'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.
createUserAvatar()
'Read the “', createUserAvatar(), '” section'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.
Parameters
'Read the “', Parameters, '” section'string
The email address of the user.
Promise
<string
>
A promise that resolves to the URL of the user’s avatar.
getUserData()
'Read the “', getUserData(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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:
- Extracts the session token from cookies.
- If no session token is found, returns an object indicating the user is not logged in.
- Validates the session token.
- If the session is invalid, deletes the session token cookie and returns an object indicating the user is not logged in.
- If the user is not found, returns an object indicating the user is not logged in.
- Retrieves the user’s permission level from the database.
- Returns an object containing the user’s login status, user information, and permission level.
getUserFromEmail()
'Read the “', getUserFromEmail(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.
getUserPasswordHash()
'Read the “', getUserPasswordHash(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.
updateUserPassword()
'Read the “', updateUserPassword(), '” section'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.
Parameters
'Read the “', Parameters, '” section'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.
verifyUsernameInput()
'Read the “', verifyUsernameInput(), '” section'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.
Parameters
'Read the “', Parameters, '” section'string
The username to verify.
boolean
true
if the username is valid, false
otherwise.
verifyUserPermissionLevel()
'Read the “', verifyUserPermissionLevel(), '” section'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.
Parameters
'Read the “', Parameters, '” section'The session data of the user, which includes their permission level.
requiredPermission
'Read the “', requiredPermission, '” section'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.