Building Authentication with passport.js Chapter 6
url: '/api/current_user',
})
.then((response) => {
this.current_user = response.data.current_user;
})
.catch(() => {
});
},
logout() {
return axios({
method: 'get',
url: '/api/logout',
})
.then(() => {
bus.$emit('refreshUser');
this.$router.push({ name: 'Home' });
})
.catch(() => {
});
},
},
};
</script>
Here we have added the method called refreshUser which is being listened by App.vue
in the mounted method. Whenever a user logs in to the app, the method called
refreshUser in App.vue gets called and fetches the logged in user's information.
Also, we are displaying the user's email in the top bar so that we can know if the user is
logged in or not.
Also, let's remove the JWT authentication from movies controller as well. Replace the
contents in controllers/movies.js with the following code:
const MovieSchema = require('../models/Movie.js');
const Rating = require('../models/Rating.js');
module.exports.controller = (app) => {
// fetch all movies
app.get('/movies', (req, res) => {
MovieSchema.find({}, 'name description release_year genre', (error,
movies) => {
if (error) { console.log(error); }
res.send({
movies,
});
});