'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { Requests , RequestType } from '@Entities/Requests' ; import { Accounts } from '@Entities/Accounts' ; import { HTTPStatusCode } from '@Route-Tools/RESTResponse' ; import { IsNotNullOrEmpty } from '@Tools/Misc' ; import { Config } from '@Base/config' ; import { logger , Logger } from '@Tools/Logging' ; import { AccountEntity } from '@Entities/AccountEntity' ; const procGetVerifyAccount = async ( req , resp , next ) => { logger . debug ( `procGetVerifyAccount: ` ) ; if ( typeof ( req . query . a ) === 'string' && IsNotNullOrEmpty ( req . query . a ) && typeof ( req . query . v ) === 'string' && IsNotNullOrEmpty ( req . query . v ) ) { const accountId = req . query . a ; const verifyCode = req . query . v ; logger . debug ( `procGetVerifyAccount: acctId=${ accountId }, code=${ verifyCode }` ) ; const request = await Requests . getWithRequesterOrTarget ( accountId , RequestType . VERIFYEMAIL ) ; if ( request ) { if ( request . verificationCode === verifyCode ) { const aAccount = await Accounts . getAccountWithId ( accountId ) ; if ( aAccount ) { Accounts . doEnableAccount ( aAccount ) ; Logger . info ( `procGetVerifyAccount: account ${ accountId } verified and enabled` ) ; await Requests . remove ( request ) ; if ( IsNotNullOrEmpty ( Config [ 'metaverse-server' ] [ 'email-verification-success-redirect' ] ) ) { const redirectURL = BuildRedirect ( Config [ 'metaverse-server' ] [ 'email-verification-success-redirect' ] , accountId ) ; Logger . debug ( `procGetVerifyAccount: Redirect success to ${ redirectURL }` ) ; resp . statusCode = HTTPStatusCode . Found ; resp . setHeader ( 'Location' , redirectURL ) ; resp . setHeader ( 'content-type' , 'text/html' ) ; resp . send ( ) ; } } else { await Requests . remove ( request ) ; Logger . error ( `procGetVerifyAccount: account ${ accountId } could not be found for verification` ) ; RedirectFailure ( resp , accountId , 'Not verified. Missing account' ) ; } ; } else { Logger . error ( `procGetVerifyAccount: verification code did not match for  ${ accountId }` ) ; RedirectFailure ( resp , accountId , 'Not verified' ) ; } ; } else { Logger . error ( `procGetVerifyAccount: attempt to verify account ${ accountId } but no pending verification request` ) ; RedirectFailure ( resp , accountId , 'Not verified. No pending verification request' ) ; } ; } else { Logger . error ( `procGetVerifyAccount: attempted verification with no parameters` ) ; RedirectFailure ( resp , 'UNKNOWN' , 'Verification parameters not present' ) ; } ; } ; function BuildRedirect ( pUrl , pAccountId , pReason ? ) { let url = pUrl ; if ( IsNotNullOrEmpty ( pReason ) ) { url = url . replace ( "FAILURE_REASON" , encodeURI ( pReason ) ) ; } return url . replace ( "METAVERSE_SERVER_URL" , Config . metaverse [ 'metaverse-server-url' ] ) . replace ( "DASHBOARD_URL" , Config . metaverse [ 'dashboard-url' ] ) . replace ( "ACCOUNT_ID" , pAccountId ) ; } ; function RedirectFailure ( pResp , pAccountId , pReason ) { const redirectURL = BuildRedirect ( Config [ 'metaverse-server' ] [ 'email-verification-failure-redirect' ] , pAccountId , pReason ) ; Logger . debug ( `procGetVerifyAccount: Redirect failure to ${ redirectURL }` ) ; pResp . statusCode = HTTPStatusCode . Found ; pResp . setHeader ( 'Location' , redirectURL ) ; pResp . setHeader ( 'content-type' , 'text/html' ) ; pResp . send ( ) ; } ; export const name = '/api/v1/account/verify/email' ; export const router = Router ( ) ; router . get ( '/api/v1/account/verify/email' , [ setupMetaverseAPI , procGetVerifyAccount , finishMetaverseAPI ] ) ;