Despite the plathora of information on the internet, I was unable to find a great tutorial on how to link up Passport with Postgres... I guess elephants don't need passports to travel.

The lack of tutorials for using passport with postgres, meant that I followed a tutorial that hooked up mongodb with passport and then worked backwards to replace the postgres as the database. This turned out to be very tricky! If you are thinking about using mongodb as your database I strongly recommend the Scotch.io: Easy Node Authentication Tutorial as it was very clear and (almost) fun to follow!

I will be following the same structure that Scotch.io lays out for the files and planning.

First we need to set up the Postgres database, for detailed instructions on how I set up my Postgres Database check out my blog: Setting Up the Database (sidenote: for this project I created a new database called auth instead of navigatio to prevent confusion).

Once our database is up and running we must require it on the server.js file. This is also where I create the passport object that is then sent it to the passport.js file to get configured. So we should also set up our postgres on the server.js file.

I want to comment out (or delete) anything that requires or uses mongoose and I want to require Postgres(pg). I also run a function to make sure that Postgres is working alright! If it is I will get the date and time printed on the server:

Perfect! Other than requiring the database on server.js I will only focus on two files in order to switch from mongodb to Postgres:

  1. user.js file - holds the model of information that we want to hold for our users. For our local accounts we will want to keep the username and password, and for our social accounts we want to keep id and token. For now, we will focus only on creating a local user.

  2. passport.js file - the configuration for passport happens here. Passport authenticates our users and standardizes the information from different social networks. This is where we create our strategy for local, facebook, twitter and serialize/deserialize our User to store the user in session.

He is a quick look at what these two files looked like when they were using mongodb:

So lets think about whats happening here:

On the user.js file , we are using mongoose to define the local user, hashing the password and exporting the module as the User. This means that when we take away mongoose, we will need to define the User in Postgres terms.

On the passport.js file, when a user tries to sign-up, we pass their User information through a LocalStrategy. This strategy checks to see if the email already exists in our database by using the User.findOne function. This means that we will have to define the User.findOne function.. If the email already exists, it will inform the user. If the email does not already exist in our database, then it will create and save the newUser. So we also need to define a User.save function , for our User.

It is important to remember that these two files work very closely together. I like to think of the user.js as the glossary of difficult words, which the passport.js(as the book) has these difficult words spread throughout it.

The user.js defines what the user can do, and the attributes it should have, and the passport.js asks it to execute these skills. (for more analogies please see bottom)

So the things that need to happen are:

    1. Define the User in Postgres terms.
    2. Define the User.findOne function.
    3. Define a User.save function. ,
    4. Define findUserById //this is for sessions

1. Define the User Object

Similar to the mongoose schema, we need to define the object User but this time in Postgres terms.


ANOTHER ANALOGY for the user.js and passport.js relationship :)
A better analogy than the book one might be work training. In my first week at work, if I was an accountant, I would learn taht each month I have to calculate payroll and operational cost.

For payroll:

$wage/hour * # of hours = net pay of employee

For operational cost:

The Sum of all employees net pay + the cost of electricity and rent = the operational cost.

Even though you aren't going to use these on your first day, they are given to you for when you need them. You may save these in a document called IMPORTANT EQUATIONS that you reference when the time comes. Then when you do calculate each employees net pay and the operational costs each month. Each month will give different outcomes based on the number of hours worked, but they will follow the same important equations. The EXECUTION of the equations are the saved in the passport.js function. The important equations are saved in theuser.js!

comments powered by Disqus