Skip to main content

Authentication Project: Secret Page App Part One (Set up & Create User Model)


So far we only write the RESTful routes to send request and redirect to the related page, but we haven't do anything about authentication.

If we can sign up, login, and log out, it will make our app more meaningful. Therefore, the course shows us how to create a secret page app, which will allow us to sign up, login to a secret page, and logout.

Since it requires so many pieces, I will divided this project into three parts: set up & create user model, sign up, and login/logout. In this post, I am going to show all the set up process.

Set Up


Step One: Install packages

The first thing we are going to do to create a folder "Auth", and inside of "Auth", we need to create another folder "AuthDemo". Then we cd (change directory) to "AuthDemo".

Now we can set up the actual application structure by installing a few packages: passport, passport-local, passport-local-mongoose, mongoose, express, ejs, body-parser, and express-session.


Step Two: Require all the packages

After installation is done, we need to create "app.js" file inside of the "AuthDemo" folder. And in the file, we need to require all the packages, connect to the mongodb, and set up some basic configuration.


Step Three: Add first two routes: home and secret page

We will write our first Get route "/" which will render to the home page. At the same time, we need to create a new folder "views", and inside "views", we add "home.ejs" file. We can write a simple h1 text "Homepage" in the file.



 We also need to write another route to render to the "secret" page. And we also need to create a "secret.ejs" file inside of "views". In this file, I write some secret words.

Create User Model

Step One: Define user schema

We need to create another folder "models" inside "AuthDemo". Inside the "models", we touch "user.js" file.

In the "user.js", we will require mongoose, and define user schema which contains two different parts: username and password. Both of them are string.  Then we are going to add user schema into the mongoose model.


Then, we back to the "app.js" file, and we require the user.



Step Two: Add passport local mongoose

Inside the "user.js", we will add in passport local mongoose to our user model.



Step Three: Set passport up

Back to "app.js", we need to set passport configuration up, so it will work in our application.


Step Four: Add express-session

In the "app.js", we need to set up express-session. What we need to do is run it as a function and pass in some arguments.

In the function, we have to pass in three options in order for it to work with passport. Those options are: secret, resave and save uninitialized.


The "secret" can be anything at all. You can pick your own words. It will be used to encode and decode the sessions. Therefore, we are not going to be store data inside the session as it normally looks as readable data. 

Then, set the rest two options to be "false". Those are the required, no need to explain.

Step Five: Set up serialize and deserialize user

The last step of creating user model is to set up two more things: user serialize and user deserialize.

Those two methods are really important to passport. They are responsible for reading the session, taking the data from the session which is encoded and unencoding it. This is the deserialize.

And then encoding it, serializing it and putting it back in the session which is what serialize user does.


Comments

Popular posts from this blog

GIT Study Note: Six Git Commands You Should Know!

What is Git? According to Git website , Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Basically, we can use Git to track all of changes and revert them back if we made mistakes. The version control system is a way for us to work with different versions of our code, so that we can save different features and make notes as we progress. It is also very useful for team collaboration. Six Git Command You Should Know 1. Git Init In our server, we need to create a "Git" directory and inside of "Git", we make another direcory "git_inro". Then, we add a file "app,js" in the "git_intro" folder. The first thing we need to run is "git init" . When we run git init, it will make a directory which is hidden called ".git". Wherever I run that git init command, it's going to track all of our changes. ...

Intermediate Express.js: How To Render HTML From A EJS File?

The Express.js apps we have developed so far are just simply getting requests from users and sending matched message back to them. However, it is not what we want in the real web application. In most case, we want to send back a relevant web page when our app receives request from user. In order to do that, we need to npm install ejs file in the Express, create HTML page in the ejs file, and render HTMl from the ejs file. I know it sounds confuse now. I am going to explain more details in the example. EJS stands for embedded JavaScript. It lets us embed JavaScript code variables if statements loops inside of HTML. How to render HTML from a EJS file? Step One: Create a HTML page in EJS file Firstly, in our server, we create a folder named "EJSDemo". In this folder, we touch two files, one is "app.js", and the other one is "home.ejs". We are going to write a simple HTML code in the "home.ejs" file. The instructor create a dog page,...

Intermediate Express.Js: Write Conditions and Loops In The EJS (study note)

We continue to use the app.js which we have build to be the study example. In the last post, we only add a simple EJS tag <%= thingVar %> inside h1 tag. This will match the "thing" to any "thing" request from the user. If Statement Today, the course shows us to write a simple if statement in the EJS file. I learn to apply it to my own app. In the if statement, if users send"/wantto/workout" request, the page will show a new content "Good Idea!". We wrote the if statement in the "diet.ejs" file, and we add "<% %>" tags around the code. We have to wrap every line of JavaScript anytime JavaScript starts and ends. Moreover, you may see there are two types of ejs tags here: <%= %> The one with equal sign. According to the course, when we add equal sign, the value that is returned inside of the tag will be rendered to the HTML page. it will be added to the HTML. For example, if we write ...