Guest User

Untitled

a guest
Oct 31st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import addPost from '@/components/addPost'
  4. import showPost from '@/components/showPost'
  5. import Login from '@/components/Login'
  6. import SignUp from '@/components/SignUp'
  7. import firebase from 'firebase'
  8.  
  9.  
  10. Vue.use(Router)
  11.  
  12. const router = new Router({
  13. mode: 'history',
  14. routes: [
  15. {
  16. path: '/',
  17. name: 'app',
  18. component: showPost
  19. },
  20. {
  21. path: '/add',
  22. component: addPost,
  23. meta: {
  24. requiresAuth: true
  25. }
  26. },
  27. {
  28. path: '/login',
  29. name: 'Login',
  30. component: Login
  31. },
  32. {
  33. path: '/signup',
  34. name: 'SignUp',
  35. component: SignUp
  36. }
  37. ]
  38. })
  39.  
  40. router.beforeEach((to, from, next) => {
  41. let currentUser = firebase.auth().currentUser;
  42. let requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  43.  
  44. if (requiresAuth && !currentUser) next('/login')
  45. else if (!requiresAuth && currentUser) next('/')
  46. else next()
  47. })
  48.  
  49.  
  50. export default router
  51.  
  52. import Vue from 'vue'
  53. import App from './App'
  54. import router from './router'
  55. import VueFire from 'vuefire'
  56. import firebase from 'firebase'
  57.  
  58. Vue.use(VueFire)
  59.  
  60. Vue.config.productionTip = false
  61.  
  62. let app;
  63.  
  64. firebase.auth().onAuthStateChanged(function(user) {
  65.  
  66. if(!app) {
  67. app = new Vue({
  68. el: '#app',
  69. router,
  70. template: '<App/>',
  71. components: { App }
  72. })
  73. }
  74.  
  75. })
  76.  
  77. <template>
  78. <div id="app">
  79. <app-header></app-header>
  80. <router-view></router-view>
  81. <button @click="logout">Logout</button>
  82. </div>
  83. </template>
  84.  
  85. <script>
  86. import header from './components/header'
  87. import firebase from 'firebase'
  88.  
  89. export default {
  90. name: 'app',
  91. components: {
  92. 'app-header': header
  93. },
  94. methods: {
  95. logout: function() {
  96. firebase.auth().signOut().then(() => {
  97. this.$router.replace('login')
  98. })
  99. }
  100. }
  101. }
  102.  
  103. </script>
  104.  
  105. import firebase from 'firebase'
  106.  
  107. import db from '../firebaseInit'
  108. const postRef = db.ref('posts')
  109.  
  110. export default {
  111. name: 'login',
  112. data: function() {
  113. return {
  114. email: '',
  115. password: ''
  116. }
  117. },
  118. methods: {
  119. signIn: function() {
  120. firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
  121. (user) => {
  122. this.$router.replace('/')
  123. },
  124. (err) => {
  125. alert('Oops ' + err.message)
  126. }
  127. );
  128. }
  129. }
  130. }
  131. </script>
  132.  
  133. <template>
  134. <div id="add-blog">
  135. <h2>Add a New Post</h2>
  136. <form v-if="!submitted">
  137. <label>Title:</label>
  138. <input type="text" v-model="newPost.title" required />
  139. <p>{{ getDate }}</p>
  140. <label for="">Content:</label>
  141. <textarea v-model.trim="newPost.content"></textarea>
  142. <div id="checkboxes">
  143. <p>Categories:</p>
  144. <label>Vue.js</label>
  145. <input type="checkbox" value="vue" v-model="newPost.categories" />
  146. <label>CSS Magic</label>
  147. <input type="checkbox" value="css" v-model="newPost.categories" />
  148. </div>
  149. <label>Author:</label>
  150. <select v-model="newPost.author">
  151. <option v-for="author in authors">{{ author }}</option>
  152. </select>
  153. <button @click.prevent="addPost">Add Post</button>
  154. </form>
  155. <div v-if="submitted">
  156. <p>Congraturation!</p>
  157. </div>
  158. <div id="preview">
  159. <h3>Preview Post</h3>
  160. <h4>Title {{ newPost.title }}</h4>
  161. <h4>Content </h4>
  162. <p style="white-space: pre">{{ newPost.content }}</p>
  163. <ul>
  164. <li v-for="category in newPost.categories">{{ category }}</li>
  165. </ul>
  166. <p>{{ newPost.author }}</p>
  167. </div>
  168. </div>
  169. </template>
  170.  
  171. <script>
  172.  
  173. import db from '../firebaseInit'
  174. const postRef = db.ref('posts')
  175.  
  176. export default {
  177. data() {
  178. return {
  179. newPost: {
  180. date: '',
  181. title: '',
  182. author: '',
  183. content: '',
  184. categories: []
  185. },
  186. authors: ['Naeun', 'Raphael'],
  187. submitted: false,
  188. items: []
  189. }
  190. },
  191. methods: {
  192. addPost: function() {
  193. postRef.push(this.newPost)
  194. this.newPost.date = '',
  195. this.newPost.title = '',
  196. this.newPost.author = '',
  197. this.newPost.content = '',
  198. this.newPost.categories = ''
  199. },
  200. removePost: function() {
  201. postRef.child(post['.key']).remove()
  202. }
  203. },
  204. computed: {
  205. getDate: function() {
  206. const toTwoDigits = num => num < 10 ? '0' + num : num;
  207. let today = new Date();
  208. let year = today.getFullYear();
  209. let month = toTwoDigits(today.getMonth() + 1);
  210. let day = toTwoDigits(today.getDate());
  211. return this.newPost.date = `${year}-${month}-${day}`;
  212. }
  213. }
  214. }
  215.  
  216. </script>
Add Comment
Please, Sign In to add comment