triemli

Untitled

Sep 25th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div>
  3.     <script type="text/x-template" id="modal-template">
  4.       <transition name="modal">
  5.         <div class="modal-mask">
  6.           <div class="modal-wrapper">
  7.             <div class="modal-container">
  8.               <form id="login-form" @submit.stop.prevent="submit">
  9.  
  10.                 <div class="modal-header">
  11.                   <slot name="header">
  12.                     Authenticate!
  13.                   </slot>
  14.                 </div>
  15.  
  16.                 <div class="modal-body">
  17.                   <slot name="body">
  18.                     <div v-if="errors.length">
  19.                       <b>Please correct the following error(s):</b>
  20.                       <ul>
  21.                         <li v-for="(err, index) in errors"> {{ err }}</li>
  22.                       </ul>
  23.                     </div>
  24.  
  25.                     <label>Login:
  26.                       <input type="text" name="username" v-model="username">
  27.                     </label>
  28.                     <label>Password:
  29.                       <input type="password" name="password" v-model="password">
  30.                     </label>
  31.                   </slot>
  32.                 </div>
  33.  
  34.                 <div class="modal-footer">
  35.  
  36.                   <slot name="footer">
  37.                     <a href="#" class="modal-default-button" @click="$emit('close')">
  38.                       Close
  39.                     </a>
  40.                     <button type="submit" class="modal-default-button">Submit</button>
  41.                   </slot>
  42.                 </div>
  43.  
  44.               </form>
  45.             </div>
  46.           </div>
  47.         </div>
  48.       </transition>
  49.     </script>
  50.  
  51.     <div class="profile-form">
  52.  
  53.       <button id="show-modal" v-if="isGuest" @click="showModal = true">Login {{ currentUsername }}</button>
  54.       <div v-else class="user-panel">
  55.         <a href="/api/logout">Exit</a>
  56.  
  57.         <div class="identity" v-text="username"></div>
  58.       </div>
  59.  
  60.  
  61. <!--      <button @click="authenticated">authenticated</button>-->
  62.       <modal v-if="showModal" @close="showModal = false">
  63.  
  64.         <h3 slot="header">Authenticate</h3>
  65.       </modal>
  66.     </div>
  67.   </div>
  68. </template>
  69.  
  70.  
  71. <script>
  72. import Modal from 'vue-js-modal';
  73. import axios from 'axios';
  74.  
  75. export default {
  76.   name: 'Profile',
  77.   data() {
  78.     return {
  79.       showModal: false,
  80.       isGuest: true,
  81.       username: '',
  82.       err: '',
  83.     };
  84.   },
  85.   computed: {
  86.     currentUsername() {
  87.       return window.currentUsername;
  88.     },
  89.   },
  90.   methods: {
  91.     authenticated(username) {
  92.       this.isGuest = false;
  93.       this.username = username;
  94.     }
  95.   },
  96.   components: {
  97.     Modal: {
  98.       template: "#modal-template",
  99.       data() {
  100.         return {
  101.           errors: ['asd', 'sdfdfg'],
  102.           username: '[email protected]',
  103.           password: 'pa$$w0rd',
  104.         };
  105.       },
  106.       methods: {
  107.         submit: function() {
  108.           if(!this.username) {
  109.             this.errors.push("Name required.");
  110.             return false;
  111.           }
  112.           axios.post('/api/login',{
  113.             /*headers: {
  114.               'Content-type': 'application/x-www-form-urlencoded',
  115.             },*/
  116.             params: {
  117.               username: this.username,
  118.               password: this.password
  119.             }})
  120.               .then(response => {
  121.                 if(response.data.success) {
  122.                   this.$parent.authenticated(response.data.identity.username);
  123.                   this.$emit('close');
  124.                 }
  125.               })
  126.               .catch(error => {});
  127.         }
  128.       },
  129.  
  130.     }
  131.   },
  132.  
  133.   created(){
  134.     if(window.currentUsername) {
  135.       this.authenticated(window.currentUsername);
  136.     }
  137.   },
  138.   mounted() {
  139.  
  140.   },
  141. };
  142. </script>
  143.  
Advertisement
Add Comment
Please, Sign In to add comment