Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta charset=utf-8 />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>FireBase Test</title>
  8.  
  9. // Include firebase credentials here
  10.  
  11. <script type="text/javascript">
  12. /**
  13. * Handles the sign in button press.
  14. */
  15. function toggleSignIn() {
  16. if (firebase.auth().currentUser) {
  17. // [START signout]
  18. firebase.auth().signOut();
  19. // [END signout]
  20. } else {
  21. var email = document.getElementById('email').value;
  22. var password = document.getElementById('password').value;
  23. if (email.length < 4) {
  24. alert('Please enter an email address.');
  25. return;
  26. }
  27. if (password.length < 4) {
  28. alert('Please enter a password.');
  29. return;
  30. }
  31. // Sign in with email and pass.
  32. // [START authwithemail]
  33. firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  34. // Handle Errors here.
  35. var errorCode = error.code;
  36. var errorMessage = error.message;
  37. // [START_EXCLUDE]
  38. if (errorCode === 'auth/wrong-password') {
  39. alert('Wrong password.');
  40. } else {
  41. alert(errorMessage);
  42. }
  43. console.log(error);
  44. document.getElementById('sign-in').disabled = false;
  45. // [END_EXCLUDE]
  46. });
  47. // [END authwithemail]
  48. }
  49. document.getElementById('sign-in').disabled = true;
  50. }
  51.  
  52. /**
  53. * Handles the sign up button press.
  54. */
  55. function handleSignUp() {
  56. var email = document.getElementById('email').value;
  57. var password = document.getElementById('password').value;
  58. if (email.length < 4) {
  59. alert('Please enter an email address.');
  60. return;
  61. }
  62. if (password.length < 4) {
  63. alert('Please enter a password.');
  64. return;
  65. }
  66. // Sign in with email and pass.
  67. // [START createwithemail]
  68. firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  69. // Handle Errors here.
  70. var errorCode = error.code;
  71. var errorMessage = error.message;
  72. // [START_EXCLUDE]
  73. if (errorCode == 'auth/weak-password') {
  74. alert('The password is too weak.');
  75. } else {
  76. alert(errorMessage);
  77. }
  78. console.log(error);
  79. // [END_EXCLUDE]
  80. });
  81. // [END createwithemail]
  82. }
  83.  
  84. /**
  85. * Sends an email verification to the user.
  86. */
  87. function sendEmailVerification() {
  88. // [START sendemailverification]
  89. firebase.auth().currentUser.sendEmailVerification().then(function() {
  90. // Email Verification sent!
  91. // [START_EXCLUDE]
  92. alert('Email Verification Sent!');
  93. // [END_EXCLUDE]
  94. });
  95. // [END sendemailverification]
  96. }
  97.  
  98. function sendPasswordReset() {
  99. var email = document.getElementById('email').value;
  100. // [START sendpasswordemail]
  101. firebase.auth().sendPasswordResetEmail(email).then(function() {
  102. // Password Reset Email Sent!
  103. // [START_EXCLUDE]
  104. alert('Password Reset Email Sent!');
  105. // [END_EXCLUDE]
  106. }).catch(function(error) {
  107. // Handle Errors here.
  108. var errorCode = error.code;
  109. var errorMessage = error.message;
  110. // [START_EXCLUDE]
  111. if (errorCode == 'auth/invalid-email') {
  112. alert(errorMessage);
  113. } else if (errorCode == 'auth/user-not-found') {
  114. alert(errorMessage);
  115. }
  116. console.log(error);
  117. // [END_EXCLUDE]
  118. });
  119. // [END sendpasswordemail];
  120. }
  121.  
  122. /**
  123. * initApp handles setting up UI event listeners and registering Firebase auth listeners:
  124. * - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or
  125. * out, and that is where we update the UI.
  126. */
  127. function initApp() {
  128. // Listening for auth state changes.
  129. // [START authstatelistener]
  130. firebase.auth().onAuthStateChanged(function(user) {
  131. // [START_EXCLUDE silent]
  132. document.getElementById('verify-email').disabled = true;
  133. // [END_EXCLUDE]
  134. if (user) {
  135. // User is signed in.
  136. var displayName = user.displayName;
  137. var email = user.email;
  138. var emailVerified = user.emailVerified;
  139. var photoURL = user.photoURL;
  140. var isAnonymous = user.isAnonymous;
  141. var uid = user.uid;
  142. var providerData = user.providerData;
  143. // [START_EXCLUDE silent]
  144. document.getElementById('sign-in-status').textContent = 'Signed in';
  145. document.getElementById('sign-in').textContent = 'Sign out';
  146. document.getElementById('account-details').textContent = JSON.stringify(user, null, ' ');
  147. if (!emailVerified) {
  148. document.getElementById('verify-email').disabled = false;
  149. }
  150. // [END_EXCLUDE]
  151. } else {
  152. // User is signed out.
  153. // [START_EXCLUDE silent]
  154. document.getElementById('sign-in-status').textContent = 'Signed out';
  155. document.getElementById('sign-in').textContent = 'Sign in';
  156. document.getElementById('account-details').textContent = '';
  157. // [END_EXCLUDE]
  158. }
  159. // [START_EXCLUDE silent]
  160. document.getElementById('sign-in').disabled = false;
  161. // [END_EXCLUDE]
  162. });
  163. // [END authstatelistener]
  164.  
  165. document.getElementById('sign-in').addEventListener('click', toggleSignIn, false);
  166. document.getElementById('sign-up').addEventListener('click', handleSignUp, false);
  167. document.getElementById('verify-email').addEventListener('click', sendEmailVerification, false);
  168. document.getElementById('password-reset').addEventListener('click', sendPasswordReset, false);
  169. document.getElementById('changename').addEventListener('click', updateName, false);
  170. }
  171.  
  172. function updateName(name) {
  173. var user = firebase.auth().currentUser;
  174.  
  175. if (user) {
  176. user.updateProfile({
  177. displayName: document.getElementById('newname').value,
  178. }).then(function() {
  179. location.reload();
  180. }, function(error) {
  181. // An error happened.
  182. });
  183. } else {
  184. alert("Please sign in.");
  185. }
  186. }
  187.  
  188. window.onload = function() {
  189. initApp();
  190. };
  191. </script>
  192. </head>
  193.  
  194. <body>
  195. <fieldset>
  196. <legend>Sing-In:</legend>
  197.  
  198. <input type="text" id="email" name="email" placeholder="Email" />
  199. <input type="password" id="password" name="password" placeholder="Password" />
  200. <button disabled id="sign-in" name="signin">Sign in</button>
  201. <button id="sign-up" name="signup">Register</button>
  202. <button id="password-reset" name="verify-email">Forgot password?</button>
  203. </fieldset>
  204.  
  205. <fieldset>
  206. <legend>User data:</legend>
  207.  
  208. <input type="text" id="newname" name="newname" placeholder="Display Name" />
  209. <button id="changename" name="changename">Change name</button>
  210.  
  211. <h2><span id="sign-in-status">Unknown</span></h2>
  212. <pre><code id="account-details"></code></pre>
  213. <button disabled id="verify-email" name="verify-email">Send Email Verification</button>
  214. </fieldset>
  215. </body>
  216.  
  217. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement