Error class on login form

This commit is contained in:
An Phan 2016-01-01 13:16:17 +07:00
parent 2db70a9d00
commit 739a447ce9
4 changed files with 23 additions and 19 deletions

View file

@ -55,7 +55,7 @@
ready() {
// The app has just been initialized, check if we can get the user data with an already existing token
var token = ls.get('jwt-token')
var token = ls.get('jwt-token');
if (token) {
this.authenticated = true;
this.init();

View file

@ -1,5 +1,5 @@
<template>
<form @submit.prevent="login">
<form @submit.prevent="login" :class="{ error: failed }">
<input v-model="email" type="email" name="email" placeholder="Email Address" autofocus required>
<input v-model="password" type="password" name="password" placeholder="Password" required>
<button type="submit">Log In</button>
@ -14,13 +14,17 @@
return {
email: '',
password: '',
failed: false,
};
},
methods: {
login() {
userStore.login(this.email, this.password, () => {
this.failed = false;
this.$dispatch('user:loggedin');
}, () => {
this.failed = true;
});
},
},

View file

@ -9,30 +9,33 @@ Vue.config.debug = false;
Vue.use(require('vue-resource'));
Vue.http.options.root = '/api';
Vue.http.interceptors.push({
request(request) {
request(r) {
var token = ls.get('jwt-token');
if (token) {
Vue.http.headers.common.Authorization = `Bearer ${token}`;
}
return request;
return r;
},
response(response) {
if (response.status === 400 || response.status === 401) {
app.logout();
response(r) {
if (r.status === 400 || r.status === 401) {
if (r.request.method !== 'POST' && r.request.url !== 'me') {
// This is not a failed login. Log out then.
app.logout();
}
}
if (response.headers && response.headers.Authorization) {
ls.set('jwt-token', response.headers.Authorization);
if (r.headers && r.headers.Authorization) {
ls.set('jwt-token', r.headers.Authorization);
}
if (response.data && response.data.token && response.data.token.length > 10) {
ls.set('jwt-token', response.data.token);
if (r.data && r.data.token && r.data.token.length > 10) {
ls.set('jwt-token', r.data.token);
}
return response;
return r;
},
});

View file

@ -76,14 +76,11 @@ export default {
*
* @param {String} email
* @param {String} password
* @param {Function} cb
* @param {Function} successCb
* @param {Function} errorCb
*/
login(email, password, cb = null) {
http.post('me', { email, password }, () => {
if (cb) {
cb();
}
});
login(email, password, successCb = null, errorCb = null) {
http.post('me', { email, password }, successCb, errorCb);
},
/**