login.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Created by apple on 2017/10/31.
  3. */
  4. $( document ).ready(function() {
  5. // DOM ready
  6. // Test data
  7. /*
  8. * To test the script you should discomment the function
  9. * testLocalStorageData and refresh the page. The function
  10. * will load some test data and the loadProfile
  11. * will do the changes in the UI
  12. */
  13. // testLocalStorageData();
  14. // Load profile if it exits
  15. loadProfile();
  16. });
  17. /**
  18. * Function that gets the data of the profile in case
  19. * thar it has already saved in localstorage. Only the
  20. * UI will be update in case that all data is available
  21. *
  22. * A not existing key in localstorage return null
  23. *
  24. */
  25. function getLocalProfile(callback){
  26. var profileImgSrc = localStorage.getItem("PROFILE_IMG_SRC");
  27. var profileName = localStorage.getItem("PROFILE_NAME");
  28. var profileReAuthEmail = localStorage.getItem("PROFILE_REAUTH_EMAIL");
  29. if(profileName !== null
  30. && profileReAuthEmail !== null
  31. && profileImgSrc !== null) {
  32. callback(profileImgSrc, profileName, profileReAuthEmail);
  33. }
  34. }
  35. /**
  36. * Main function that load the profile if exists
  37. * in localstorage
  38. */
  39. function loadProfile() {
  40. if(!supportsHTML5Storage()) { return false; }
  41. // we have to provide to the callback the basic
  42. // information to set the profile
  43. getLocalProfile(function(profileImgSrc, profileName, profileReAuthEmail) {
  44. //changes in the UI
  45. $("#profile-img").attr("src",profileImgSrc);
  46. $("#profile-name").html(profileName);
  47. $("#reauth-email").html(profileReAuthEmail);
  48. $("#inputEmail").hide();
  49. $("#remember").hide();
  50. });
  51. }
  52. /**
  53. * function that checks if the browser supports HTML5
  54. * local storage
  55. *
  56. * @returns {boolean}
  57. */
  58. function supportsHTML5Storage() {
  59. try {
  60. return 'localStorage' in window && window['localStorage'] !== null;
  61. } catch (e) {
  62. return false;
  63. }
  64. }
  65. /**
  66. * Test data. This data will be safe by the web app
  67. * in the first successful login of a auth user.
  68. * To Test the scripts, delete the localstorage data
  69. * and comment this call.
  70. *
  71. * @returns {boolean}
  72. */
  73. function testLocalStorageData() {
  74. if(!supportsHTML5Storage()) { return false; }
  75. localStorage.setItem("PROFILE_IMG_SRC", "//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" );
  76. localStorage.setItem("PROFILE_NAME", "César Izquierdo Tello");
  77. localStorage.setItem("PROFILE_REAUTH_EMAIL", "oneaccount@gmail.com");
  78. }