Skip to content

Instantly share code, notes, and snippets.

@lucianmachado
Created September 13, 2018 13:12
Show Gist options
  • Save lucianmachado/54f9866ec6a261cccb250cca6a84a978 to your computer and use it in GitHub Desktop.
Save lucianmachado/54f9866ec6a261cccb250cca6a84a978 to your computer and use it in GitHub Desktop.
Google Sign in snippet
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<div style="padding-top:20px" ><a href="#" onclick="signOut();">Sign out</a></div>
<div id="user-informations" style="padding-top:20px"></div>
<img src="" alt="">
<script charset="utf-8">
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log(profile)
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
console.log('Granted Scopes: ' + googleUser.getGrantedScopes())
console.log('AuthResponse: ' + JSON.stringify(googleUser.getAuthResponse().id_token))
var div = document.getElementById('user-informations');
div.innerHTML += '<h1>User informations:</h1>'
div.innerHTML += '<img src="'+ profile.getImageUrl() +'" alt="">'
div.innerHTML += '<br>'
div.innerHTML += 'Name: ' + profile.getName()
div.innerHTML += '<br>'
div.innerHTML += 'Email: ' + profile.getEmail()
div.innerHTML += '<br>'
div.innerHTML += JSON.stringify(profile)
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="CLIENT-ID">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment