oauth-2.0single-sign-ongoogle-apps-for-education

Set default domain when using "Sign In With Google"


Using the instructions for Google sign in located here, We are implementing the "Sign in with Google" button.

This implementation has been successful, however, we want to set a default domain other than gmail.com as this will be used by our Gsuite for education users. This would make it so that users can click the sign in button and just type their username, instead of username@gSuiteDomain.com

When this button is clicked:

Log In With Google Button

The user is redirected to: enter image description here

How do we make it so the second image defaults to our gSuite domain instead of gmail.com?


Solution

  • You can't use the simple directions at the location I linked in the question, as it is only for a basic minimum authentication. For customization, you need to access the full client API here (server side validation can stay the same.)

    This allows you to pass information to the init functions:

    <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
    

    Then:

    window.onLoadCallback = function () {
    gapi.load('auth2', initSigninV2);
    
    function initSigninV2() {
        gapi.auth2.init({
            client_id: 'Your_client_id',
            hosted_domain: "YourHostedDomain.com"
        }).then(function (authInstance) {
            if (authInstance != null && authInstance != undefined) {
                gapi.signin2.render("IDOfSignInButton", { onsuccess: onSignIn });
            }
        });
    }
    

    The rest is the same as in the original tutorial