Part 8…

Brian Gordon
4 min readJun 28, 2021

The last post left off with finishing up User validation… but now we need to dive into Authentication. And to start, we will be using cookies. If you are not familiar with what cookies are… grab some milk! To start, a cookie identifies a specific user so that when the user makes changes to the UI in their browser, the sever know what and who to save certain information to… an example of this could be a shopping cart… lets say you’ve added some items but leave the site without purchasing anything. If the site uses cookies to keep track of your user info, the cart will still have the items you’ve added pending stock of product. First we need to create some users records…

Inside of the index.js file, we will first require in our usersRepo at the top of the file. This gives us access from the users repository. Now we need to create a user…

but we are going to need the id of that user when its created so navigate to the the users.js file, and to do that, we can just return the attributes object that we have assigned an id to…

So now when we call create, we will get back an object that contains the id of the user we just made. Now back inside of the index.js, we’ll save that new user to the variable user…

So now that we have that users id we can save it to the cookie…but first, we need to figure out how the cookie interacts with that user. We can spend the time using express and setting it all up, or we can go the simple route and add a 3rd party api package to our project that will handle all the nitty gritty of cookie relations. The library that we are going to be using is “cookie-session” head over to the terminal at the top level of the project and npm install “cookie-session”…

Now we can start wiring up the library and then store the users id inside of a cookie. First, require cookieSession…

Cookie-session is a middle-ware, so to wire it to our app we will app.use(cookieSession()) that is an function that takes a configuration object as its argument, and that object will have only one property… “keys”, which is an array and it will take a string of some random letters and numbers…

This encryption of random characters is CRUCIAL… I highly recommend checking the docs! But now, the cookie-session library is all wired up to our app and now we can start to store a users id to that cookie…

so now, what we are doing here, req.session comes with cookie-session. The req.session property is an object that we can add as many properties as we want. SO… if we create the property userId and set it equal to user.id, the user should be automatically be signed into the application when they sign up for an account! And now a quick test, make sure the server is running and head over to the browser at localhost3000, sign up with NEW information, and hit submit…

Now its a bit ugly, but using the dev tools, open up the network tab and look under the headers and you should see some new information “Set-Cookie”…

Now, to double check, Im going to print the userId and to do that I will interpolate req.session.userId in the app.get() function…

now save, and if you navigate back to localhost3000 you should see the users id which we got from our session…

And thats that for this post… up next, an even deeper dive into Auth…

--

--