Auto-link verified Google sign-ins by email

This commit is contained in:
2026-03-28 14:24:10 +01:00
parent 9f949ee9df
commit 19a4da9382
3 changed files with 110 additions and 1 deletions
@@ -135,6 +135,15 @@ public sealed class AuthController : ControllerBase
x => x.GoogleSubject == google.Subject || (!string.IsNullOrWhiteSpace(google.Email) && x.GoogleEmail == google.Email),
cancellationToken);
if (user is null && google.EmailVerified && !string.IsNullOrWhiteSpace(google.Email))
{
user = await _users.FindByEmailAsync(google.Email);
if (user is not null)
{
_logger.LogInformation("Auto-linking Google sign-in for existing local account {Email}", google.Email);
}
}
if (user is null)
{
return Unauthorized("This Google account is not linked to a Jobbjakt user yet.");
@@ -145,6 +154,9 @@ public sealed class AuthController : ControllerBase
user.GoogleSubject = google.Subject;
user.GoogleEmail = google.Email;
user.GoogleLinkedAt ??= DateTimeOffset.UtcNow;
user.DisplayName ??= TrimOrNull(google.Name);
user.FirstName ??= TrimOrNull(google.GivenName);
user.LastName ??= TrimOrNull(google.FamilyName);
await _users.UpdateAsync(user);
}