UNPKG

@mussnad/frappe-js-client

Version:

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

200 lines (199 loc) 6.26 kB
"use strict"; /** * @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(); * ``` */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FrappeAuth = void 0; var axios_1 = require("../utils/axios"); /** * 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' * }); * ``` */ var FrappeAuth = /** @class */ (function () { /** * 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' * ); * ``` */ function FrappeAuth(appURL, axios, useToken, token, tokenType) { this.appURL = appURL; this.axios = axios; this.useToken = useToken !== null && useToken !== void 0 ? useToken : false; this.token = token; this.tokenType = tokenType; } /** * 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' * }); * ``` */ FrappeAuth.prototype.loginWithUsernamePassword = function (credentials) { return (0, axios_1.handleRequest)({ axios: this.axios, config: { method: 'POST', url: '/api/method/login', data: { usr: credentials.username, pwd: credentials.password, otp: credentials.otp, tmp_id: credentials.tmp_id, device: credentials.device, }, }, errorMessage: 'There was an error while logging in', transformResponse: function (response) { return response.data; }, }); }; /** * 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}`); * ``` */ FrappeAuth.prototype.getLoggedInUser = function (method) { return (0, axios_1.handleRequest)({ axios: this.axios, config: { url: "/api/method/".concat(method !== null && method !== void 0 ? method : 'frappe.auth.get_logged_user'), }, errorMessage: 'There was an error while fetching the logged in user', transformResponse: function (response) { return response.message; }, }); }; /** * 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'); * ``` */ FrappeAuth.prototype.logout = function () { return (0, axios_1.handleRequest)({ axios: this.axios, config: { method: 'POST', url: '/api/method/logout', data: {}, }, errorMessage: 'There was an error while logging out', transformResponse: function (response) { return response.data; }, }); }; /** * 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'); * ``` */ FrappeAuth.prototype.forgetPassword = function (user) { return (0, axios_1.handleRequest)({ axios: this.axios, config: { method: 'POST', url: '/', data: { cmd: 'frappe.core.doctype.user.user.reset_password', user: user, }, }, errorMessage: 'There was an error while sending password reset email', transformResponse: function (response) { return response.data; }, }); }; return FrappeAuth; }()); exports.FrappeAuth = FrappeAuth;