Skip to content

Commit 0939e20

Browse files
committed
improve login, register and delete flows
1 parent 4173223 commit 0939e20

File tree

12 files changed

+119
-154
lines changed

12 files changed

+119
-154
lines changed

src/web/Jordnaer/Components/Account/IdentityComponentsEndpointRouteBuilderExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public static IEndpointConventionBuilder MapAdditionalIdentityEndpoints(this IEn
4646
[FromForm] string returnUrl) =>
4747
{
4848
await signInManager.SignOutAsync();
49-
return TypedResults.LocalRedirect($"~/{returnUrl}");
49+
return TypedResults.LocalRedirect(string.IsNullOrEmpty(returnUrl)
50+
? "~/"
51+
: returnUrl);
5052
});
5153

5254
var manageGroup = accountGroup.MapGroup("/Manage").RequireAuthorization();

src/web/Jordnaer/Components/Account/IdentityRedirectManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage
1717
};
1818

1919
[DoesNotReturn]
20-
public void RedirectTo(string? uri)
20+
public void RedirectTo(string? uri, bool forceLoad = false)
2121
{
2222
uri ??= "";
2323

@@ -29,7 +29,7 @@ public void RedirectTo(string? uri)
2929

3030
// During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect.
3131
// So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown.
32-
navigationManager.NavigateTo(uri);
32+
navigationManager.NavigateTo(uri, forceLoad);
3333
throw new InvalidOperationException($"{nameof(IdentityRedirectManager)} can only be used during static rendering.");
3434
}
3535

