UNPKG

@withstudiocms/auth-kit

Version:

Utilities for managing authentication

86 lines (85 loc) 4.73 kB
import { Effect } from '@withstudiocms/effect'; import type { APIContext, AstroGlobal } from 'astro'; import { UserError } from '../errors.js'; import type { CombinedUserData, UserConfig, UserData, UserSessionData } from '../types.js'; import { UserPermissionLevel } from '../types.js'; /** * Factory function to create user-related operations for authentication and user management. * * This function initializes and returns a set of user management utilities, including * username validation, avatar creation, user creation (local and OAuth), password management, * user data retrieval, and permission checks. It requires configuration for password hashing, * session management, and user tools. * * @param config - The configuration object for user management. * @param config.Scrypt - The password hashing implementation. * @param config.session - The session management configuration. * @param config.userTools - Utilities for user data access, creation, and notification. * @returns An Effect generator yielding an object with user management methods: * - `verifyUsernameInput`: Validates a username against length, character, and safety rules. * - `createUserAvatar`: Generates a Libravatar URL for a user's email. * - `createLocalUser`: Creates a new local user with the provided details. * - `createOAuthUser`: Creates a new user with OAuth credentials. * - `updateUserPassword`: Updates a user's password. * - `getUserPasswordHash`: Retrieves the password hash for a user. * - `getUserFromEmail`: Retrieves a user by their email address. * - `getUserData`: Retrieves user session data from the provided context. * - `getUserPermissionLevel`: Gets the user's permission level as an enum. * - `isUserAllowed`: Checks if a user has the required permission level. * * @example * ```typescript * const userModule = User({ Scrypt, session, userTools }); * ``` */ export declare const User: ({ Scrypt, session, userTools }: UserConfig) => Effect.Effect<{ readonly verifyUsernameInput: (username: string) => Effect.Effect<string | true, import("../errors.js").CheckIfUnsafeError | UserError, never>; readonly createUserAvatar: (email: string) => Effect.Effect.AsEffect<Effect.Effect<string, UserError, never>>; readonly createLocalUser: (name: string, username: string, email: string, password: string) => Effect.Effect<{ name: string; username: string; id: string; url: string | null; email: string | null; avatar: string | null; password: string | null; updatedAt: Date | null; createdAt: Date | null; emailVerified: boolean; notifications: string | null; }, import("@withstudiocms/effect/scrypt").ScryptError | UserError, never>; readonly createOAuthUser: (data: UserData, oAuthFields: { provider: string; providerUserId: string; }) => Effect.Effect<{ name: string; username: string; id: string; url: string | null; email: string | null; avatar: string | null; password: string | null; updatedAt: Date | null; createdAt: Date | null; emailVerified: boolean; notifications: string | null; }, UserError, never>; readonly updateUserPassword: (userId: string, password: string) => Effect.Effect<{ name: string; username: string; id: string; url: string | null; email: string | null; avatar: string | null; password: string | null; updatedAt: Date | null; createdAt: Date | null; emailVerified: boolean; notifications: string | null; }, import("@withstudiocms/effect/scrypt").ScryptError | UserError, never>; readonly getUserPasswordHash: (userId: string) => Effect.Effect<string, UserError, never>; readonly getUserFromEmail: (email: string) => Effect.Effect.AsEffect<Effect.Effect<CombinedUserData | null | undefined, UserError, never>>; readonly getUserData: (context: APIContext<Record<string, any>, Record<string, string | undefined>> | AstroGlobal<Record<string, any>, import("astro/runtime/server/index.js").AstroComponentFactory, Record<string, string | undefined>>) => Effect.Effect<UserSessionData, import("../errors.js").SessionError | UserError, never>; readonly getUserPermissionLevel: (userData: UserSessionData | CombinedUserData | null) => Effect.Effect<UserPermissionLevel, UserError, never>; readonly isUserAllowed: (userData: UserSessionData | CombinedUserData | null, requiredPerms: "owner" | "admin" | "editor" | "visitor" | "unknown") => Effect.Effect<boolean, UserError, never>; }, import("../errors.js").SessionError | UserError, never>;