Email Confirmation via code sent by email

Hi Adam
Is there a way to ask within UD Stepper for Email Confirmation via code sent by email?
Anna

Hi, it seems like it should be possible with the -OnValidateStep parameter. Here’s a sample that’s working for me

New-UDStepper -Steps {
    # Get email address
    New-UDStep -Label "Step 1" -OnLoad {
        New-UDElement -tag 'div' -Content { 'Email address' }
        New-UDTextbox -Id 'EmailAddress' -Value $EventData.Context.EmailAddress
    } 

    # Send confirmation code and get code from user
    New-UDStep -Label "Step 2" -OnLoad {
        $Session:SentCode = New-Password -Length 6
        
        $mailParams = @{
            From       = 'jdoe@contoso.com'
            To         = $EventData.Context.EmailAddress
            Subject    = 'Confirmation code'
            Body       = $Session:SentCode
            SmtpServer = 'smtp.contoso.com'
        }
        Send-MailMessage @mailParams

        New-UDElement -tag 'div' -Content { "Confirmation code:" }
        New-UDTextbox -Id 'ConfirmationCode' -Value $EventData.Context.ConfirmationCode
    }
} -OnValidateStep {
    # Validate step 1
    if ($EventData.CurrentStep -eq 0) {
        if ($EventData.Context.EmailAddress -match '@') {
            New-UDValidationResult -Valid
        } else {
            New-UDValidationResult -ValidationError 'Not a valid email address'
        }
    }

    # Validate step 2
    if ($EventData.CurrentStep -eq 1) {
        if ($EventData.Context.ConfirmationCode -eq $Session:SentCode) {
            New-UDValidationResult -Valid
        } else {
            New-UDValidationResult -ValidationError 'Confirmation code is incorrect'
        }
    }
} -OnFinish {
    New-UDTypography -Text 'Nice! You did it!' -Variant h3
    New-UDElement -Tag 'div' -Id 'result' -Content { $Body }
}

And here’s the New-Password function on line 10:

function New-Password {
    param (
        [Int]$Length = 12
    )

    $characters = @()
    $characterRanges = '33,33', '35,38', '40,94', '97,126'
    $characterRanges | ForEach-Object {
        $range = $_
        [Int]$x, [Int]$y = $range.Split(',')
        $x..$y | ForEach-Object {
            $characters += ($([char]$_))
        }
    }

    $password = @()
    do {
        $password += $characters | Get-Random
    } until (
        ($password -join '').Length -eq $Length
    )
    $password = $password -join ''

    return $password
}
4 Likes