ICreateAccountHook
This interface allows you to define hooks that can validate and perform actions during the account creation process in Blazor components. The hook provides two key methods: one for validating account creation data before the account is created, and another for performing actions after the account has been successfully registered.
Interface declaration
public interface ICreateAccountHook { CreateAccountValidationResult Validate(CreateAccountHookContext context); void AfterRegistration(CreateAccountHookContext context); }
Example
In this example, we are implementing a custom validation hook that checks if the email domain is allowed and adds custom validation messages. We also demonstrate how to perform additional actions after successful registration, such as logging or sending notifications.
public class CustomCreateAccountHook : ICreateAccountHook { public CreateAccountValidationResult Validate(CreateAccountHookContext context) { var result = new CreateAccountValidationResult { Succeeded = true }; var email = context.InputFormContext.GetValueAsString("Email"); if (!string.IsNullOrEmpty(email) && !email.EndsWith("@company.com")) { result.Succeeded = false; result.AddMessage("Only company email addresses are allowed."); } return result; } public void AfterRegistration(CreateAccountHookContext context) { // Log the new user registration var logger = serviceProvider.GetService<ILogger>(); logger.LogInformation($"New user registered: {context.CreatedUser.Email}"); // Send welcome email or perform other post-registration actions } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { services.AddNamed<ICreateAccountHook, CustomCreateAccountHook>(ServiceLifetime.Transient); }