Skip to content

Commit

Permalink
VCST-1705: Validation error displayed after Google SSO when account d…
Browse files Browse the repository at this point in the history
…ata is incomplete (#7)

VCST-1705: Validation error displayed after Google SSO when account data is incomplete

fix: Validation error displayed after Google SSO when account data is incomplete.
  • Loading branch information
OlegoO authored Aug 26, 2024
1 parent 5f1ceef commit dfd067a
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/VirtoCommerce.Xapi.Data/Services/ExternalSignInUserBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
Expand All @@ -14,15 +15,16 @@ namespace VirtoCommerce.Xapi.Data.Services;

public class ExternalSignInUserBuilder(IStoreService storeService, IMemberService memberService) : IExternalSignInUserBuilder
{
public async Task BuildNewUser(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
public virtual async Task BuildNewUser(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
{
if (user.MemberId is null && user.UserType == UserType.Customer.ToString())
{
var contact = AbstractTypeFactory<Contact>.TryCreateInstance();
contact.Name = externalLoginInfo.Principal.FindFirstValue("name");
contact.FirstName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName);
contact.LastName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname);
contact.Emails = [user.Email];

contact.Name = ResolveContactName(user, externalLoginInfo);
contact.FirstName = ResolveContactFirstName(user, externalLoginInfo);
contact.LastName = ResolveContactLastName(user, externalLoginInfo);
contact.Emails = ResolveContactEmails(user, externalLoginInfo);

if (!string.IsNullOrEmpty(user.StoreId))
{
Expand All @@ -35,4 +37,24 @@ public async Task BuildNewUser(ApplicationUser user, ExternalLoginInfo externalL
user.MemberId = contact.Id;
}
}

protected virtual string ResolveContactName(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
{
return externalLoginInfo.Principal.FindFirstValue("name");
}

protected virtual string ResolveContactFirstName(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
{
return externalLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName) ?? string.Empty;
}

protected virtual string ResolveContactLastName(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
{
return externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname) ?? string.Empty;
}

protected virtual IList<string> ResolveContactEmails(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
{
return [user.Email];
}
}

0 comments on commit dfd067a

Please sign in to comment.