Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Vue Example</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <form>
  10. <input type="text" v-model="auth.username">
  11. {{ error.username }}
  12. <input v-bind:type="typepassword" v-model="auth.password">
  13. {{ error.password }}
  14. <button type="button" v-on:click="login()">Login</button>
  15. {{ error.success }}
  16. </form>
  17.  
  18.  
  19. <button type="button" v-on:click="switchTypePassword()">unhide</button>
  20.  
  21. <button type="button" v-on:click="logout()">log out</button>
  22.  
  23.  
  24. <div>
  25. <table>
  26. <thead>
  27. <tr>
  28. <th>id</th>
  29. <th>username</th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <tr v-for="row in userdata">
  34. <td v-for="(item, attr, i) in row">{{ item }}</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. </div>
  39. </div>
  40.  
  41.  
  42. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  43. <script src="vue.js"></script>
  44. </body>
  45. </html>
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. new Vue ({
  53. el: '#app',
  54. // yvdayfrk
  55. // rrPY6zrrPY6z
  56. // @rudrig
  57. data: {
  58. auth: {
  59. username: '',
  60. password: '',
  61. },
  62. error: {
  63. username: '',
  64. password: '',
  65. success: '',
  66. },
  67. typepassword: 'password',
  68.  
  69.  
  70. userdata: [],
  71. },
  72.  
  73. created() {
  74. this.getUserData();
  75. },
  76.  
  77. methods: {
  78. login() {
  79. fetch('http://192.168.12.44:8000/api-token-auth/', {
  80. method: 'POST',
  81. headers: {
  82. 'Content-type': 'application/json'
  83. },
  84. body: JSON.stringify(this.auth),
  85. })
  86. .then(res => {
  87. res.json()
  88. .then(body => {
  89. if(res.status === 200) {
  90. localStorage.setItem('token', body.token);
  91. this.error.success = "Log In you token: " + body.token;
  92. } else {
  93. this.error.username = body.username[0];
  94. this.error.password = body.password[0];
  95. }
  96. })
  97. })
  98. },
  99.  
  100. logout() {
  101. localStorage.setItem('token', '');
  102. alert("Log out");
  103. },
  104.  
  105. getUserData() {
  106. fetch('http://192.168.12.44:8000/users/', {
  107. method: 'GET',
  108. headers: {
  109. 'Content-type': 'application/json'
  110. },
  111. })
  112. .then(res => {
  113. res.json()
  114. .then(body => {
  115. body.results.forEach(item => {
  116. let data = {
  117. id: item.id,
  118. username: item.username,
  119. }
  120.  
  121. this.userdata.push(data);
  122. })
  123. })
  124. })
  125. },
  126.  
  127. switchTypePassword() {
  128. console.log("type");
  129. if(this.typepassword === 'text') {
  130. this.typepassword = 'password';
  131. }else if(this.typepassword === 'password') {
  132. this.typepassword = 'text';
  133. }
  134. }
  135. }
  136. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement