MongoDB + Mongoose

Maya Alexandera
4 min readDec 7, 2020

--

This is a mongoose.

For my last post, I wrote about my experience with passport JS/Google OAuth.

Next on the list was to create a database to save user data. For this project, I decided to explore MongoDB, a schema-less database. The biggest difference I noticed with Mongo is that each entry can have its own unique set of properties. This is in direct contrast to SQL-based, or relational databases — whose entries must all have the same properties.

MongoDB Setup

  • Sign up with MongoDB if you don’t already have an account.
  • ‘Create a Cluster’ — I selected the free sandbox version for now.
  • Create a new database user — a user/password is required to interact with Mongo. I named mine ‘testUser’.
  • You will be provided a collections string, which will be used to connect with the database. Make the necessary adjustments as instructed, filling in the user/password/database name properties.
  • Copy/Paste “Collections String” into a secret file with a key of “mongoURI”

Mongoose

Mongoose is an optional library that makes interacting with the database easier. One thing to note is, even though MongoDB does not require all records of a collection to have identical properties, mongoose is different. Mongoose wants to know ahead of time what all the properties will be to a given ‘Model’ class. The next step in this process is to use mongoose to create our User model/userShema. A Schema is a mongoose object that defines all the properties and datatypes of a given collection.

Updated import statements in the root index file. mongoose.connect() links mongoose and mongoDB.

mongoose Schema

  • inside the models directory, create a file, ‘User.js’.
  • import mongoose library and Schema component.
  • Declare const userSchema by creating a new mongoose Schema. Schema takes in a properties object — this is where you create all your ‘columns’ for this table.
  • Once the Schema is defined, connect it to the users collection by calling ‘mongoose.model({‘table-name’, schemaObject})’

passport configuration

  • import mongoose library and the secret file containing your mongoURI.
  • declare const User by calling ‘mongoose.model(‘users’)’ — when passed one argument (name of the table as a String) this allows us to use our User model within this passport file.
  • To test, replace the old callback function in GoogleStrategy with an arrow function that saves a new user entry by calling save() on a new User model object passing in the profile id from the response object.

If everything is set up correctly mongoDB will create a new user with their googleId, as well as an automatically generated _id property. The only purpose of the googeId is for authentication. Once the user is created/retrieved from the database, the automatically generated id (_id) provided by MongoDB will be used.

magic

GoogleStrategy Callback Function

  • After testing, re-factor the GoogleStrategy callback function to an asynchronous function.
  • Include a conditional to check if the user exists within the collection already.
Once mongo is set up to save incoming entries I refactored the callback function to include a conditional if the user exists already or not.

The final addition I will include on this topic is creating a logout route. passportJS has a neat built-in function, logout() which will manage re-setting the currentUser to null.

So far — this might be the most amount of time I’ve spent on authentication. There is a lot to it and having a solid knowledge of basic authentication has helped tremendously, and made the learning process more engaging.

What is your favorite method of authentication?

--

--