Guest User

Untitled

a guest
Jan 31st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import store from '../../../store.js'
  2. import axios from 'axios'
  3. import ContentApi from '../../../api.js'
  4. const contentApi = new ContentApi();
  5.  
  6. import qs from 'qs'
  7.  
  8. export default {
  9. data () {
  10. return {
  11. username: '',
  12. password: '',
  13. loading:false,
  14. submitErrors: [],
  15. invalid: false,
  16. }
  17. },
  18. methods: {
  19.  
  20. attemptLogin(){
  21. if(this.username == '' || this.password == ''){
  22. this.submitErrors = [];
  23. this.submitErrors.push("You must provide both a username and password.");
  24. this.invalid = true;
  25. setTimeout(()=>{ this.invalid = false }, 300);
  26. }
  27. else{
  28. this.submitErrors = [];
  29. this.loading = true;
  30. let that = this;
  31.  
  32. let data = {
  33. username:this.username,
  34. password:this.password,
  35. client_id:contentApi.clientID,//application client
  36. grant_type: 'password'
  37. }
  38. let headers = {
  39. 'Content-Type': 'application/x-www-form-urlencoded',
  40. }
  41. axios.post(contentApi.authToken, qs.stringify(data), headers)
  42. .then(response => {
  43. localStorage.setItem("authToken", response.data.access_token);
  44. store.state.authenticated = true;
  45. })
  46. .catch(e => {
  47. that.invalid = true;
  48. setTimeout(()=>{ that.invalid = false }, 300);
  49. console.log(e);//change to raise exception.
  50. that.submitErrors = [];
  51. that.submitErrors.push(e.response.data.error_description);
  52. that.loading = false;
  53. e.response.data.error_description
  54. });
  55. //do your ajax call here to request a token.
  56. }
  57. },
  58.  
  59. },
  60. }
Add Comment
Please, Sign In to add comment