wip
This commit is contained in:
103
examples/engine/10_email.rhai
Normal file
103
examples/engine/10_email.rhai
Normal file
@@ -0,0 +1,103 @@
|
||||
// Example 10: Communication - Email Verification
|
||||
|
||||
print("=== Communication & Email Verification Example ===\n");
|
||||
|
||||
// Get a context to work with
|
||||
print("1. Getting context...");
|
||||
let ctx = get_context(["alice", "bob"]);
|
||||
print(" ✓ Context created: " + ctx.context_id());
|
||||
|
||||
// Create verification for a user
|
||||
print("\n2. Creating verification...");
|
||||
|
||||
let verification = new_verification(
|
||||
"user_123",
|
||||
"alice@example.com"
|
||||
)
|
||||
.callback_url("https://example.com/verify");
|
||||
|
||||
print(" ✓ Created verification");
|
||||
print(" Entity ID: " + verification.get_entity_id());
|
||||
print(" Contact: " + verification.get_contact());
|
||||
print(" Code: " + verification.get_code());
|
||||
print(" Nonce: " + verification.get_nonce());
|
||||
print(" Verification URL: " + verification.get_verification_url());
|
||||
print(" Status: " + verification.get_status());
|
||||
|
||||
// Create email client
|
||||
print("\n3. Creating email client...");
|
||||
let email_client = new_email_client();
|
||||
print(" ✓ Email client created");
|
||||
|
||||
// Send verification email with link
|
||||
print("\n4. Sending verification email...");
|
||||
email_client.send_verification_link(verification);
|
||||
verification.mark_sent();
|
||||
print(" ✓ Verification email sent");
|
||||
print(" Status: " + verification.get_status());
|
||||
|
||||
// Save the verification
|
||||
print("\n5. Saving verification to context...");
|
||||
let verification_id = ctx.save(verification);
|
||||
print(" ✓ Saved verification with ID: " + verification_id);
|
||||
|
||||
// Simulate user clicking link (nonce verification)
|
||||
print("\n6. Simulating URL click verification...");
|
||||
let nonce = verification.get_nonce();
|
||||
print(" User clicks: " + verification.get_verification_url());
|
||||
verification.verify_nonce(nonce);
|
||||
print(" ✓ Verification successful via URL!");
|
||||
print(" Status: " + verification.get_status());
|
||||
|
||||
// Save verified status
|
||||
ctx.save(verification);
|
||||
|
||||
// Create another verification for code-based flow
|
||||
print("\n7. Creating code-based verification for Bob...");
|
||||
|
||||
let bob_verification = new_verification(
|
||||
"user_456",
|
||||
"bob@example.com"
|
||||
);
|
||||
|
||||
print(" ✓ Created verification for Bob");
|
||||
print(" Contact: " + bob_verification.get_contact());
|
||||
print(" Code: " + bob_verification.get_code());
|
||||
|
||||
// Send code via email
|
||||
print("\n8. Sending code-based verification email...");
|
||||
email_client.send_verification_code(bob_verification);
|
||||
bob_verification.mark_sent();
|
||||
print(" ✓ Code sent");
|
||||
|
||||
// Save Bob's verification
|
||||
let bob_verification_id = ctx.save(bob_verification);
|
||||
print(" ✓ Saved with ID: " + bob_verification_id);
|
||||
|
||||
// Simulate user entering wrong code
|
||||
print("\n9. Simulating code verification attempts...");
|
||||
print(" Attempt 1 with wrong code...");
|
||||
try {
|
||||
bob_verification.verify_code("000000");
|
||||
print(" ✗ Unexpected success");
|
||||
} catch(err) {
|
||||
print(" ✗ Verification failed (expected): " + err);
|
||||
print(" Attempts: " + bob_verification.get_attempts());
|
||||
}
|
||||
|
||||
// Verify with correct code
|
||||
print("\n10. Verifying with correct code...");
|
||||
let correct_code = bob_verification.get_code();
|
||||
print(" Using code: " + correct_code);
|
||||
bob_verification.verify_code(correct_code);
|
||||
print(" ✓ Verification successful!");
|
||||
print(" Status: " + bob_verification.get_status());
|
||||
|
||||
ctx.save(bob_verification);
|
||||
|
||||
// List all verifications
|
||||
print("\n11. Listing all verifications in context...");
|
||||
let verification_ids = ctx.list("verifications");
|
||||
print(" ✓ Found " + verification_ids.len() + " verifications");
|
||||
|
||||
print("\n=== Communication & Email Verification Example Complete ===");
|
||||
Reference in New Issue
Block a user