Fix: Sign-in user if exists

This commit is contained in:
Kamigen
2024-05-24 17:00:25 -04:00
parent 4fba9e2a0a
commit fc0c94306b
3 changed files with 18 additions and 8 deletions

View File

@@ -84,8 +84,12 @@ public class UserService(UserManager<ApplicationUser> userManager, IHttpContextA
} }
} }
// If we need to add special characters we can alternate between 2 pools.
public class RandomGenerator 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" private const string LetterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789" + "0123456789"
+ "!@#$%^&*()_+" + "!@#$%^&*()_+"

View File

@@ -39,16 +39,22 @@ public class GoogleController(
FamilyName = claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value FamilyName = claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value
}; };
await userService.CreateUserAsync(userInfo); // TODO: Don't create user if already exists var claimsIdentity = new ClaimsIdentity(new List<Claim>
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
{ {
new(ClaimTypes.Name, userInfo.Name), new(ClaimTypes.Name, userInfo.Name),
new(ClaimTypes.Email, userInfo.Email), new(ClaimTypes.Email, userInfo.Email),
new(ClaimTypes.GivenName, userInfo.GivenName), new(ClaimTypes.GivenName, userInfo.GivenName),
new(ClaimTypes.Surname, userInfo.FamilyName) 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("/"); return Redirect("/");
} }
} }

View File

@@ -72,11 +72,11 @@ builder.Services.AddAuthentication(options =>
// Password hashing // Password hashing
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options => builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{ {
options.Password.RequireDigit = true; options.Password.RequireDigit = false;
options.Password.RequireLowercase = false; options.Password.RequireLowercase = false;
options.Password.RequireUppercase = true; options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = true; options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredLength = 8; options.Password.RequiredLength = 16;
}) })
.AddEntityFrameworkStores<ApplicationDbContext>() .AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); .AddDefaultTokenProviders();