import * as Hapi from '@hapi/hapi' ; import * as Joi from 'joi' ; import { authenticate , storeUserInformation , createToken , generateAndSendVerificationCode } from '@auth/features/authenticate/service' ; import { generateNonce } from '@auth/features/verifyCode/service' ; import { unauthorized , forbidden } from '@hapi/boom' ; import { WEB_USER_JWT_AUDIENCES , JWT_ISSUER } from '@auth/constants' ; interface IAuthPayload { username : string ; password : string ; } interface IAuthResponse { nonce : string ; mobile : string ; status : string ; token ? : string ; } export default async function authenticateHandler ( request , h ) { const payload = request . payload as IAuthPayload ; let result ; const { username , password } = payload ; try { result = await authenticate ( username . trim ( ) , password ) ; } catch ( err ) { throw unauthorized ( ) ; } if ( result . status === 'deactivated' ) { throw forbidden ( ) ; } const nonce = generateNonce ( ) ; const response = { mobile : result . mobile , status : result . status , nonce } ; const isPendingUser = response . status && response . status === 'pending' ; if ( isPendingUser ) { response . token = await createToken ( result . userId , result . scope , WEB_USER_JWT_AUDIENCES , JWT_ISSUER ) ; } else { await storeUserInformation ( nonce , result . userId , result . scope , result . mobile ) ; await generateAndSendVerificationCode ( nonce , result . mobile , result . scope ) ; } return response ; } export const requestSchema = Joi . object ( { username : Joi . string ( ) , password : Joi . string ( ) } ) ; export const responseSchema = Joi . object ( { nonce : Joi . string ( ) , mobile : Joi . string ( ) , status : Joi . string ( ) , token : Joi . string ( ) . optional ( ) } ) ;