Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. var config = {
  2. apiKey: "asdfasdfasdf",
  3. authDomain: "asdfasdfasdf",
  4. databaseURL: "asdfasdfasdf",
  5. storageBucket: "asdfasdfasdf",
  6. };
  7. firebase.initializeApp(config);
  8.  
  9. //The stuff
  10. var logine = document.getElementById("logine");
  11. var reggy = document.getElementById("reggy");
  12. var usr = document.getElementById("usr");
  13. var pwd = document.getElementById("pwd");
  14. var reset = document.getElementById("forgot");
  15. var google = document.getElementById("google");
  16. var logout = document.getElementById("logout");
  17. var github = document.getElementById("github");
  18.  
  19. //Login event
  20. function loginEvent(){
  21. //Retreive the email and dat pwd
  22. var email = usr.value;
  23. var password = pwd.value;
  24. var auth = firebase.auth();
  25. //User signs in
  26. var promise = auth.signInWithEmailAndPassword(email,password).catch(function(error){
  27. //Handle any errors here
  28. var errorCode = error.code;
  29. var errorMessage = error.message;
  30.  
  31. if (errorCode === 'auth/wrong-password'){
  32. alert('Get the password right');
  33. } else {
  34. alert(errorMessage);
  35. }
  36. });
  37. promise.catch(e => console.log(e.message));
  38. if (email.length < 12){
  39. document.getElementById("bademail").classList.remove("hide");
  40.  
  41. }
  42. if(password.length < 6){
  43. document.getElementById("bademail").classList.remove("hide");
  44. }
  45. }
  46.  
  47. //Register event
  48. function registerEvent(){
  49. var email = usr.value;
  50. var password = pwd.value;
  51. var auth = firebase.auth();
  52.  
  53. //User signs in
  54.  
  55. // TODO: Check for email
  56. var promise = auth.createUserWithEmailAndPassword(email, password).catch(function(error){
  57. //Handle any errors here
  58. var errorCode = error.code;
  59. var errorMessage = error.message;
  60.  
  61. if(errorCode === 'auth/weak-password'){
  62. alert('All passwords are must be 6 characters or more')
  63. } else {
  64. alert(errorMessage);
  65. }
  66.  
  67. });
  68. promise
  69. .catch(e => console.log(e.message));
  70. if (email.length < 12){
  71. document.getElementById("bademail").classList.remove("hide");
  72. }
  73.  
  74. }
  75. function logoutEvent(){
  76. firebase.auth().signOut();
  77. }
  78. //Signing in with Google
  79. function googleAuth(){
  80. //Google Auth
  81. var provider = new firebase.auth.GoogleAuthProvider();
  82. firebase.auth().signInWithPopup(provider).then(function(result) {
  83. // This gives you a Google Access Token. You can use it to access the Google API.
  84. var token = result.credential.accessToken;
  85. // The signed-in user info.
  86. var user = result.user;
  87. // ...
  88. }).catch(function(error) {
  89. // Handle Errors here.
  90. var errorCode = error.code;
  91. var errorMessage = error.message;
  92. // The email of the user's account used.
  93. var email = error.email;
  94. // The firebase.auth.AuthCredential type that was used.
  95. var credential = error.credential;
  96. // ...
  97. });
  98. }
  99. //Signing in with github
  100. function githubAuth(){
  101. var provider = new firebase.auth.GithubAuthProvider();
  102. firebase.auth().signInWithPopup(provider).then(function(result) {
  103. // This gives you a GitHub Access Token. You can use it to access the GitHub API.
  104. var token = result.credential.accessToken;
  105. // The signed-in user info.
  106. var user = result.user;
  107. // ...
  108. }).catch(function(error) {
  109. // Handle Errors here.
  110. var errorCode = error.code;
  111. var errorMessage = error.message;
  112. // The email of the user's account used.
  113. var email = error.email;
  114. // The firebase.auth.AuthCredential type that was used.
  115. var credential = error.credential;
  116. // ...
  117. });
  118.  
  119. }
  120. //Password reset email
  121. function sendPasswordReset() {
  122. var email = document.getElementById('usr').value;
  123. // [START sendpasswordemail]
  124. firebase.auth().sendPasswordResetEmail(email).then(function() {
  125. // Password Reset Email Sent!
  126. // [START_EXCLUDE]
  127. alert('We sent the email so check tu inbox');
  128. // [END_EXCLUDE]
  129. }).catch(function(error) {
  130. // Handle Errors here.
  131. var errorCode = error.code;
  132. var errorMessage = error.message;
  133. // [START_EXCLUDE]
  134. if (errorCode == 'auth/invalid-email') {
  135. alert(errorMessage);
  136. } else if (errorCode == 'auth/user-not-found') {
  137. alert(errorMessage);
  138. }
  139. console.log(error);
  140. // [END_EXCLUDE]
  141. });
  142. // [END sendpasswordemail];
  143. }
  144.  
  145. //Realtime listener
  146. firebase.auth().onAuthStateChanged(firebaseUser => {
  147. if(firebaseUser){
  148. console.log(firebaseUser);
  149. document.getElementById("logout").classList.remove('hide');
  150. window.location("index.html");
  151. }else{
  152. console.log("GET OUT KID");
  153. document.getElementById("logout").classList.add('hide');
  154. reset.addEventListener('click', sendPasswordReset, false);
  155. google.addEventListener('click', googleAuth, false);
  156. logine.addEventListener('click', loginEvent, false);
  157. reggy.addEventListener('click', registerEvent, false);
  158. logout.addEventListener('click', logoutEvent, false);
  159. github.addEventListener('click', githubAuth, false);
  160. }
  161. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement