Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. //USEI https://github.com/disitec/vue-passport
  2.  
  3. <template>
  4. <section>
  5. <div :class="controlClass">
  6. <h1 v-text="labels.title"></h1>
  7. </div>
  8.  
  9. <div :class="controlClass">
  10. <label class="label" v-text="labels.user"></label>
  11. <input
  12. :class="inputClass"
  13. type="text"
  14. :placeholder="userPlaceholder"
  15. v-model="login.user"
  16. >
  17. </div>
  18.  
  19. <div :class="controlClass">
  20. <label class="label" v-text="labels.password"></label>
  21. <input
  22. :class="[inputClass, {'reveal-password': passwordReveal}]"
  23. :type="typePassword"
  24. :placeholder="passwordPlaceholder"
  25. v-model="login.password"
  26. >
  27. <a v-if="passwordReveal && typePassword === 'password'" @click.prevent="toggleTypePassword">
  28. <i class="fa fa-eye fa-2x" aria-hidden="true"></i>
  29. </a>
  30. <a v-else-if="passwordReveal && typePassword === 'text'" @click.prevent="toggleTypePassword">
  31. <i class="fa fa-eye-slash fa-2x" aria-hidden="true"></i>
  32. </a>
  33. </div>
  34.  
  35. <div :class="controlClass">
  36. <a
  37. @click.prevent="handleLogin"
  38. type="submit"
  39. :class="buttonClass"
  40. v-text="labels.button"
  41. :disabled="loading"
  42. ></a>
  43.  
  44.  
  45. </div>
  46. </section>
  47. </template>
  48.  
  49. <script>
  50. import axios from 'axios'
  51. export default {
  52. name: 'VuePassportLogin',
  53. props: {
  54. labels: {
  55. type: Object,
  56. default() {
  57. return {
  58. title: 'Login',
  59. user: 'User',
  60. password: 'Password',
  61. button: 'Login'
  62. }
  63. }
  64. },
  65. userPlaceholder: String,
  66. passwordPlaceholder: String,
  67. passwordReveal: Boolean,
  68. controlClass: {
  69. type: String,
  70. default: 'control'
  71. },
  72. inputClass: {
  73. type: String,
  74. default: 'input'
  75. },
  76. buttonClass: {
  77. type: String,
  78. default: 'button'
  79. },
  80. loginRoute: {
  81. type: String,
  82. default: 'oauth/token'
  83. },
  84. userRoute: {
  85. type: String,
  86. default: 'api/user'
  87. },
  88. clientId: {
  89. type: [Number, String],
  90. default: 2
  91. }
  92. },
  93. data() {
  94. return {
  95. loading: false,
  96. login: {
  97. user: '',
  98. password: ''
  99. },
  100. typePassword: 'password',
  101. apiUrl: "http://projeto.dev",
  102. secret: "j5vHvXG5mS91NustwyyrWdEXYIprpEjNPD8cnVai",
  103. title: 'Login',
  104. user: 'User',
  105. password: 'Password',
  106. button: 'Login'
  107. }
  108. },
  109. computed: {
  110. loginUrl() {
  111. return `${this.apiUrl}/${this.loginRoute}`
  112. },
  113. userUrl() {
  114. return `${this.apiUrl}/${this.userRoute}`
  115. }
  116. },
  117. methods: {
  118. getHeaders(token) {
  119. return {
  120. Accept: 'application/json',
  121. Authorization: `Bearer ${token}`
  122. }
  123. },
  124. handleLogin() {
  125.  
  126. this.loading = true
  127. const postData = {
  128. grant_type: 'password',
  129. client_id: this.clientId,
  130. client_secret: this.secret,
  131. username: this.login.user,
  132. password: this.login.password,
  133. scope: ''
  134. }
  135.  
  136. const authUser = {}
  137. axios
  138. .post(this.loginUrl, postData)
  139. .then(response => {
  140. authUser.access_token = response.data.access_token
  141. authUser.refresh_token = response.data.refresh_token
  142. const headers = this.getHeaders(authUser.access_token)
  143. axios
  144. .get(this.userUrl, { headers })
  145. .then(response => {
  146. authUser.data = response.data
  147. this.$emit('success', { authUser, headers })
  148. this.loading = false
  149. alert('Sucesso'+authUser.client_id);
  150. })
  151. .catch(error => {
  152. alert(error);
  153. this.$emit('failed', error)
  154. this.loading = false;
  155. })
  156. })
  157. .catch(error => {
  158. this.$emit('failed', error)
  159. this.loading = false
  160. })
  161. },
  162. handleErrors (errors) {
  163. alert('Authorization error' + errors)
  164. },
  165. toggleTypePassword() {
  166. this.typePassword = this.typePassword === 'password' ? 'text' : 'password'
  167. }
  168. }
  169. }
  170.  
  171.  
  172. </script>
  173.  
  174. <style scoped>
  175. .reveal-password {
  176. width: calc(100% - 40px);
  177. margin-right: 8px;
  178. }
  179. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement