Skip to content

Instantly share code, notes, and snippets.

@shyamgupta
Last active September 13, 2019 19:37
Show Gist options
  • Save shyamgupta/d8ba035403e8165510585b805cf64ee6 to your computer and use it in GitHub Desktop.
Save shyamgupta/d8ba035403e8165510585b805cf64ee6 to your computer and use it in GitHub Desktop.
Google Signin
<!-- Google+ API was shutdown on 3/7/2019. New OAuthFlow (listed below) uses the updated Google sign-in. -->
<!--You just need to update this in login.html page for Udacity's Full Stack nanodegree -->
<!-- Below is the Google link I used for reference -->
<!-- https://developers.google.com/identity/sign-in/web/server-side-flow -->
**login.html Page**
(1) Include the following in <head>..</head> tag:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'YOUR CLIENT ID HERE'
});
});
}
</script>
(2) In the <body> tag, add the below button and div
<button id="signinButton">Google Signin</button>
<div id="result"></div>
(3) Add the below script before the ending </body> tag
<script>
$('#signinButton').click(function() {
function signInCallback(authResult){
if (authResult['code']){
$('#signinButton').attr('style', 'display: none');
$.ajax({
type: 'POST',
url: '/gconnect?state={{STATE}}',
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/octet-stream; charset=utf-8',
success:function(result){
$('#result').html('Login Successful!</br>'+ result + '</br>Redirecting...')
setTimeout(function() {
window.location.href = "/";
}, 2000);
},
processData:false,
data:authResult['code']
});
} else{
// handle error
console.log('There was an error: ' + authResult['error']);
$('#result').html('Failed to make a server-side call. Check your configuration and console.');
}
}
auth2.grantOfflineAccess().then(signInCallback);
});
</script>
@UddeshJain
Copy link

Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment