triemli

Untitled

Sep 28th, 2020
527
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" v-text="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="asd">
  38.                       Create a new account
  39.                     </a>
  40.                     <a href="#" class="modal-default-button" @click="$emit('close')">
  41.                       Close
  42.                     </a>
  43.                     <button type="submit" class="modal-default-button">Submit</button>
  44.                   </slot>
  45.                 </div>
  46.  
  47.               </form>
  48.             </div>
  49.           </div>
  50.         </div>
  51.       </transition>
  52.     </script>
  53.  
  54.     <div class="profile-form">
  55.  
  56.       <button id="show-modal" v-if="isGuest" @click="showModal = true">Login {{ currentUsername }}</button>
  57.       <div v-else class="user-panel">
  58.         <a href="/api/logout">Exit</a>
  59.  
  60.         <div class="identity" v-text="username"></div>
  61.       </div>
  62.  
  63.  
  64. <!--      <button @click="authenticated">authenticated</button>-->
  65.       <modal v-if="showModal" @close="showModal = false">
  66.  
  67.         <h3 slot="header">Authenticate</h3>
  68.       </modal>
  69.     </div>
  70.   </div>
  71. </template>
  72.  
  73.  
  74. <script>
  75. // import Modal from 'vue-js-modal';
  76. import {registrationInst} from "./registration";
  77.  
  78. import axios from 'axios';
  79.  
  80. export default {
  81.   name: 'Profile',
  82.   data() {
  83.     return {
  84.       showModal: false,
  85.       isGuest: true,
  86.       username: '',
  87.     };
  88.   },
  89.   computed: {
  90.     currentUsername() {
  91.       return window.currentUsername;
  92.     },
  93.   },
  94.   methods: {
  95.     authenticated(username) {
  96.       this.isGuest = false;
  97.       this.username = username;
  98.     },
  99.   },
  100.   components: {
  101.     Modal: {
  102.       template: "#modal-template",
  103.       data() {
  104.         return {
  105.           errors: [],
  106.           username: '[email protected]',
  107.           password: 'pa$$w0rd',
  108.         };
  109.       },
  110.       methods: {
  111.         asd() {
  112.           this.$emit('close');
  113.           this.$emit('go-modal', {showModal: true});
  114.         },
  115.         submit: function() {
  116.           if(!this.username) {
  117.             this.errors.push("Name required.");
  118.           }
  119.           if(!this.password) {
  120.             this.errors.push("Password required.");
  121.           }
  122.           if(!this.username || !this.password) {
  123.             return false;
  124.           }
  125.  
  126.           axios.post('/api/login',{
  127.             /*headers: {
  128.               'Content-type': 'application/x-www-form-urlencoded',
  129.             },*/
  130.             params: {
  131.               username: this.username,
  132.               password: this.password
  133.             }})
  134.               .then(response => {
  135.                 if(response.data.success) {
  136.                   this.$parent.authenticated(response.data.identity.username);
  137.                   this.$emit('close');
  138.                 }
  139.               })
  140.               .catch(error => {});
  141.         }
  142.       },
  143.  
  144.     }
  145.   },
  146.  
  147.   created(){
  148.     if(window.currentUsername) {
  149.       this.authenticated(window.currentUsername);
  150.     }
  151.   },
  152.   mounted() {
  153.  
  154.   },
  155. };
  156. </script>
  157.  
Advertisement
Add Comment
Please, Sign In to add comment