Guest User

Untitled

a guest
Jan 31st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. <template>
  2. <div class="row">
  3. <div class="container">
  4. <div class="col-md-4 col-md-offset-4">
  5. <div class="col-md-12">
  6. </div>
  7. <div class="col-md-12 no-p no-m">
  8. <ul class="error-list " v-for="error in submitErrors">
  9. <li class="error">{{error}}</li>
  10. </ul>
  11. </div>
  12. <div v-if="loading" class="animated-loader">Loading...</div>
  13. <form v-else v-on:submit.prevent="attemptLogin" v-bind:class="{ invalid: invalid }" >
  14. <label>Username:</label>
  15. <input type="text" name="username" v-model="username" class="form-control" >
  16. <br>
  17. <label>Password:</label>
  18. <input type="password" name="password" v-model="password" class="form-control">
  19. <br>
  20. <input type="submit" value="Login" class="btn btn-default submit-btn">
  21. </form>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26.  
  27. <script>
  28. import store from '../../../store.js'
  29. import axios from 'axios'
  30. import ContentApi from '../../../api.js'
  31. const contentApi = new ContentApi();
  32. import qs from 'qs'
  33.  
  34. export default {
  35. data () {
  36. return {
  37. username: '',
  38. password: '',
  39. loading:false,
  40. submitErrors: [],
  41. invalid: false,
  42. }
  43. },
  44. methods: {
  45. attemptLogin(){
  46. if(this.username == '' || this.password == ''){
  47. this.submitErrors = [];
  48. this.submitErrors.push("You must provide both a username and password.");
  49. this.invalid = true;
  50. setTimeout(()=>{ this.invalid = false }, 300);
  51. }
  52. else{
  53. this.submitErrors = [];
  54. this.loading = true;
  55.  
  56. let data = {
  57. username:this.username,
  58. password:this.password,
  59. client_id:contentApi.clientID,//application client
  60. grant_type: 'password'
  61. }
  62.  
  63. let headers = {
  64. 'Content-Type': 'application/x-www-form-urlencoded',
  65. }
  66.  
  67. axios.post(contentApi.authToken, qs.stringify(data), headers)
  68. .then(response => {
  69. this.loading = true;
  70. localStorage.setItem("authToken", response.data.access_token);
  71. this.invalid = false;
  72. store.state.authenticated = true;
  73. })
  74. .catch(e => {
  75. this.invalid = true;
  76. this.loading = false;
  77. this.submitErrors.push(e.response.data.error_description);
  78. console.log(e);
  79. });
  80. }
  81. },
  82.  
  83. },
  84. }
  85. </script>
  86.  
  87. <style lang="scss">
  88. //no styles for medium post
  89. </style>
Add Comment
Please, Sign In to add comment