Created
January 19, 2018 04:20
-
-
Save SpencerCooley/9d167c3103191fcc0091a506a7d651e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="row"> | |
<div class="container"> | |
<div class="col-md-4 col-md-offset-4"> | |
<div class="col-md-12"> | |
</div> | |
<div class="col-md-12 no-p no-m"> | |
<ul class="error-list " v-for="error in submitErrors"> | |
<li class="error">{{error}}</li> | |
</ul> | |
</div> | |
<div v-if="loading" class="animated-loader">Loading...</div> | |
<form v-else v-on:submit.prevent="attemptLogin" v-bind:class="{ invalid: invalid }" > | |
<label>Username:</label> | |
<input type="text" name="username" v-model="username" class="form-control" > | |
<br> | |
<label>Password:</label> | |
<input type="password" name="password" v-model="password" class="form-control"> | |
<br> | |
<input type="submit" value="Login" class="btn btn-default submit-btn"> | |
</form> | |
</div> | |
</div> | |
</div> | |
</template> | |
<script> | |
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 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 => { | |
this.loading = true; | |
localStorage.setItem("authToken", response.data.access_token); | |
this.invalid = false; | |
store.state.authenticated = true; | |
}) | |
.catch(e => { | |
this.invalid = true; | |
this.loading = false; | |
this.submitErrors.push(e.response.data.error_description); | |
console.log(e); | |
}); | |
} | |
}, | |
}, | |
} | |
</script> | |
<style lang="scss"> | |
//no styles for medium post | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment