function renderSignInGoogleButton(width = 300) { gapi.load('auth2'); signOutGoogle(function() { gapi.signin2.render('signin-google', { 'scope': 'profile email', 'width': width, 'height': 50, 'longtitle': true, 'theme': 'dark', 'onsuccess': onSignInGoogle, 'onfailure': onSignInGoogleFailure }); }); } function onSignInGoogle(googleUser) { var id_token = googleUser.getAuthResponse().id_token; $.ajax({ type: 'POST', url: '@controllers.gui.routes.Authentication.signInWithGoogle', data: id_token, contentType: "text/plain; charset=UTF-8", success: function(json) { window.location.href = '@controllers.gui.routes.Home.home'; }, error: function(error) { signOutGoogle(); onSignInGoogleFailure(error.responseText); } }); } function onSignInGoogleFailure(error) { clearLoginAlerts(); $('#signin-google').parent().append('

' + $($.parseHTML(error)).text() + '

'); } function signOutGoogle(callback = function() {}) { gapi.load('auth2', function() { gapi.auth2.init().then(function() { var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function() { auth2.disconnect(); callback(); }); }, callback); }); } function clearLoginAlerts() { $('#signin-google').parent().find(".text-success, .text-warning, .text-danger").remove(); }