Advertisement
Guest User

Untitled

a guest
Feb 5th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var data = {
  2.     isAuthenticated: false, isValid: true, userName: "",
  3.     login: {
  4.         username: "",
  5.         password: ""
  6.     }
  7. }
  8.  
  9. // register
  10. Vue.component("auth-partial",
  11. {
  12.     template: `
  13.         <ul class ="nav navbar-nav">
  14.             <li class ="nav-item float-xs-right">
  15.                 <a href="#" class ="nav-link" data-toggle="modal" data-target="#loginModal" v-if="!isAuthenticated">
  16.                     Log in
  17.                 </a>
  18.                 <a href="#" class ="nav-link" v-on:click="logout" v-if="isAuthenticated">
  19.                     Hello {{userName}}!
  20.                 </a>
  21.             </li>
  22.         </ul>
  23.     `,
  24.     data: function() {
  25.         return data;
  26.     },
  27.     ready: function () {
  28.         this.isAuthenticated = this.checkIfAuthenticated();
  29.         this.userName = localStorage.getItem("id_name");
  30.     },
  31.  
  32.     methods: {
  33.         checkIfAuthenticated: function () {
  34.             const users = [
  35.                 {
  36.                     id: 1,
  37.                     name: "tom"
  38.                 },
  39.                 {
  40.                     id: 2,
  41.                     name: "brian"
  42.                 },
  43.                 {
  44.                     id: 3,
  45.                     name: "sam"
  46.                 }
  47.             ];
  48.  
  49.             this.$set("users", users);
  50.         }
  51.     }
  52. });
  53. // create a root instance
  54. var vue = new Vue({
  55.     el: "#navbarResponsive"
  56. });
  57.  
  58.  
  59.  
  60. // register
  61. Vue.component("login-form",
  62. {
  63.     template: `
  64.         <div class ="modal fade" id="loginModal" tabindex="-1" role="dialog" v-if="!isAuthenticated" aria-labelledby="loginModalLabel" aria-hidden="true">
  65.         <div class ="modal-dialog modal-sm" role="document">
  66.             <div class ="modal-content">
  67.                 <form v-on:submit.prevent="login($event)">
  68.                     <div class ="modal-header">
  69.                         <button type="button" class ="close" data-dismiss="modal" aria-label="Close">
  70.                             <span aria-hidden="true">&times; </span>
  71.                         </button>
  72.                         <h4 class ="modal-title" id="loginModalLabel">Log in</h4>
  73.                     </div>
  74.                     <div class ="modal-body">
  75.                         <div v-if="!isValid"
  76.                                 class ="alert alert-danger">
  77.                             Invalid username or password.
  78.                         </div>
  79.                         <div class ="form-group">
  80.                             <input class ="form-control" type="text" name="Username" v-model="login.username" placeholder="Username" id="formLoginUsername" required>
  81.                         </div>
  82.                         <div class ="form-group">
  83.                             <input type="password" class ="form-control" name="Password" v-model="login.password" id="formLoginPassword" placeholder="Password" required>
  84.                         </div>
  85.                     </div>
  86.                     <div class ="modal-footer">
  87.                         <button type="button" class ="btn btn-secondary" data-dismiss="modal">Close</button>
  88.                         <button type="button" type="submit" class ="btn btn-primary">Sign in</button>
  89.                     </div>
  90.                 </form>
  91.             </div>
  92.         </div>
  93.     </div>`
  94. ,
  95.     data: function () {
  96.         return data;
  97.     },
  98.     ready: function () {
  99.         this.isAuthenticated = this.checkIfAuthenticated();
  100.         this.userName = localStorage.getItem("id_name");
  101.     },
  102.  
  103.     methods: {
  104.         checkIfAuthenticated: function () {
  105.             const users = [
  106.                 {
  107.                     id: 1,
  108.                     name: "tom"
  109.                 },
  110.                 {
  111.                     id: 2,
  112.                     name: "brian"
  113.                 },
  114.                 {
  115.                     id: 3,
  116.                     name: "sam"
  117.                 }
  118.             ];
  119.  
  120.             this.$set("users", users);
  121.         },
  122.         login: function (event) {
  123.             const headers = { "Content-Type": "application/x-www-form-urlencoded" };
  124.  
  125.             $.ajax({
  126.                 url: "/token",
  127.                 type: "post",
  128.                 data: `username=${this.login.username}&password=${this.login.password}`,
  129.                 headers: headers,
  130.                 success: function (data) {
  131.                     console.info(data);
  132.                 },
  133.                 error: function() {
  134.                     this.isValid = false;
  135.                 }
  136.             });
  137.         }
  138.     }
  139. });
  140. // create a root instance
  141. var vue = new Vue({
  142.     el: "#loginForm"
  143. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement