Skip to main content

Posts

Showing posts with the label routes

Authentication Project: Secret Page App Part Two (Login/Logout)

Login Routes Step One: Add login route For login, we need to write a "/login" route to render to the login page. At the same time, we need to create login page in the "view" folder. Step Two: Set up the local strategy Then, we need to set up the local strategy to be "user authenticate". Step Three: Write post route and add middle-ware Inside the app.post, we pass in "passport.authenticate", which is a middle-ware. It allows codes that run before our final route call back. When app gets a post request to "/login", it's going to run this code immediately.  Step Four: Run the app We run the app and can see the login form. Then, for every page, we add three links: sign up, login, and logout. So the homepage will look like this: Logout Routes  Logout routes are very simple. We need to create a "/logout" route which will log user out and redirect to the...

Authentication Project: Secret Page App Part Two (Sign Up Page)

Sign Up Page After a complex set up process, finally we can start to write our routes! Step One: Write register route We will write our register route which will render to a "register" page. In this page, it will have a simple sign up form. Step Two: Create Sign up form We add "register.ejs" in the "views" folder. Then, we create a simple form which the url link will be "/register". Then, we run the app.js and get the "/register", so we can see the form. Step Three: Set up body parser For every form post request, we need to add body parser to make it work. Step Four: Handle user sign up Once users sign up, it will lead them to the secret page. Therefore, we need to write a post route. Inside the post route, we add passport authenticate , which will log the user in and store the correct information and run the serialize method. And we specify to use "local" strategy .  ...