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

Intro To Authentication: Three Useful Tools Will Make Everything Easier

Authentication is very important for both users and websites. It helps to keep users' information safe and also helps us to manage the accounts. Authentication is also complex because there is a lot of moving pieces that have to fit together in just the right way to make that work. The course introduce three tools to make authentication more quickly and safely. 1. Passport.js Passport.js will make our life easier and make our code shorter. It also helps to make implementation faster. Most importantly, it is used a lot in the real word. 2. Passport Local Passport Local is for username and password. It's about providing this entry point into all sorts of destinations. 3. Passport Local Mongoose This package will help us implement authentication with passport, and it's specially made to work with mongoose. We are also going to use these three tools into a Secret Page App project, which will show in next few posts.

Express Study Notes: Five Steps To Develop My First Express App

In the last post, I shared my study note about one of the most important reason that Node is very popular is because Node has the largest package ecosystem of open source libraries in the world. Node's NPM, the package manager for Javascript, contains almost 200,000 packages which are centralized on the NPM website. And more importantly, NPM has a command line tool which help us to install packages really easily. The course instructor told us that we are going to learn Express.js to develop Apps in this course. screenshot for the Express.js.com homepage What is Express.js? Express.js is a web development framework. According to the course, framework is a bunch of code that someone else wrote just like a library is except when we use a framework, we have less control. The goal of using this framework is to make our development of APPs faster. Why do we use Express.js? 1. It is the most popular Node web development framework so far. 2. It is a very lightweight ...

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,...