Full-Stack Web Development with Vue.js and Node

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

Yup, that's it! We will need to initialize passport and just add
passport.authenticate('jwt', { session: false }). We have to pass the JWT


token and the passport JWT strategy automatically authenticates the current user.


Now, let's also send the JWT token while making a request to the movie listing page.


In Home.vue, add the following code:


...
<script>
import axios from 'axios';

export default {
name: 'Movies',
data() {
return {
movies: [],
};
},
mounted() {
this.fetchMovies();
},
methods: {
async fetchMovies() {
const token = window.localStorage.getItem('auth');
return axios({
method: 'get',
url: 'http://localhost:8081/movies',
headers: {
Authorization: `JWT ${token}`,
'Content-Type': 'application/json',
},
})
.then((response) => {
this.movies = response.data.movies;
this.current_user = response.data.current_user;
})
.catch(() => {
});
},
},
};
</script>
Free download pdf