Skip to content

Instantly share code, notes, and snippets.

@erickoledadevrel
Created April 22, 2016 18:42
Show Gist options
  • Select an option

  • Save erickoledadevrel/ffefb8f0eeedc705f954245fd40b1d66 to your computer and use it in GitHub Desktop.

Select an option

Save erickoledadevrel/ffefb8f0eeedc705f954245fd40b1d66 to your computer and use it in GitHub Desktop.

Revisions

  1. Eric Koleda created this gist Apr 22, 2016.
    84 changes: 84 additions & 0 deletions AppsMarketplaceSignIn.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    <html>
    <head>
    <title>GAM Sample App</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.17.1/URI.js"></script>
    <script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>
    </head>
    <body>
    <h1>GAM Sample App</h1>
    <p id="message">Loading...</p>

    <script type="text/javascript">
    // A "Web application" client ID from the same Google Developers Console
    // project that contains your Google Apps Marketplace configuration.
    var CLIENT_ID = '...';

    /**
    * Initializes the Google Sign-in library. Automatically called when the
    * JavaScript library is done loading.
    */
    function init() {
    // Read the user's domain from the URL. This requires that you add
    // "?domain=${DOMAIN_NAME}" to your Universal Navigation Extension URL
    // in the Google Apps Marketplace configuration.
    var url = URI(location.href);
    var params = url.search(true);
    var domain = params['domain'];

    // Load the auth library.
    gapi.load('auth2', function() {
    // Initialize the auth library.
    var auth = gapi.auth2.init({
    client_id: CLIENT_ID,
    // Specify the domain of the user, in case they are logged into
    // multiple Google accounts.
    hosted_domain: params['domain'],
    // Basic profile information is required for the library to validate
    // the domain of the user. This requires that the scope
    // "https://www.googleapis.com/auth/plus.me" is added to your
    // Google Apps Marketplace configuration, even if you don't need
    // in your application.
    fetch_basic_profile: true
    });

    // Add the listener for changes to signed-in status.
    auth.isSignedIn.listen(onSignedInChange);

    // Once the auth library has fully initialized, sign-in the user if they
    // aren't already.
    auth.then(function() {
    var auth = gapi.auth2.getAuthInstance();
    var user = auth.currentUser.get();
    if (!user.isSignedIn()) {
    auth.signIn().then(null, function(error) {
    // Something went wrong during sign-in.
    alert('Unable to sign-in: ' + error);
    });
    }
    });
    });
    }

    /**
    * Callback for whenever the signed-in status of the user changes.
    */
    function onSignedInChange(isSignedIn) {
    var auth = gapi.auth2.getAuthInstance();
    var message;
    if (isSignedIn) {
    var user = auth.currentUser.get();
    var profile = user.getBasicProfile();
    document.querySelector('#message').innerText =
    'Welcome ' + profile.getName();

    // To sign-in the user to your backend, retrieve the ID token, send it
    // to your server, have the server verify it (https://goo.gl/WB5SLP),
    // and use the subject field ("sub") as the user's unique, stable ID in
    // your system.
    var idToken = user.getAuthResponse().id_token;
    // TODO: sendToServer(idToken);
    }
    }
    </script>
    </body>
    </html>