Full-Stack Web Development with Vue.js and Node

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

app.use(passport.initialize());
app.use(passport.session());

The above code blocks uses a secret token required to save the user details. We will be


defining the token in a separate file so that all of our configuration token reside in a single


place.


So, let's go ahead and create a file called Config.js inside the config directory and the


following lines of code:


module.exports = {
DB: 'mongodb://localhost/movie_rating_app',
SECRET: 'movieratingappsecretkey'
}

We also added a GET route called /api/current_user to fetch the current logged in user


details. This api uses a middleware method called isLoggedIn which checks if the user's


data is on the session or not. And if the user's data exists in the session, the current user
details is sent back as the response.


Another endpoint which we added is the /logout which simply logs out the user and


destroys the session.


Hence, with this configuration, now we should be able to log in successfully using the


passport.js Local Strategy.


The only problem that we have now is there is no way to know if the user successfully
logged in or not. For that we need to display some user's information such as email to


indicate the logged in user.


For this, we need to pass the user's information from Login.vue to App.vue so that we can


display the user's email in the top bar. We can use a method called emit provided by Vue


which is used to pass the information between the Vue components. Let's go ahead and


configure that.


Configuring emit method


Let's first create a transmitter which can communicate between the different Vue


components. Create a file called bus.js inside src directory and add the following


contents:


import Vue from 'vue';
Free download pdf