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

@@ -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<Claim>
var claimsIdentity = new ClaimsIdentity(new List<Claim>
{
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("/");
}
}

View File

@@ -72,11 +72,11 @@ builder.Services.AddAuthentication(options =>
// Password hashing
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(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<ApplicationDbContext>()
.AddDefaultTokenProviders();