Skip to content
Page AuthForm

AuthForm

A sign-in or sign-up form built from a description of its fields, with OAuth provider buttons, validation and a loading state. It posts in static rendering too.

On this page

Examples

Sign in

Fields are described by KlyAuthField records. Required fields, email format and a custom Validate callback run on submit; the password has a show/hide button that works without .NET.

Welcome back

Sign in to your account to continue.

Forgot your password?
Razor
<div class="flex w-full flex-col items-center gap-4">
    <KlyAuthForm FormName="auth-sign-in" Title="Welcome back" Description="Sign in to your account to continue."
                Icon="@KlyIcons.Named("lock")" Fields="Fields" Providers="Providers" SubmitLabel="Sign in"
                Validate="CheckPassword" Error="@_error" OnValidSubmit="SignInAsync">
        <Validation>
            <a href="/docs" class="text-sm text-(--klyroo-primary-text) underline underline-offset-4">Forgot your password?</a>
        </Validation>
        <Footer>New here? <a href="/docs" class="font-medium text-(--klyroo-primary-text) underline underline-offset-4">Create an account</a></Footer>
    </KlyAuthForm>
    @if (_signedIn is not null)
    {
        <p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="auth-signed-in">Signed in as @_signedIn.</p>
    }
</div>

@code {
    private string? _error;
    private string? _signedIn;

    private static readonly KlyAuthField[] Fields =
    [
        new("email", KlyAuthFieldType.Email) { Label = "Email", Placeholder = "you@example.com", Required = true, Autocomplete = "email" },
        new("password", KlyAuthFieldType.Password) { Label = "Password", Required = true, Autocomplete = "current-password" },
        new("remember", KlyAuthFieldType.Checkbox) { Label = "Remember me" },
    ];

    private static readonly KlyAuthProvider[] Providers =
    [
        new("Continue with GitHub", "/docs") { Icon = "code" },
        new("Continue with Google", "/docs") { Icon = "globe" },
    ];

    private static IReadOnlyDictionary<string, string>? CheckPassword(IReadOnlyDictionary<string, string?> values) =>
        values.GetValueOrDefault("password") is { Length: < 8 }
            ? new Dictionary<string, string> { ["password"] = "Use at least 8 characters." }
            : null;

    private async Task SignInAsync(IReadOnlyDictionary<string, string?> values)
    {
        _error = null;
        // Stands for a slow call: the form is busy and its fields are disabled meanwhile.
        await Task.Delay(300);
        if (values["email"] == "locked@example.com")
        {
            _error = "This account is locked. Contact your administrator.";
            return;
        }
        _signedIn = values["email"];
    }
}

Sign up

Checkbox and select fields, a help text and a different color.

Create your account

Start your 14-day trial, no credit card needed.

At least 8 characters.

Developer
Designer
Product manager
Razor
<div class="flex w-full flex-col items-center gap-4">
    <KlyAuthForm FormName="auth-sign-up" Title="Create your account" Description="Start your 14-day trial, no credit card needed."
                Fields="Fields" SubmitLabel="Create account" OnValidSubmit="SignUpAsync" Color="KlyColor.Secondary"
                Separator="@null" HeadingLevel="2" />
    @if (_created is not null)
    {
        <p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="auth-created">Account created for @_created.</p>
    }
</div>

@code {
    private string? _created;

    private static readonly KlyAuthField[] Fields =
    [
        new("name", KlyAuthFieldType.Text) { Label = "Full name", Required = true, Autocomplete = "name" },
        new("email", KlyAuthFieldType.Email) { Label = "Work email", Placeholder = "you@company.com", Required = true, Autocomplete = "email" },
        new("password", KlyAuthFieldType.Password) { Label = "Password", Required = true, Autocomplete = "new-password", Help = "At least 8 characters." },
        new("role", KlyAuthFieldType.Select)
        {
            Label = "Your role",
            Placeholder = "Pick a role",
            Items = [new("dev", "Developer"), new("design", "Designer"), new("pm", "Product manager")],
        },
        new("terms", KlyAuthFieldType.Checkbox) { Label = "I agree to the terms of service", Required = true },
    ];

    private Task SignUpAsync(IReadOnlyDictionary<string, string?> values)
    {
        _created = values["email"];
        return Task.CompletedTask;
    }
}

One-time code

An otp field is a KlyPinInput. Server-side messages come back through FieldErrors. Enhance updates the page without a reload.

Check your email

We sent a 6-digit code to you@example.com.

The code expires in 10 minutes.

