Skip to content

Instantly share code, notes, and snippets.

@SpencerCooley
Created January 19, 2018 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpencerCooley/bba53864790a90f8fe66aa1fc0070811 to your computer and use it in GitHub Desktop.
Save SpencerCooley/bba53864790a90f8fe66aa1fc0070811 to your computer and use it in GitHub Desktop.
import store from '../../../store.js'
import axios from 'axios'
import ContentApi from '../../../api.js'
const contentApi = new ContentApi();
import qs from 'qs'
export default {
data () {
return {
username: '',
password: '',
loading:false,
submitErrors: [],
invalid: false,
}
},
methods: {
attemptLogin(){
if(this.username == '' || this.password == ''){
this.submitErrors = [];
this.submitErrors.push("You must provide both a username and password.");
this.invalid = true;
setTimeout(()=>{ this.invalid = false }, 300);
}
else{
this.submitErrors = [];
this.loading = true;
let that = this;
let data = {
username:this.username,
password:this.password,
client_id:contentApi.clientID,//application client
grant_type: 'password'
}
let headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
axios.post(contentApi.authToken, qs.stringify(data), headers)
.then(response => {
localStorage.setItem("authToken", response.data.access_token);
store.state.authenticated = true;
})
.catch(e => {
that.invalid = true;
setTimeout(()=>{ that.invalid = false }, 300);
console.log(e);//change to raise exception.
that.submitErrors = [];
that.submitErrors.push(e.response.data.error_description);
that.loading = false;
e.response.data.error_description
});
//do your ajax call here to request a token.
}
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment