@page "/login"
@inject NavigationManager Navigation
@inject AuthenticationStateProvider Authentication

<Title Value="Login" />

<h1>Login</h1>

@if (_loginFailed)
{
  <p>Could not log you in :'(</p>
}

<EditForm Model="_model" OnValidSubmit="TryLogin">
  <DataAnnotationsValidator />
  <ValidationSummary />

  <label for="username">Username</label>
  <InputText id="username" @bind-Value="_model.Username" />

  <label for="password">Password</label>
  <InputText id="password" type="password" @bind-Value="_model.Password" />

  <button type="submit">Log me in!</button>
</EditForm>

@code {
  private LoginViewModel _model = new LoginViewModel();
  private  bool _loginFailed;

  private async Task TryLogin()
  {
    _loginFailed = false;
    try 
    {
      await ((JwtAuthStateProvider)Authentication).TryLoginAsync(_model);
      Navigation.NavigateTo("/");
    }
    catch
    {
      _loginFailed = true;
    }
  }
}