Linking account from custom User Storage SPI with social login provider

Hi, I’m trying to write a custom User Storage API in order to replace a legacy service. In the legacy service we have information regarding how a user logs in (e.g. password, google, facebook, etc.).

As described in the documentation if the custom User Storage SPI returns a result then the user trying to log in gets a message “User with email {email} already exists. How do you want to continue?”.

I’m to add a federated identity in the getUserByEmail method of my provider in order to allow the existing user to authenticate with the social provider (in this case Google). I’m using the following code to link the Google account:

// ... getting the user from the legacy service first

session.userFederatedStorage().removeFederatedIdentity(
    realm, userModel.getId(), "google");
FederatedIdentityModel identityModel = new FederatedIdentityModel(
    "google", "<The google ID of the user>", userModel.getEmail());
session.userFederatedStorage().addFederatedIdentity(
    realm, userModel.getId(), identityModel);

return userModel

However, the code above doesn’t have any effect and I’m still getting the error message saying the account exists. A second issue is that I’m not sure how to get the Google User ID (currently for testing I’m just hard-coding it).

I would be really grateful for any help!