UNPKG

@earnaha/auth0-action-helper

Version:
122 lines (110 loc) 3.26 kB
/* eslint-disable no-console */ const BaseHelper = require('./base.js'); class PostChangePasswordHelper extends BaseHelper { async acceptInvitation(api, payload) { const accessKey = await super.getServerAccessKey(api); const res = await super.axiosPostWithBreadcrumb( `${super.DOMAIN}/auth/v3/refer/invitation/accept`, payload, { headers: { ServerAccessKey: accessKey, }, }, ); return res; } /** * Handler that will be called during the execution of a PostChangePassword flow. * @param {Event} event - Details about the user and the context in which the change password is happening. * @param {PostChangePasswordAPI} api - Methods and utilities to help change the behavior after a user changes their password. */ async exec(event, api) { const auth0Id = event?.user?.user_id; let scprLogs = null; let scpLogs = null; try { super.LOGGER.setMeta({ auth0Id }); const userToken = await super.getUserToken(); scprLogs = await super.searchChangePasswordRequestSucceededLogs( event, userToken, ); super.LOG.debug( { scprLogCount: scprLogs?.data?.length }, `PostChangePassword.exec | ChangePasswordRequestSucceededLogs`, ); scpLogs = await super.searchSuccessChangePasswordLogs( event, userToken, ); super.LOG.debug( { scpLogCount: scpLogs?.data?.length }, `PostChangePassword.exec | SuccessChangePasswordLogs`, ); const userLogs = scpLogs && Array.isArray(scpLogs.data) && scpLogs.data.length > 0 ? scpLogs.data : scprLogs?.data; let inviterId = null; if (userLogs && Array.isArray(userLogs) && userLogs.length > 0) { const [lastScpLog] = userLogs; const resultUrl = lastScpLog?.details?.query?.resultUrl || lastScpLog?.details?.body?.resultUrl; super.LOG.debug( { lastLog: lastScpLog, urlInQuery: lastScpLog?.details?.query?.resultUrl, urlInBody: lastScpLog?.details?.body?.resultUrl, parsedUrl: resultUrl, }, `PostChangePassword.exec | possible resultUrl`, ); if (resultUrl) { const [urlPart, referPart] = resultUrl.split('?refer='); inviterId = referPart; super.LOG.debug( { urlPart, referPart, inviterId }, `PostChangePassword.exec | destructured resultUrl`, ); } } if (inviterId === null) { super.LOG.debug( { ChangePasswordRequestSucceededLogs: scprLogs?.data, SuccessChangePasswordLogs: scpLogs?.data, }, `PostChangePassword.exec | no inviter id, check log details`, ); } const payload = { receiverAuth0Id: event.user.user_id, inviterMemberId: inviterId, }; if (inviterId !== null) { const { data: resData } = await this.acceptInvitation( api, payload, ); super.LOG.debug( { data: resData?.data }, `PostChangePassword.exec | response of api-auth invitation acceptance api`, ); return resData?.data; } return null; } catch (e) { super.LOG.error( { error: e?.response?.data || e?.message || `${e}` }, `PostChangePassword.exec | caught exception`, ); super.SENTRY.captureException(e); return api.access.deny(e.message); } } } module.exports = PostChangePasswordHelper;