Advertisement
asimryu

app.vue

May 5th, 2021
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div class="todoListContainer">
  3.         <div class="heading">
  4.             <h2 id="title">Todo List</h2>
  5.             <add-item-form v-on:reloadlist="getList()" />
  6.         </div>
  7.         <list-view
  8.             :items="items"
  9.             v-on:reloadlist="getList()"
  10.             />
  11.         <div id="loginform" v-bind:class="[ isLogin ? 'hide' : 'show' ]">
  12.             <form v-on:submit.prevent="loginUser">
  13.                 <p class="h4 text-center mb-4">Sign in</p>
  14.                 <div class="grey-text">
  15.                     <input placeholder="email" id="email" type="email" ref="email"/>
  16.                     <input placeholder="password" id="password" type="password" ref="password"/>
  17.                 </div>
  18.                 <div class="text-center">
  19.                     <button>Login</button>
  20.                 </div>
  21.             </form>
  22.         </div>         
  23.     </div>
  24.  
  25. </template>
  26. <script>
  27. import addItemForm from "./addItemForm"
  28. import listView from "./listView"
  29. export default {
  30.     components: {
  31.         addItemForm,
  32.         listView
  33.     },
  34.     data: function () {
  35.         return {
  36.             items: [],
  37.             isLogin: false,
  38.             token: ''
  39.         }
  40.     },
  41.     methods: {
  42.         getList () {
  43.             let headers = {
  44.                     'headers': {
  45.                         'Content-Type': 'application/json;charset=utf-8',
  46.                         'Accept': 'application/json',
  47.                         'Authorization': 'Bearer ' + localStorage.token                    
  48.                     }
  49.                 };
  50.             axios.get('/api/items', headers)
  51.             .then(response => {
  52.                 this.items = response.data
  53.             })
  54.             .catch( error => {
  55.                 console.log( error )
  56.             })
  57.         },
  58.         loginUser() {
  59.             let headers = {
  60.                 'headers': {
  61.                     'Content-Type': 'application/json;charset=utf-8',
  62.                     'Accept': 'application/json'
  63.                 }
  64.             };
  65.             var email = this.$refs.email.value;
  66.             var password = this.$refs.password.value;
  67.             axios.post('/api/login', {
  68.                 email: email,
  69.                 password: password
  70.             }, headers)
  71.             .then(response => {
  72.                 let user = response.data;
  73.                 let access_token = user.access_token;
  74.                 localStorage.token = access_token;
  75.                 console.log(localStorage.token);
  76.                 this.getList();
  77.             })
  78.             .catch ((err) => {
  79.                 console.log (err)
  80.             })
  81.         }
  82.     },
  83.     created() {
  84.         if (localStorage.token) {
  85.             this.isLogin = true;
  86.             this.token = localStorage.access_token;
  87.             this.getList();
  88.         } else {
  89.             this.isLogin = false;
  90.             this.token = '';
  91.         }
  92.     }  
  93. }
  94. </script>
  95. <style scoped>
  96.     .todoListContainer {
  97.         width: 350px;
  98.         margin: auto;
  99.     }
  100.     .heading {
  101.         background: #e6e6e6;
  102.         padding: 10px;
  103.     }
  104.     #title {
  105.         text-align: center;
  106.     }
  107.     .hide { display: none; }
  108.     .show { display: block; }
  109. </style>
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement