Guest User

Untitled

a guest
Jan 31st, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 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.  
  33. import qs from 'qs'
  34.  
  35. export default {
  36. data () {
  37. return {
  38. username: '',
  39. password: '',
  40. loading:false,
  41. submitErrors: [],
  42. invalid: false,
  43. }
  44. },
  45. methods: {
  46.  
  47. attemptLogin(){
  48. if(this.username == '' || this.password == ''){
  49. this.submitErrors = [];
  50. this.submitErrors.push("You must provide both a username and password.");
  51. this.invalid = true;
  52. setTimeout(()=>{ this.invalid = false }, 300);
  53. }
  54. else{
  55. this.submitErrors = [];
  56. this.loading = true;
  57.  
  58. let data = {
  59. username:this.username,
  60. password:this.password,
  61. client_id:contentApi.clientID,//application client
  62. grant_type: 'password'
  63. }
  64. let headers = {
  65. 'Content-Type': 'application/x-www-form-urlencoded',
  66. }
  67. axios.post(contentApi.authToken, qs.stringify(data), headers)
  68. .then(response => {
  69. localStorage.setItem("authToken", response.data.access_token);
  70. store.state.authenticated = true;
  71. })
  72. .catch(e => {
  73. this.invalid = true;
  74. setTimeout(()=>{ this.invalid = false }, 300);
  75. console.log(e);//change to raise exception.
  76. this.submitErrors = [];
  77. this.submitErrors.push(e.response.data.error_description);
  78. this.loading = false;
  79. e.response.data.error_description
  80. });
  81. //do your ajax call here to request a token.
  82. }
  83. },
  84.  
  85. },
  86. }
  87. </script>
  88.  
  89. <style lang="scss">
  90. //no styles
  91. </style>
Add Comment
Please, Sign In to add comment