Guest User

Untitled

a guest
Jan 25th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import Vue from 'vue'
  2. import App from './App'
  3. import Firebase from 'firebase';
  4. import vuefire from 'vuefire';
  5. import { store } from './store/store';
  6. import router from './router/index';
  7. import { config } from './firebase-config'
  8.  
  9. Vue.config.productionTip = false
  10. Vue.use(vuefire)
  11. export const firebaseApp = Firebase.initializeApp(config);
  12. export const db = firebaseApp.database();
  13. export const usersRef = db.ref('users')
  14.  
  15. Firebase.auth().onAuthStateChanged(function(user){
  16. new Vue({
  17. el: '#app',
  18. router,
  19. store,
  20. components: { App },
  21. template: '<App/>'
  22. })
  23. })
  24.  
  25. <template>
  26. <section class="section">
  27. <div class="col-half">
  28. <h2>Create an Account</h2>
  29. <form v-on:submit.prevent>
  30. <div class="control">
  31. <input class="input" type="text" placeholder="Your Display Name">
  32. </div>
  33. <div class="control">
  34. <input class="input" type="email" placeholder="joe@someplace.com" >
  35. </div>
  36. <div class="control">
  37. <input id="password" class="input" type="password" placeholder="Password">
  38. </div>
  39. <div class="control">
  40. <input id="confirm_password" class="input" type="password" placeholder="Retype your password" v-on:keyup="checkRetypePassword">
  41. </div><span id='message'></span>
  42. <button type="submit" class="button is-primary" v-on:click="signUp">Register</button><span> or </span><button type="submit" class="button is-primary" v-on:click="googleSignUp">Register with Google</button>
  43. </form>
  44. <p>Already registered? <router-link to="/Sign-In">Log in</router-link></p>
  45. </div>
  46. </section>
  47. </template>
  48.  
  49. <script>
  50. import Firebase from "firebase";
  51. import { usersRef, firebaseApp } from '../main';
  52.  
  53. var provider = new Firebase.auth.GoogleAuthProvider();
  54. var emailRE = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/
  55.  
  56. export default {
  57. data: function() {
  58. return {
  59. username: this.$store.getters.getUser.displayName, //side comment, this is throwing an error if user is not signed in, not important but I thought I should mention
  60. email: "",
  61. password: "",
  62. ref: "",
  63.  
  64. };
  65. },
  66. firebase() {
  67. return {
  68. users: usersRef,
  69. newUser: {
  70. name: '',
  71. email: '',
  72. uid: ''
  73. }
  74. }
  75. },
  76. beforeMount(){
  77. this.checkuserlogged()
  78. },
  79. methods: {
  80. signUp: function() {
  81. Firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
  82. var user = Firebase.auth().currentUser;
  83. logUser(user); // Optional
  84. }, function(error) {
  85. // Handle Errors here.
  86. var errorCode = error.code;
  87. var errorMessage = error.message;
  88. });
  89. function logUser(user) {
  90. var ref = firebase.database().ref("users");
  91. var obj = {
  92. "user": {
  93. name: '',
  94. email: '',
  95. },
  96. };
  97. console.log(obj);
  98. ref.push(obj); // or however you wish to update the node
  99. }
  100.  
  101. },
  102. googleSignUp: function() {
  103. Firebase.auth().signInWithRedirect(provider).then(function(result) {
  104. // This gives you a Google Access Token. You can use it to access the Google API.
  105. var token = result.credential.accessToken;
  106. // The signed-in user info.
  107. var user = result.user;
  108. }).catch(function(error) {
  109. // Handling 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. // PREVIOUS FAILED ATTEMPT TO REGISTER USERS AND WRITE TO DB
  120. // registerUserDbwriteUserData: function(userId, name, email, imageUrl) {
  121. // user.onAuth(function(authData) {
  122. // if (authData && isNewUser) {
  123. // // save the user's profile into the database so we can list users,
  124. // // use them in Security and Firebase Rules, and show profiles
  125. // firebase.database().ref('users/' + userId).set({
  126. // username: name,
  127. // email: email,
  128. // profile_picture : imageUrl
  129. // });
  130. // }
  131. // });
  132. // googleSignUp();
  133. // },
  134.  
  135. checkuserlogged: function(){
  136. var user = Firebase.auth().currentUser;
  137. if ( user != null ) {
  138. this.$router.replace('/dashboard');
  139. }
  140. },
  141. checkRetypePassword: function(){
  142. var password = document.getElementById('password');
  143. var confirmPassword = document.getElementById('confirm_password');
  144. var message = document.getElementById('message');
  145. if (password.value == confirmPassword.value) {
  146. confirmPassword.style.color = '#8F5';
  147. message.style.color = '#8F5';
  148. message.innerHTML = 'Retype is matching';
  149. } else {
  150. confirmPassword.style.color = 'red';
  151. message.style.color = 'red';
  152. message.innerHTML = 'Password is not matching';
  153. }
  154. }
  155. }
  156. };
  157. </script>
  158.  
  159. <style>
  160.  
  161. </style>
Add Comment
Please, Sign In to add comment