Skip to content

Commit

Permalink
Merge pull request #363 from ITU-BDSA2024-GROUP10/Login-after-email-c…
Browse files Browse the repository at this point in the history
…onfirmed

Login after email confirmed
  • Loading branch information
math045b authored Dec 17, 2024
2 parents a998091 + f7bb12e commit dbbaa78
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page
@model ConfirmEmailModel
@{
ViewData["Title"] = "Confirm email";
}

<h1>@ViewData["Title"]</h1>
<partial name="_StatusMessage" model="Model.StatusMessage" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chirp.Infrastructure.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;

namespace Chirp.Web.Areas.Identity.Pages.Account
{
public class ConfirmEmailModel : PageModel
{
private readonly UserManager<Author> _userManager;

private readonly SignInManager<Author> _signInManager;

public ConfirmEmailModel(UserManager<Author> userManager, SignInManager<Author> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}

/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[TempData]
public string StatusMessage { get; set; }
public async Task<IActionResult> OnGetAsync(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToPage("/Index");
}

var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}

code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
var result = await _userManager.ConfirmEmailAsync(user, code);
// StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";

if(result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect("~/");
}

StatusMessage = "Error confirming your email.";

return Page();
}
}
}
3 changes: 0 additions & 3 deletions Chirp/test/PlaywrightTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public async Task EndToEnd_RegisterLoginCheepAndLogout()

await Page.GotoAsync("/");
await Register(author, password);
await Expect(Page.GetByText("Thank you for confirming")).ToBeVisibleAsync();

await RazorPageUtils.Login(author.UserName!, password);

await Page.GetByRole(AriaRole.Link, new() { Name = "my timeline" }).ClickAsync();
await Page.Locator("#Message").ClickAsync();
Expand Down
6 changes: 4 additions & 2 deletions Chirp/test/PlaywrightTests/ExternalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public async Task CreateUserUsingExternalProvider_OnlyUsername()
await Page.GetByPlaceholder("Please enter your email.").FillAsync("Test@withUsername.com");
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your" }).ClickAsync();
await Expect(Page.GetByText("Thank you for confirming your")).ToBeVisibleAsync();

await logout();

await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [mr. Test with username]");
Expand All @@ -89,8 +90,9 @@ public async Task CreateUserUsingExternalProvider_NoInfo()
await Page.GetByPlaceholder("Please enter your username").FillAsync("mr. test with no info");
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your" }).ClickAsync();
await Expect(Page.GetByText("Thank you for confirming your")).ToBeVisibleAsync();

await logout();

await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [mr. test with no info]");
}
Expand Down

0 comments on commit dbbaa78

Please sign in to comment.