import * as Hapi from '@hapi/hapi' ; import * as Joi from 'joi' ; import { storeRetrievalStepInformation , getRetrievalStepInformation , RetrievalSteps , IRetrievalStepInformation } from '@auth/features/retrievalSteps/verifyUser/service' ; import { checkVerificationCode } from '@auth/features/verifyCode/service' ; import { unauthorized } from '@hapi/boom' ; import { logger } from '@auth/logger' ; interface IVerifyNumberPayload { nonce : string ; code : string ; } interface IVerifyNumberResponse { nonce : string ; securityQuestionKey : string ; } export default async function verifyNumberHandler ( request , h ) { const payload = request . payload as IVerifyNumberPayload ; let retrievalStepInfo ; retrievalStepInfo = await getRetrievalStepInformation ( payload . nonce ) . catch ( ( ) => { throw unauthorized ( ) ; } ) ; if ( retrievalStepInfo . status !== RetrievalSteps . WAITING_FOR_VERIFICATION . toString ( ) ) { throw unauthorized ( ) ; } try { await checkVerificationCode ( payload . nonce , payload . code ) ; } catch ( err ) { logger . error ( err ) ; throw unauthorized ( ) ; } await storeRetrievalStepInformation ( payload . nonce , RetrievalSteps . NUMBER_VERIFIED , retrievalStepInfo ) ; const response = { securityQuestionKey : retrievalStepInfo . securityQuestionKey , nonce : payload . nonce } ; return response ; } export const requestSchema = Joi . object ( { nonce : Joi . string ( ) . required ( ) , code : Joi . string ( ) . required ( ) } ) ; export const responseSchema = Joi . object ( { nonce : Joi . string ( ) , securityQuestionKey : Joi . string ( ) } ) ;