Full-Stack Web Development with Vue.js and Node

(singke) #1
Building Authentication with passport.js Chapter 6

Replace the contents of the script tag of Login.vue with the following code:


<script>
import axios from 'axios';
import bus from "./../bus.js";

export default {
data: () => ({
valid: true,
email: '',
password: '',
emailRules: [
(v) => !!v || 'E-mail is required',
(v) => /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v)
|| 'E-mail must be valid'
],
passwordRules: [
(v) => !!v || 'Password is required',
]
}),
methods: {
async submit () {
if (this.$refs.form.validate()) {
return axios({
method: 'post',
data: {
email: this.email,
password: this.password
},
url: '/users/login',
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => {
localStorage.setItem('jwtToken', response.data.token)
this.$swal("Good job!", "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")
});
}
},
Free download pdf