UNPKG

pagamio-frontend-commons-lib

Version:

Pagamio library for Frontend reusable components like the form engine and table container

100 lines (99 loc) 4.11 kB
/** * Transforms a valid Strapi response into our application's auth response format * @param strapiResponse - Validated Strapi auth response * @returns Transformed auth response */ export function transformStrapiResponse(strapiResponse) { // Create a new user object that conforms to T['UserInfo'] // This approach avoids direct type assertion of the entire object const userInfo = { id: strapiResponse.user.id, userName: strapiResponse.user.username, firstName: strapiResponse.user.firstName, lastName: strapiResponse.user.lastName, email: strapiResponse.user.email, blocked: strapiResponse.user.blocked, company: strapiResponse.user.company, isFirstTimeLogin: strapiResponse.user.isFirstTimeLogin, hasCompletedOnboarding: strapiResponse.user.hasCompletedOnboarding, hasSkippedOnboarding: strapiResponse.user.hasSkippedOnboarding, onBoardingReminderTime: strapiResponse.user.onBoardingReminderTime, loginCount: strapiResponse.user.loginCount, roleName: strapiResponse.user.role.name, roleCode: strapiResponse.user.role.type, }; return { user: userInfo, auth: { accessToken: { token: strapiResponse.jwt, expiresIn: 36000000, // Default to 10 hours if not provided }, }, }; } /** * Validates if the response is a valid Strapi authentication response * @param response - API response to validate * @returns Boolean indicating if response is a valid Strapi auth response */ export function isStrapiAuthResponse(response) { if (!response || typeof response !== 'object') { return false; } const potentialStrapiResponse = response; // Check if jwt exists and is a non-empty string if (typeof potentialStrapiResponse.jwt !== 'string' || potentialStrapiResponse.jwt.trim() === '') { return false; } // Check if user exists and is an object if (!potentialStrapiResponse.user || typeof potentialStrapiResponse.user !== 'object') { return false; } // Optional: Add additional validation for critical user properties // For example, check if required user fields are present const user = potentialStrapiResponse.user; if (typeof user.id !== 'number' || typeof user.email !== 'string' || user.email?.trim() === '') { return false; } return true; } /** * Strapi authenticator processor * Handles authentication process specific to Strapi backend * @template T - Type extending CustomAuthConfig */ export class StrapiAuthenticatorProcessor { /** * Process login credentials using the Strapi authentication flow * @param credentials - User login credentials * @param rememberMe - Whether to remember the user session * @param login - The login function from auth context * @returns Promise resolving to authentication response */ async processLogin(credentials, rememberMe, login) { // Strapi typically expects credentials in identifier/password format // Adapt the credentials if needed for Strapi API const strapiCredentials = {}; // If username is provided, map it to identifier if ('username' in credentials) { strapiCredentials.identifier = credentials.username; } // Copy the password if ('password' in credentials) { strapiCredentials.password = credentials.password; } // Copy any additional fields Object.entries(credentials).forEach(([key, value]) => { if (key !== 'username' && !strapiCredentials[key]) { strapiCredentials[key] = value; } }); // Validate the credentials if (!strapiCredentials.identifier || !strapiCredentials.password) { throw new Error('Invalid Strapi login data: identifier and password are required'); } // Use type assertion to satisfy the login function parameter type return login(strapiCredentials, rememberMe); } }