Guest User

Untitled

a guest
Dec 20th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import Vue from 'vue'
  2. import firebase from 'firebase'
  3. import BootstrapVue from 'bootstrap-vue'
  4. import 'bootstrap/dist/css/bootstrap.css'
  5. import 'bootstrap-vue/dist/bootstrap-vue.css'
  6. import Router from 'vue-router'
  7. import Login from '../views/Login.vue'
  8. import Home from '../views/Home.vue'
  9. import SignUp from '../views/SignUp.vue'
  10.  
  11. Vue.use(Router)
  12. Vue.use(BootstrapVue)
  13.  
  14. const router = new Router({
  15. routes: [
  16. {
  17. path: '/login',
  18. name: 'login',
  19. component: Login
  20. },
  21. {
  22. path: '/home',
  23. name: 'home',
  24. component: Home,
  25. meta: {
  26. requiresAuth: true
  27. }
  28. },
  29. {
  30. path: '/',
  31. name: 'login',
  32. component: Login
  33.  
  34. },
  35. {
  36. path: '/sign-up',
  37. name: 'sign up',
  38. component: SignUp
  39. },
  40. {
  41. path: '*',
  42. redirect: '/login'
  43. }
  44. ]
  45. })
  46.  
  47. router.beforeEach((to, from, next) => {
  48. const currentUser = firebase.auth().currentUser
  49. const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  50.  
  51. if (requiresAuth && !currentUser) next('login')
  52. else if (!requiresAuth && currentUser) next('home')
  53. else next()
  54. })
  55.  
  56. export default router
  57.  
  58. import Vue from 'vue'
  59. import firebase from 'firebase'
  60. import App from './App'
  61. import router from './router'
  62.  
  63. Vue.config.productionTip = false
  64.  
  65. // Initialize Firebase
  66. let app = ''
  67. const config = {
  68. apiKey: 'AIzaSyDE0IVgepdhCOJLjcunmLN35AJ-6e0X0ak',
  69. authDomain: 'repti-care-90f1d.firebaseapp.com',
  70. databaseURL: 'https://repti-care-90f1d.firebaseio.com',
  71. projectId: 'repti-care-90f1d',
  72. storageBucket: 'repti-care-90f1d.appspot.com',
  73. messagingSenderId: '551446965940'
  74. }
  75.  
  76. firebase.initializeApp(config)
  77.  
  78. firebase.auth().onAuthStateChanged(() => {
  79. if (!app) {
  80. app = new Vue({
  81. router,
  82. render: h => h(App)
  83. }).$mount('#app')
  84. }
  85. })
  86.  
  87. /* eslint-disable no-new */
  88. new Vue({
  89. el: '#app',
  90. router,
  91. components: { App },
  92. template: '<App/>'
  93. })
  94.  
  95. <template>
  96. <div class="login">
  97. <form action="" class="form-signin">
  98. <h3 class="heading">Sign In</h3>
  99. <input type="text" placeholder="Email" v-model="email" class="form-control"><br>
  100. <input type="password" placeholder="Password" v-model="password" class="form-control"><br>
  101. <button @click="login" class="btn btn-lg btn-primary btn-block">Log In</button>
  102. <p>You don't have an account? You can <router-link to="/sign-up">create one here</router-link></p>
  103. </form>
  104. </div>
  105. </template>
  106.  
  107.  
  108. <script>
  109. import firebase from 'firebase'
  110.  
  111. export default {
  112. name: 'login',
  113. data() {
  114. return {
  115. email: '',
  116. password: ''
  117. }
  118. },
  119. methods: {
  120. login: function () {
  121. firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
  122. function(user) {
  123. alert('Welcome!')
  124. },
  125. function (error) {
  126. alert(`Ooops ${error}`)
  127. }
  128. )
  129. }
  130. }
  131. }
  132. </script>
  133.  
  134. <template>
  135. <div class="sign-up">
  136. <form action="" class="form-signin">
  137. <h3 class="heading">Create a new account</h3>
  138. <input type="text" placeholder="Email" v-model="email" class="form-control"><br>
  139. <input type="password" placeholder="Password" v-model="password" class="form-control"><br>
  140. <button @click="signUp" class="btn btn-lg btn-primary btn-block">Sign Up</button>
  141. <span>Already have an account?<router-link to="/login"> Sign in Here</router-link></span>
  142. </form>
  143. </div>
  144. </template>
  145.  
  146. <script>
  147. import firebase from 'firebase'
  148. export default {
  149. name: 'signUp',
  150. data() {
  151. return {
  152. email: '',
  153. password: ''
  154. }
  155. },
  156. methods: {
  157. signUp: function () {
  158. firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(function(user) {
  159. alert('Your account has been created!')
  160. },
  161. function (error) {
  162. alert(`Ooops ${error.message}`)
  163. }
  164. )
  165. }
  166. }
  167. }
  168. </script>
Add Comment
Please, Sign In to add comment