import { PREFIX_URL, ONECLICK_API_URL as apiUrl, DISPLAY_CODE_URL as displayCodeUrl, PHONE_FAILURE_URL as failureUrl, PHONE_SUCCESS_URL as successUrl, } from '../lib/constants' import { isEmpty, isNil, merge } from 'ramda' import MFA from '@oyst/mfa' import PhoneChecker from '@oyst/phone-checker-lambda' import UserAPI from '../apis/user' import authorize from '../handlers/authorize' import log from '../lib/log' import { orderSession } from '../sessions' const prefix = PREFIX_URL.length ? `${PREFIX_URL}/v1` : undefined const variables = { apiUrl: `${apiUrl}/v1`, displayCodeUrl, failureUrl, successUrl, smsType: 'oneclick.enroll', } const hooks = { check: { after: async data => { const { sessionID, phone, uuid } = data await orderSession.setData(sessionID, { success: true }, 'mfa-validated') let loginUser try { loginUser = await orderSession.getData<{ phone: string, session: string, user_id: string }>(sessionID, 'login-user') } catch (err) { return merge(data, { sessionID, phone, uuid }) } if (isNil(loginUser) || isEmpty(loginUser)) { return merge(data, { sessionID, phone, uuid }) } const { user_id, session } = loginUser const user = await UserAPI.update(user_id, { phone, last_session_id: session }) return merge(data, { sessionID, phone, uuid }) }, pre: [authorize] }, resend: { after: async data => { const { sessionID, phone, uuid } = data await orderSession.setData(sessionID, { uuid }, 'phone-session') const session = await orderSession.getData<{ phone: string }>(sessionID, 'phone') session.phone = phone await orderSession.setData(sessionID, session, 'phone') return merge(data, { sessionID, phone, uuid }) }, before: async data => { const { sessionID } = data const { phone } = await orderSession.getData<{ phone: string }>(sessionID, 'phone') if (!phone) { throw new Error('phone-not-found') } const { uuid } = await orderSession.getData<{ uuid: string }>(sessionID, 'phone-session') return merge(data, { sessionID, phone, uuid }) }, pre: [authorize] } } export default { options: { hooks, variables }, register: MFA, routes: { prefix } }