Skip to content

Commit

Permalink
fix register/login buttons (validate prevented invoking them)
Browse files Browse the repository at this point in the history
  • Loading branch information
chucker committed Jun 22, 2023
1 parent c566889 commit fe14bc1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 66 deletions.
115 changes: 57 additions & 58 deletions PubNet.Frontend/Shared/LoginForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,86 @@

@if (Error is not null)
{
<Alert Color="Color.Danger" Visible>
<strong>That didn't work.</strong> @Error
</Alert>
<Alert Color="Color.Danger" Visible>
<strong>That didn't work.</strong> @Error
</Alert>
}

@if (Success is not null)
{
<Alert Color="Color.Success" Visible>
<strong>Success!</strong> @Success
</Alert>
<Alert Color="Color.Success" Visible>
<strong>Success!</strong> @Success
</Alert>
}

<Form @onsubmit="Submit">
<Validations @ref="_validations" Mode="ValidationMode.Manual" Model="LoginRequest">
<Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2">E-Mail</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is10">
<TextEdit Placeholder="Your e-mail address" Role="@TextRole.Email" @bind-Text="@LoginRequest.Email" Autocomplete="email">
<Feedback>
<ValidationError />
</Feedback>
</TextEdit>
</FieldBody>
</Field>
</Validation>
<Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2">Password</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is10">
<TextEdit Placeholder="Your password" Role="@TextRole.Password" @bind-Text="@LoginRequest.Password" Autocomplete="current-password">
<Feedback>
<ValidationError />
</Feedback>
</TextEdit>
</FieldBody>
</Field>
</Validation>
<Validations @ref="_validations" Mode="ValidationMode.Manual" Model="LoginRequest">
<Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2" />
<FieldLabel ColumnSize="ColumnSize.Is2">E-Mail</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is10">
<Button Color="Color.Primary" Clicked="@Submit">Login</Button>
<TextEdit Placeholder="Your e-mail address" Role="@TextRole.Email" @bind-Text="@LoginRequest.Email" Autocomplete="email">
<Feedback>
<ValidationError />
</Feedback>
</TextEdit>
</FieldBody>
</Field>
</Validation>
</Validations>
<Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2">Password</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is10">
<TextEdit Placeholder="Your password" Role="@TextRole.Password" @bind-Text="@LoginRequest.Password" Autocomplete="current-password">
<Feedback>
<ValidationError />
</Feedback>
</TextEdit>
</FieldBody>
</Field>
</Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2" />
<FieldBody ColumnSize="ColumnSize.Is10">
<Button Color="Color.Primary" Clicked="@Submit">Login</Button>
</FieldBody>
</Field>
</Validations>
</Form>

@code {
private Validations? _validations;
private Validations? _validations;

private LoginRequest LoginRequest { get; set; } = new();
private LoginRequest LoginRequest { get; set; } = new();

private string? Error { get; set; }
private string? Success { get; set; }
private string? Error { get; set; }
private string? Success { get; set; }

private async Task Submit() {
if (_validations is null || !await _validations.ValidateAll()) return;
private async Task Submit()
{
if (_validations is null || !await _validations.ValidateAll()) return;

Error = null;
Success = null;
Error = null;
Success = null;

var response = await ApiClient.PostAsync("authentication/login", LoginRequest);
if (response.IsSuccessStatusCode)
{
var token = await response.Content.ReadFromJsonAsync<JwtTokenResponse>();
if (token is null)
return;
var response = await ApiClient.PostAsync("authentication/login", LoginRequest);
if (response.IsSuccessStatusCode)
{
var token = await response.Content.ReadFromJsonAsync<JwtTokenResponse>();
if (token is null)
return;

await Auth.StoreTokenAsync(token.Token);
await Auth.GetSelfAsync();
await Auth.StoreTokenAsync(token.Token);
await Auth.GetSelfAsync();

Nav.NavigateTo("/?message=welcome-back", true);
}
else
{
var message = await response.Content.ReadFromJsonAsync<ErrorResponse>();
Nav.NavigateTo("/?message=welcome-back", true);
}
else
{
var message = await response.Content.ReadFromJsonAsync<ErrorResponse>();

Error = message?.Error?.Message;
}
}
Error = message?.Error?.Message;
}
}

}
14 changes: 6 additions & 8 deletions PubNet.Frontend/Shared/RegisterForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@
</FieldBody>
</Field>
</Validation>
<Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2" />
<FieldBody ColumnSize="ColumnSize.Is10">
<Button Color="Color.Primary" Clicked="@Submit">Register</Button>
</FieldBody>
</Field>
</Validation>
<Field Horizontal>
<FieldLabel ColumnSize="ColumnSize.Is2" />
<FieldBody ColumnSize="ColumnSize.Is10">
<Button Color="Color.Primary" Clicked="@Submit">Register</Button>
</FieldBody>
</Field>
</Validations>
</Form>

Expand Down

0 comments on commit fe14bc1

Please sign in to comment.