IAuthenticationHook

A authentication hook is a plugin which can be applied to the LoginForm component and allows developers to perform certain actions after a user has authenticated.  It can also perform some validation logic before the authentication takes place, like checking if the user exists in a backend system or is a paying customer to name some examples.

Interface declaration

	public interface IAuthenticationHook
	{
		AuthenticationValidationResult ValidateLogin(string username, string password);

		void AfterLogin(bool loginSuccessful, string username, string password);
	}

Example

This is an example of an AuthenticationHook which has a toggle switch to temporily disable all logins and a customizable message which is shown to the user when logins are disabled.

[DisplayName("Prevent authentication")]
	[Description("This authentication hook can be used to temporarily prevent login attempts")]
	public class PreventAuthenticationHook : IAuthenticationHook
	{
		[EditorBrowsable]
		[DisplayName("Prevent logins")]
		[Description("If this is checked, no logins are permitted")]
		public bool PreventLogins { get; set; }

		[EditorBrowsable]
		[DisplayName("Message")]
		[Description("The message to show to the user if logins are prevented")]
		public string Message { get; set; }

		public void AfterLogin(bool loginSuccessful, string username, string password)
		{
			if (loginSuccessful)
			{
				Console.WriteLine(string.Format("The user {0} successfully logged in!", username));
			}
			else
			{
				Console.WriteLine(string.Format("Unsuccessful login attempt by the user {0}!", username));
			}
		}

		public AuthenticationValidationResult ValidateLogin(string username, string password)
		{
			return new AuthenticationValidationResult() { AuthenticationSucceeded = !PreventLogins, Messages = new List<string>() { Message } };
		}
	}

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<IAuthenticationHook, PreventAuthenticationHook>("PreventAuthenticationHook");
		}

Usage

To use this AuthenticationHook, open the page which contains the LoginForm component and click on it.  In the settings on the right hand side, add the "Prevent authentication" hook to the list of hooks.