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

Intermediate Express.js: How To Add Styles & Partials in EJS File?

So far, we only have simple HTML tags and ejs tags in each ejs file. Every template page has no style at all. And the basic HTML header and footer are also missing. Today, I learned how to add styles and partials in ejs file. Link Style Step One: Touch a Separate CSS file I create a new directory "Style" under the "EJSDemo" directory, then I add a new CSS file "app.css" inside "Style" folder. Step Two: Add app.use(express.static()) in the app.js I add app.use(express.static("style")) in the "app.js". This will tell Express.js to serve the content of "Style" directory. Step Three: Write styles in CSS file I simple give body an orange background color and set text color to be grey. Step Four: Link to CSS file in the EJS file I just add <link> tag to link the "app.css" file on the top of the h1 tag in each ejs template. As the result, when I run the app and...

Intro to Backend: Is Browser The Only Place To Send HTTP Request?

Today,  I begin to learn the second part of web develop course: backend. Frontend is the stuff that we can see and interactive with, such as HTML, CSS, and JS. We can type our code, style our page, or write some function to make interaction. However, backend is everything else. For example, we type Target web address in the Internet Browser. As we hit "enter", there are a few steps to go through: 1. The HTTP request is sent to a particular server's IP address. 2. The server figures out what to send us 3. It sends a HTTP response back to us Those process we are hard to see, and it happens in less than one second. The instructor said that the browser is not the only place to send the HTTP request, and there are so many choices out there. In this learning course, we are going to use Postman App . Postman allows us to make HTTP requests and viewer responses. It is really for developers to understand how things are working or debug something. In the P...

Intermediate CSS: Three Steps To Create A Photo Gallery

I have seen many image blogs these years. People love to post their photos online to express life and feeling.  Image is powerful because it could bring a visual story to audience. People can see what you see, and may be feel the same way as you feel, too. If we want to build our own photo gallery, what should we do? Luckily, the boot-camp instructor just showed me how to use HTML and CSS to create a simple photo gallery.  I would love to share my study note here. (p.s. all the image links are provided by Udemy course instructor) Step One: Add Image Links on HTMLPage The instructor provides all the image links. All we need to do is add those links inside the <img> tag. Without CSS, the page will looks like this: they are not organized, and sizes are different, too. Step Two: Set Image Property Values We set the width of each photo the same value which is 30%, so there will be three images per line. The percentage value can help...