From fc0c94306b2a4bb591125865688e6fc301697a28 Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Fri, 24 May 2024 17:00:25 -0400 Subject: [PATCH] Fix: Sign-in user if exists --- src/Infrastructure/Services/UserService.cs | 4 ++++ src/Web/Controllers/GoogleController.cs | 14 ++++++++++---- src/Web/Program.cs | 8 ++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/Services/UserService.cs b/src/Infrastructure/Services/UserService.cs index ebb5695..8610844 100644 --- a/src/Infrastructure/Services/UserService.cs +++ b/src/Infrastructure/Services/UserService.cs @@ -84,8 +84,12 @@ public class UserService(UserManager userManager, IHttpContextA } } +// If we need to add special characters we can alternate between 2 pools. public class RandomGenerator { + // For the moment, numbers and special characters don't work because + // the random generator is designed to handle a single integer. + // We can modify this in the future. private const string LetterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "!@#$%^&*()_+" diff --git a/src/Web/Controllers/GoogleController.cs b/src/Web/Controllers/GoogleController.cs index 173413a..236f130 100644 --- a/src/Web/Controllers/GoogleController.cs +++ b/src/Web/Controllers/GoogleController.cs @@ -39,16 +39,22 @@ public class GoogleController( FamilyName = claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value }; - await userService.CreateUserAsync(userInfo); // TODO: Don't create user if already exists - - await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(new List + var claimsIdentity = new ClaimsIdentity(new List { new(ClaimTypes.Name, userInfo.Name), new(ClaimTypes.Email, userInfo.Email), new(ClaimTypes.GivenName, userInfo.GivenName), new(ClaimTypes.Surname, userInfo.FamilyName) - }, CookieAuthenticationDefaults.AuthenticationScheme))); + }, CookieAuthenticationDefaults.AuthenticationScheme); + if (await userService.FindUserByEmailAsync(userInfo.Email) != null) // TODO: Do we need to check for null ? + { + await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); + return Redirect("/"); + } + + await userService.CreateUserAsync(userInfo); + await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return Redirect("/"); } } diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 5407839..c49790c 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -72,11 +72,11 @@ builder.Services.AddAuthentication(options => // Password hashing builder.Services.AddIdentity(options => { - options.Password.RequireDigit = true; + options.Password.RequireDigit = false; options.Password.RequireLowercase = false; - options.Password.RequireUppercase = true; - options.Password.RequireNonAlphanumeric = true; - options.Password.RequiredLength = 8; + options.Password.RequireUppercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequiredLength = 16; }) .AddEntityFrameworkStores() .AddDefaultTokenProviders();