Forge

Jessie / forge public

Self-hosted git - made by qwen 3.8 27b

commit: 0c9688e9

Branches

main
Setup.razor 3.8 kB · 119 lines RawHistory
@page "/setup"
@attribute [EnableRateLimiting(ForgeApp.LoginRateLimit)]
@using Microsoft.AspNetCore.Identity
@inject ForgeDb Db
@inject IPasswordHasher<User> Hasher
@inject IHttpContextAccessor Http
@inject NavigationManager Nav

@if (_open)
{
    <PageTitle>@T["First run"] · Forge</PageTitle>

    <section class="narrow">
        <div class="auth-logo"><Icon Name="logo" Size="36" Class="logo-mark"/></div>
        <h1>@T["Welcome to Forge"]</h1>
        <p class="muted">@T["Create your account. This page then closes for good."]</p>

        @if (_error is not null)
        {
            <p class="alert error">@_error</p>
        }

        <form method="post" @formname="setup" @onsubmit="CreateAsync" class="form">
            <AntiforgeryToken/>
            <label>
                @T["Username"]
                <input name="Username" value="@Username" required maxlength="39" autocomplete="username" autofocus/>
                <small class="muted">@T["It becomes part of your repository addresses."]</small>
            </label>
            <label>
                @T["Display name"]
                <input name="DisplayName" value="@DisplayName" maxlength="100" autocomplete="name"/>
            </label>
            <label>
                @T["Password"]
                <input name="Password" type="password" required minlength="@AccountRules.MinPasswordLength" autocomplete="new-password"/>
            </label>
            <label>
                @T["Repeat password"]
                <input name="PasswordRepeat" type="password" required autocomplete="new-password"/>
            </label>
            <button type="submit" class="button primary">@T["Create account"]</button>
        </form>
    </section>
}

@code {
    [SupplyParameterFromForm]
    private string? Username { get; set; }

    [SupplyParameterFromForm]
    private string? DisplayName { get; set; }

    [SupplyParameterFromForm]
    private string? Password { get; set; }

    [SupplyParameterFromForm]
    private string? PasswordRepeat { get; set; }

    private bool _open;
    private string? _error;

    protected override async Task OnInitializedAsync()
    {
        // Setup exists only while there is no account at all.
        if (await Db.Users.AnyAsync())
        {
            Nav.NotFound();
            return;
        }

        _open = true;
    }

    private async Task CreateAsync()
    {
        if (!_open)
        {
            return;
        }

        var username = Username?.Trim();
        var invalid = AccountRules.ValidateUsername(username)
                      ?? AccountRules.ValidatePassword(Password)
                      ?? (Password != PasswordRepeat ? Localizer.Key("The passwords do not match.") : null);
        if (invalid is not null)
        {
            // The rules return English templates; {0}, where present, is the minimum password length.
            _error = T[invalid, AccountRules.MinPasswordLength];
            return;
        }

        var user = new User
        {
            Username = username!,
            DisplayName = string.IsNullOrWhiteSpace(DisplayName) ? username! : DisplayName.Trim(),
            CreatedAt = DateTime.UtcNow,
        };
        user.PasswordHash = Hasher.HashPassword(user, Password!);

        // The check and the insert share one write transaction, so two simultaneous setups cannot both win.
        await using (var transaction = await Db.Database.BeginTransactionAsync())
        {
            if (await Db.Users.AnyAsync())
            {
                _open = false;
                Nav.NotFound();
                return;
            }

            Db.Users.Add(user);
            await Db.SaveChangesAsync();
            await transaction.CommitAsync();
        }

        await CurrentUser.SignInAsync(Http.HttpContext!, user);
        Nav.NavigateTo("/");
    }
}

Keyboard shortcuts

/
Search repositories
s
Search file names and code
y
Open the permalink of the file being viewed
?
Show this help