Skip to content

Commit b298d9d

Browse files
committed
bugfixes
1 parent 34d38ff commit b298d9d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Endpoints/AuthEndpoints.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,27 @@ async Task<IResult> (string returnUrl, HttpContext context, UserManager<User> us
319319
user = newUser;
320320
}
321321

322-
var loginInfo = new UserLoginInfo("Google",
323-
principal.FindFirstValue(ClaimTypes.Email) ?? string.Empty, "Google");
324-
var loginResult = await userManager.AddLoginAsync(user, loginInfo);
325-
if (!loginResult.Succeeded)
322+
var providerKey = principal.FindFirstValue(ClaimTypes.Email) ?? string.Empty;
323+
var loginInfo = new UserLoginInfo("Google", providerKey, "Google");
324+
325+
// Fetch the user's existing external logins from the database
326+
var existingLogins = await userManager.GetLoginsAsync(user);
327+
328+
// Check if the Google login is already linked
329+
var isAlreadyLinked = existingLogins.Any(l =>
330+
l.LoginProvider == loginInfo.LoginProvider &&
331+
l.ProviderKey == loginInfo.ProviderKey);
332+
333+
// Only add the login if it hasn't been linked yet
334+
if (!isAlreadyLinked)
326335
{
327-
return TypedResults.Json(ApiResponse<object>.Fail("Failed to add login",
328-
loginResult.Errors.Select(e => e.Description).ToList()),
329-
statusCode: StatusCodes.Status400BadRequest);
336+
var loginResult = await userManager.AddLoginAsync(user, loginInfo);
337+
if (!loginResult.Succeeded)
338+
{
339+
return TypedResults.Json(data: ApiResponse<object>.Fail(message: "Failed to add login",
340+
loginResult.Errors.Select(e => e.Description).ToList()),
341+
statusCode: StatusCodes.Status400BadRequest);
342+
}
330343
}
331344

332345
await signInManager.SignInAsync(user, true);

0 commit comments

Comments
 (0)