Building Authentication with passport.js Chapter 6
const bus = new Vue();
export default bus;
Now, replace the contents inside script tag of Login.vue with the following code:
...
<script>
import axios from 'axios';
import bus from './../bus';
export default {
data: () => ({
valid: true,
email: '',
password: '',
emailRules: [
v => !!v || 'E-mail is required',
v => /\S+@\S+\.\S+/.test(v) || 'E-mail must be valid',
],
}),
methods: {
async submit() {
return axios({
method: 'post',
data: {
email: this.email,
password: this.password,
},
url: 'http://localhost:8081/users/login',
headers: {
'Content-Type': 'application/json',
},
})
.then(() => {
this.$swal('Great!', 'You are ready to start!', 'success');
bus.$emit('refreshUser');
this.$router.push({ name: 'Home' });
})
.catch((error) => {
const message = error.response.data.message;
this.$swal('Oh oo!', `${message}`, 'error');
this.$router.push({ name: 'Login' });
});
},
clear() {
this.$refs.form.reset();
},