@@ -60,7 +60,7 @@ public void RedirectToWithStatus(string uri, AlertMessage? message, HttpContext
6060
private string CurrentPath => navigationManager.ToAbsoluteUri(navigationManager.Uri).GetLeftPart(UriPartial.Path);
6161

6262
[DoesNotReturn]
63-
public void RedirectToCurrentPage() => RedirectTo(CurrentPath);
63+
public void RedirectToCurrentPage(bool forceLoad = false) => RedirectTo(CurrentPath, forceLoad);
6464

6565
[DoesNotReturn]
6666
public void RedirectToCurrentPageWithStatus(string message, HttpContext context)

src/web/Jordnaer/Components/Account/Pages/ConfirmEmail.razor

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@using System.Text
44
@using Microsoft.AspNetCore.WebUtilities
55

6+
@inject SignInManager<ApplicationUser> SignInManager
67
@inject UserManager<ApplicationUser> UserManager
78
@inject IdentityRedirectManager RedirectManager
89

@@ -23,6 +24,9 @@
2324
[SupplyParameterFromQuery]
2425
private string? Code { get; set; }
2526

27+
[SupplyParameterFromQuery]
28+
private string? ReturnUrl { get; set; }
29+
2630
protected override async Task OnInitializedAsync()
2731
{
2832
if (UserId is null || Code is null)
@@ -34,15 +38,19 @@
3438
if (user is null)
3539
{
3640
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
37-
_statusMessage = new AlertMessage("Fejl ved indlæsning af bruger med ID {UserId}", true);
41+
_statusMessage = new AlertMessage($"Fejl ved indlæsning af bruger med ID {UserId}", true);
3842
}
3943
else
4044
{
4145
var code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(Code));
4246
var result = await UserManager.ConfirmEmailAsync(user, code);
4347
if (result.Succeeded)
4448
{
45-
_statusMessage = new AlertMessage("Tak fordi du bekræftede din email.");
49+
_statusMessage = new AlertMessage("Tak fordi du bekræftede din email, du bliver nu logget ind.");
50+
51+
await SignInManager.SignInAsync(user, isPersistent: true);
52+
53+
RedirectManager.RedirectTo(ReturnUrl ?? "/profile", forceLoad: true);
4654
}
4755
else
4856
{

src/web/Jordnaer/Components/Account/Pages/ExternalLogin.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@inject JordnaerDbContext Context
1515
@inject IMediator Mediator
1616

17-
<MetadataComponent Title="Registrér"/>
17+
<MetadataComponent Title="Registrér" />
1818

1919
<StatusMessage Message="@_message" />
2020
<h1>Registrér</h1>
@@ -150,7 +150,7 @@
150150
var result = await UserManager.CreateAsync(user);
151151
if (!result.Succeeded)
152152
{
153-
_message = new AlertMessage(result.Errors.Select(error => error.Description), true);
153+
_message = new AlertMessage(result.Errors.Select(error => error.Description), true);
154154
return;
155155
}
156156

@@ -175,7 +175,7 @@
175175
// If account confirmation is required, we need to show the link if we don't have a real email sender
176176
if (UserManager.Options.SignIn.RequireConfirmedAccount)
177177
{
178-
RedirectManager.RedirectTo("Account/RegisterConfirmation", new() { ["email"] = Input.Email });
178+
RedirectManager.RedirectTo("Account/RegisterConfirmation", queryParameters: new() { ["email"] = Input.Email });
179179
}
180180

181181
await SignInManager.SignInAsync(user, isPersistent: false, _externalLoginInfo.LoginProvider);

src/web/Jordnaer/Components/Account/Pages/Login.razor

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,45 @@
33
@inject SignInManager<ApplicationUser> SignInManager
44
@inject ILogger<Login> Logger
55
@inject IdentityRedirectManager RedirectManager
6-
@inject ILocalStorageService LocalStorage
76

87
<MudContainer MaxWidth="MaxWidth.Small">
98
<MudPaper Elevation="3" Class="pa-4">
109
<EditForm Model="Input" OnValidSubmit="LoginUser" FormName="login">
10+
1111
<StatusMessage Message="@_errorMessage"/>
12+
13+
<h2 class="font-open-sans-medium" style="@($"color: {JordnaerPalette.RedHeader}")">Log ind</h2>
14+
<hr />
15+
1216
<div class="form-floating mb-3">
13-
<InputText @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="[email protected]" />
17+
<InputText @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="[email protected]"/>
1418
<label for="email" class="form-label">Email</label>
15-
<ValidationMessage For="() => Input.Email" class="text-danger" />
19+
<ValidationMessage For="() => Input.Email" class="text-danger"/>
1620
</div>
21+
1722
<div class="form-floating mb-3">
18-
<InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="current-password" aria-required="true" placeholder="adgangskode" />
23+
<InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="current-password" aria-required="true" placeholder="adgangskode"/>
1924
<label for="password" class="form-label">Adgangskode</label>
20-
<ValidationMessage For="() => Input.Password" class="text-danger" />
21-
</div>
22-
<div class="checkbox mb-3">
23-
<label class="form-label">
24-
<InputCheckbox @bind-Value="Input.RememberMe" class="darker-border-checkbox form-check-input" />
25-
Husk mig
26-
</label>
25+
<ValidationMessage For="() => Input.Password" class="text-danger"/>
2726
</div>
27+
2828
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Info" FullWidth>
2929
Log ind
3030
</MudButton>
31+
3132
<MudDivider DividerType="DividerType.FullWidth" Class="my-3"/>
3233
<MudStack>
3334
<MudLink Href="/Account/ForgotPassword">Glemt din adgangskode?</MudLink>
3435
<MudLink Href="/Account/ResendEmailConfirmation">Gensend emailbekræftelse</MudLink>
35-
<MudButton ButtonType="ButtonType.Button"
36-
Variant="Variant.Filled"
37-
Color="Color.Success"
36+
<MudButton ButtonType="ButtonType.Button"
37+
Variant="Variant.Filled"
38+
Color="Color.Success"
3839
FullWidth
3940
Href="/Account/Register">
4041
Opret ny konto
4142
</MudButton>
4243
</MudStack>
44+
4345
</EditForm>
4446
</MudPaper>
4547
<MudPaper Elevation="3" Class="pa-4 mt-4">
@@ -70,7 +72,10 @@
7072

7173
public async Task LoginUser()
7274
{
73-
var result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
75+
var result = await SignInManager.PasswordSignInAsync(Input.Email,
76+
Input.Password,
77+
isPersistent: true,
78+
lockoutOnFailure: true);
7479
if (result.Succeeded)
7580
{
7681
Logger.LogInformation("User logged in.");
@@ -81,7 +86,7 @@
8186
{
8287
RedirectManager.RedirectTo(
8388
"Account/LoginWith2fa",
84-
new Dictionary<string, object?> { ["returnUrl"] = ReturnUrl, ["rememberMe"] = Input.RememberMe });
89+
new Dictionary<string, object?> { ["returnUrl"] = ReturnUrl, ["rememberMe"] = true });
8590
}
8691
else if (result.IsLockedOut)
8792
{
@@ -104,8 +109,5 @@
104109
[DataType(DataType.Password)]
105110
[Display(Name = "Adgangskode")]
106111
public string Password { get; set; } = "";
107-
108-
[Display(Name = "Husk mig?")]
109-
public bool RememberMe { get; set; }
110112
}
111113
}

src/web/Jordnaer/Components/Account/Pages/Register.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@
3131

3232
<div class="form-floating mb-3">
3333
<InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="" />
34-
<label for="password">Kodeord</label>
34+
<label for="password">Adgangskode</label>
3535
<ValidationMessage For="() => Input.Password" class="text-danger" />
3636
</div>
3737

3838
<div class="form-floating mb-3">
3939
<InputText type="password" @bind-Value="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="" />
40-
<label for="confirm-password">Bekræft kodeord</label>
40+
<label for="confirm-password">Bekræft adgangskode</label>
4141
<ValidationMessage For="() => Input.ConfirmPassword" class="text-danger" />
4242
</div>
4343

4444
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Success" FullWidth>Opret</MudButton>
4545

4646
<div class="d-flex justify-center">
4747
<MudButton Href="/Account/Login" Class="mt-4" ButtonType="ButtonType.Button" Variant="Variant.Text" Color="Color.Info">
48-
Login med eksisterende konto
48+
Log ind med eksisterende konto
4949
</MudButton>
5050
</div>
5151
</EditForm>
@@ -104,7 +104,7 @@
104104
{
105105
RedirectManager.RedirectTo(
106106
"Account/RegisterConfirmation",
107-
new() { ["email"] = Input.Email, ["returnUrl"] = ReturnUrl });
107+
queryParameters: new() { ["email"] = Input.Email, ["returnUrl"] = ReturnUrl });
108108
}
109109

110110
await SignInManager.SignInAsync(user, isPersistent: false);

src/web/Jordnaer/Components/Account/Pages/RegisterConfirmation.razor

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
@page "/Account/RegisterConfirmation"
22

33
@inject UserManager<ApplicationUser> UserManager
4-
@inject IEmailSender<ApplicationUser> EmailSender
5-
@inject NavigationManager NavigationManager
64
@inject IdentityRedirectManager RedirectManager
75

86
<MetadataComponent Title="Registreringsbekræftelse" />
97

10-
<h1>Registreringsbekræftelse</h1>
8+
<h2 class="font-open-sans-light">Registreringsbekræftelse</h2>
119

1210
<StatusMessage Message="@_statusMessage" />
1311

14-
@*TODO: Opdater denne side, når emailen er bekræftet*@
15-
<p>Check venligst din email for at bekræfte din konto.</p>
16-
@*TODO: Allow resending email after 30 seconds in case it never arrives*@
12+
<p>Check venligst din email for at bekræfte din konto. Du kan lukke denne side.</p>
1713

1814
@code {
1915
private AlertMessage? _statusMessage;
@@ -31,7 +27,7 @@
3127
{
3228
if (Email is null)
3329
{
34-
RedirectManager.RedirectTo("");
30+
RedirectManager.RedirectTo(ReturnUrl ?? "");
3531
}
3632

3733
var user = await UserManager.FindByEmailAsync(Email);

src/web/Jordnaer/Consumers/SendEmailConsumer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public async Task Consume(ConsumeContext<SendEmail> consumeContext)
8989
}
9090
else
9191
{
92-
logger.LogInformation("Email sent to {@Recipient}. Subject: {Subject}", message.To, message.Subject);
92+
logger.LogInformation("Email sent to {@Recipient}. Subject: {Subject}",
93+
message.To?.Select(x => x.Email), message.Subject);
9394
}
9495
}
9596
}

src/web/Jordnaer/Features/Authentication/EmailConfirmed.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)