UNPKG

@mussnad/frappe-js-client

Version:

Next-generation TS/JS client for Frappe REST APIs, built on axios for robust, type-safe integration.

149 lines (148 loc) 4.55 kB
/** * @module auth * @description Provides authentication functionality for Frappe. * This module handles user authentication, session management, * and password reset operations. * * @packageDocumentation * * @example * ```typescript * import { FrappeApp } from '@frappe/sdk'; * * const app = new FrappeApp('https://instance.example.com'); * const auth = app.auth(); * * await auth.loginWithUsernamePassword({ * username: 'admin', * password: 'admin' * }); * * const user = await auth.getLoggedInUser(); * ``` */ import { AxiosInstance } from 'axios'; import { AuthCredentials, AuthResponse } from './types'; /** * Handles authentication operations for Frappe. * * @class FrappeAuth * @description Provides methods for user authentication, session management, * and password reset functionality. * * @example * ```typescript * const auth = new FrappeAuth( * 'https://instance.example.com', * axiosInstance, * true, * () => localStorage.getItem('token'), * 'Bearer' * ); * * // Login * await auth.loginWithUsernamePassword({ * username: 'admin', * password: 'admin' * }); * ``` */ export declare class FrappeAuth { /** URL of the Frappe App instance */ private readonly appURL; /** Axios instance for making HTTP requests */ readonly axios: AxiosInstance; /** Whether to use token based authentication */ readonly useToken: boolean; /** Function that returns the authentication token */ readonly token?: () => string; /** Type of token to be used for authentication */ readonly tokenType?: 'Bearer' | 'token'; /** * Creates a new FrappeAuth instance. * * @param appURL - Base URL of the Frappe instance * @param axios - Configured Axios instance for making requests * @param useToken - Whether to use token-based authentication * @param token - Function that returns the authentication token * @param tokenType - Type of token to use ('Bearer' or 'token') * * @example * ```typescript * const auth = new FrappeAuth( * 'https://instance.example.com', * axiosInstance, * true, * () => localStorage.getItem('token'), * 'Bearer' * ); * ``` */ constructor(appURL: string, axios: AxiosInstance, useToken?: boolean, token?: () => string, tokenType?: 'Bearer' | 'token'); /** * Authenticates a user using username/password or OTP. * * @param credentials - Authentication credentials (username/password or OTP) * @returns Promise resolving to the authentication response * @throws {FrappeError} If authentication fails * * @example * ```typescript * // Login with username/password * const response = await auth.loginWithUsernamePassword({ * username: 'admin', * password: 'admin', * device: 'desktop' * }); * * // Login with OTP * const otpResponse = await auth.loginWithUsernamePassword({ * otp: '123456', * tmp_id: 'temp123', * device: 'mobile' * }); * ``` */ loginWithUsernamePassword<T extends AuthResponse = AuthResponse>(credentials: AuthCredentials): Promise<T>; /** * Retrieves the currently logged-in user. * * @param method - The method to use to get the logged in user * @returns Promise resolving to the username of the logged-in user * @throws {FrappeError} If the request fails or no user is logged in * * @example * ```typescript * const username = await auth.getLoggedInUser(); * console.log(`Logged in as: ${username}`); * ``` */ getLoggedInUser<T = any>(method?: string): Promise<T>; /** * Logs out the current user. * * @returns Promise resolving when logout is successful * @throws {FrappeError} If logout fails * * @example * ```typescript * await auth.logout(); * console.log('User logged out successfully'); * ``` */ logout<T extends AuthResponse = AuthResponse>(): Promise<T>; /** * Initiates the password reset process for a user. * * @param user - Username or email of the user * @returns Promise resolving when the reset email is sent * @throws {FrappeError} If the password reset request fails * * @example * ```typescript * await auth.forgetPassword('user@example.com'); * console.log('Password reset email sent'); * ``` */ forgetPassword<T = any>(user: string): Promise<T>; }