Razor
<div class="flex w-full flex-col items-center gap-4">
    <KlyAuthForm FormName="auth-verify" Title="Check your email" Description="We sent a 6-digit code to you@example.com."
                Fields="Fields" SubmitLabel="Verify" OnValidSubmit="VerifyAsync" FieldErrors="_fieldErrors" Enhance />
    @if (_verified)
    {
        <p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="auth-verified">Email verified.</p>
    }
</div>

@code {
    private bool _verified;
    private IReadOnlyDictionary<string, string>? _fieldErrors;

    private static readonly KlyAuthField[] Fields =
    [
        new("code", KlyAuthFieldType.Otp) { Label = "Verification code", Length = 6, Required = true, Help = "The code expires in 10 minutes." },
    ];

    private Task VerifyAsync(IReadOnlyDictionary<string, string?> values)
    {
        // 123456 stands for the code the server sent.
        if (values["code"] == "123456")
        {
            _fieldErrors = null;
            _verified = true;
        }
        else
        {
            _fieldErrors = new Dictionary<string, string> { ["code"] = "That code is not right. Try again." };
        }
        return Task.CompletedTask;
    }
}

API

KlyAuthForm

Sign-in, sign-up or password reset form built from a list of fields, with optional provider buttons, validation and a submit button.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the auth form: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else the default of the component. Values: primary, secondary, success, info, warning, error, neutral.
Description string Text under the title of the form.
Disabled bool false Disables every field and the submit button.
Enhance bool false Enhanced form handling in static server rendering: the post updates the page without a reload.
Error string A message about the whole form (wrong password): put it here after OnValidSubmit failed.
FieldErrors IReadOnlyDictionary<string, string> Messages by field name, for what only the server can tell (the email is taken). Shown after the built-in ones.
Fields (required) IReadOnlyList<KlyAuthField> The fields, in order.
FormName string Name of the form. Required to post in static server rendering, where it identifies the posted form. Several auth forms can share a page when their names differ.
HeadingLevel int 2 Level of the title heading: 2 by default.
Loading bool false Shows the submit button as loading and disables the fields, as it does itself while OnValidSubmit runs.
Providers IReadOnlyList<KlyAuthProvider> Buttons that leave for another address (OAuth), above the fields.
Separator string Text between the providers and the fields: KlyMessages.AuthFormSeparator by default. An empty string hides the line.
SubmitLabel string Label of the submit button.
Title string Title of the form, such as "Sign in".
Ui AuthFormUi Per-instance class overrides of the slots of the auth form, see AuthFormUi . They are merged after the theme and the Class parameter wins on the root slot.
Validate Func<IReadOnlyDictionary<string, string>, IReadOnlyDictionary<string, string>> Extra validation after the required, email and code checks, with the values by field name. Returns messages by field name.

Events

Name Type Description
OnInvalidSubmit EventCallback<IReadOnlyDictionary<string, string>> Raised when the form is submitted and validation fails, with the current values of the fields by name.
OnValidSubmit EventCallback<IReadOnlyDictionary<string, string>> Called with the values by field name once they are valid. The form is busy until the task completes.

Slots

Name Type Description
Footer RenderFragment Content under the submit button, such as a link to another form.
Header RenderFragment Replaces the title block.
Icon RenderFragment Icon above the title.
Submit RenderFragment Replaces the submit button.
Validation RenderFragment Content between the fields and the submit button (a "forgot password" link).

Ui slots

Ui takes a AuthFormUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyAuthForm , after the theme and the app configuration.
Header Classes merged into the header slot of KlyAuthForm , after the theme and the app configuration.
Leading Classes merged into the leading slot of KlyAuthForm , after the theme and the app configuration.
Title Classes merged into the title slot of KlyAuthForm , after the theme and the app configuration.
Description Classes merged into the description slot of KlyAuthForm , after the theme and the app configuration.
Providers Classes merged into the providers slot of KlyAuthForm , after the theme and the app configuration.
Form Classes merged into the form slot of KlyAuthForm , after the theme and the app configuration.
Fieldset Classes merged into the fieldset slot of KlyAuthForm , after the theme and the app configuration.
Error Classes merged into the error slot of KlyAuthForm , after the theme and the app configuration.
Footer Classes merged into the footer slot of KlyAuthForm , after the theme and the app configuration.
PasswordRoot Classes merged into the passwordRoot slot of KlyAuthForm , after the theme and the app configuration.
PasswordButton Classes merged into the passwordButton slot of KlyAuthForm , after the theme and the app configuration.