Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GoogleAuthProviderHandler
global class GoogleAuthProviderHandler implements Auth.RegistrationHandler
{
global boolean canCreateUser(Auth.UserData data)
{
// if no email is provided, we can't match with a Salesforce User
if (!String.isBlank(data.email))
{
return true;
}
return false;
}
global User createUser(Id portalId, Auth.UserData data)
{
if (!canCreateUser(data))
{
// Returning null or throwing an exception fails the SSO flow
return null;
}
return getUserForAuthUserData(data);
}
global void updateUser(Id userId, Id portalId, Auth.UserData data)
{
}
private User getUserForAuthUserData(Auth.UserData data)
{
//Simple use case here, but might be a more complex check in the future
return getUserByEmail(data.email);
}
private User getUserByEmail(String email)
{
User user;
List<User> userList = [SELECT Id FROM User WHERE Email = :email];
if(!userList.isEmpty())
{
user = userList[0];
}
return user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.