Advertisement
Guest User

OAuth HTML+JS example

a guest
May 22nd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <head>
  2. <meta name="google-signin-scope" content="profile email">
  3. <meta name="google-signin-client_id" content="#HgetClientId()#">
  4. <script src="https://apis.google.com/js/platform.js" async defer></script>
  5.  
  6. ...
  7. <body>
  8. Please log in with your Google account to access content! If you found this page accidentally then most likely you have no access rights on this page.
  9. <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
  10. <script>
  11. function send(FD)
  12. {
  13. var xhr = new XMLHttpRequest();
  14. xhr.qpage=this;
  15. xhr.responseType = "text";
  16. xhr.onreadystatechange = function() {
  17. if (this.readyState == 4) {
  18. if(this.status == 200)
  19. {
  20. var page=this.qpage;
  21. eval(this.responseText);
  22. }
  23. else
  24. {
  25. this.qpage.dispose("Server communication XHR fault. Status code: "+this.status);
  26. }
  27. }
  28. }.bind(xhr);
  29. xhr.open("POST",'?login=true');
  30. xhr.send(FD);
  31. }
  32. function onSignIn(googleUser) {
  33. var fd = new FormData();
  34.  
  35. var profile = googleUser.getBasicProfile();
  36. console.log("Google API login attempt: ID: " + profile.getId());
  37. console.log("ID: " + profile.getId());
  38. console.log('Full Name: ' + profile.getName());
  39. console.log('Given Name: ' + profile.getGivenName());
  40. console.log('Family Name: ' + profile.getFamilyName());
  41. console.log("Image URL: " + profile.getImageUrl());
  42. console.log("Email: " + profile.getEmail());
  43. fd.append("id_token", googleUser.getAuthResponse().id_token);
  44. var id_token = googleUser.getAuthResponse().id_token;
  45. console.log("ID Token: " + id_token);
  46. send(fd);
  47. };
  48. </script>
  49. <a href="#" onclick="signOut();">Sign out</a><br/>
  50. <a href="#HFotok.clargs.contextPath#/listing/">enter</a>
  51. <script>
  52. function signOut() {
  53. var fd=new FormData();
  54. fd.append("signout", "true");
  55. send(fd);
  56. var auth2 = gapi.auth2.getAuthInstance();
  57. auth2.signOut().then(function () {
  58. });
  59. }
  60. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement