Block @gmail.com users from logging in or signing up with Google Login

Problem Statement

We want to enable Google Social Login only for users with a specific domain, such as example.user@test.com, and block “@gmail.com” users from signing up or logging in to the application.

Solution

You can create an action that validates what is the user’s email domain. If it matches ‘gmail.com’, you can deny access to that user.

To achieve this behavior, you need to create a Post Login Action.

const onExecutePostLogin = async (event, api) => {
    var userEmailDomain = event.user.email;
    userEmailDomain = userEmailDomain.split("@")[1];

    if (userEmailDomain == 'gmail.com'){
        return api.access.deny('You are not allowed to access this resource');
    }
};
exports.onExecutePostLogin = onExecutePostLogin;

References: