Last active
February 28, 2019 08:42
-
-
Save kohendrix/72e24e14f5e64d10eeb5ea238c0e0f00 to your computer and use it in GitHub Desktop.
Google Sign-in Express + pjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title><%= title %></title> | |
| <link rel="stylesheet" href="/stylesheets/style.css" /> | |
| <script src="https://apis.google.com/js/platform.js" async defer></script> | |
| <meta name="google-signin-client_id" content="<%= clientIdUrl %>" /> | |
| <script> | |
| // called by google client | |
| function onSignIn(googleUser) { | |
| var id_token = googleUser.getAuthResponse().id_token; | |
| // never log these out on a real app | |
| console.log('id_token: ', id_token); | |
| var profile = googleUser.getBasicProfile(); | |
| console.log('ID: ' + profile.getId()); | |
| console.log('Name: ' + profile.getName()); | |
| console.log('Image URL: ' + profile.getImageUrl()); | |
| console.log('Email: ' + profile.getEmail()); | |
| // verification | |
| var xhr = new XMLHttpRequest(); | |
| xhr.open('POST', '/tokensignin'); | |
| xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
| xhr.onload = function() { | |
| console.log('Server returned: ' + xhr.responseText); | |
| }; | |
| xhr.send('idtoken=' + id_token); | |
| } | |
| // on sign out | |
| function signOut() { | |
| var auth2 = gapi.auth2.getAuthInstance(); | |
| auth2.signOut().then(function() { | |
| console.log('User signed out.'); | |
| }); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <h1><%= title %></h1> | |
| <p>Welcome</p> | |
| <div class="g-signin2" data-onsuccess="onSignIn"></div> | |
| <br /> | |
| <a href="/" onclick="signOut();">SignOut</a> | |
| <br /> | |
| <a href="<%= googleUrl %>" class="pop-button">SignIn Using Redirect</a> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment