Skip to content

Instantly share code, notes, and snippets.

@Cristianasp
Forked from shyamgupta/login.html
Created April 8, 2019 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cristianasp/126f93d31b93d3c5e3baadb6a0929cf3 to your computer and use it in GitHub Desktop.
Save Cristianasp/126f93d31b93d3c5e3baadb6a0929cf3 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment