add Google login

This commit is contained in:
2023-12-27 16:30:16 +01:00
parent ea5bdd7f1e
commit 0c90e19895
5 changed files with 53 additions and 12 deletions
@@ -123,20 +123,42 @@ namespace Kemkas.Web.Areas.Identity.Pages.Account
{
return RedirectToPage("./Lockout");
}
else
// If the user does not have an account, and no email claim, then ask the user to create an account.
ReturnUrl = returnUrl;
ProviderDisplayName = info.ProviderDisplayName;
if (!info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
{
// If the user does not have an account, then ask the user to create an account.
ReturnUrl = returnUrl;
ProviderDisplayName = info.ProviderDisplayName;
if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
{
Input = new InputModel
{
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
};
}
return Page();
}
// User doesn't have account but external provider has email claim. => Register with claimed email.
Input = new InputModel
{
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
};
var user = CreateUser();
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
var registrationResult = await _userManager.CreateAsync(user);
if (registrationResult.Succeeded)
{
registrationResult = await _userManager.AddLoginAsync(user, info);
if (registrationResult.Succeeded)
{
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
await _userManager.ConfirmEmailAsync(user, code);
await _signInManager.SignInAsync(user, isPersistent: false, info.LoginProvider);
return LocalRedirect(returnUrl);
}
}
foreach (var error in registrationResult.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return Page();
}
public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null)