DEV Community

Code_Regina
Code_Regina

Posted on

Week 12: Authentication and YelpCamp: Adding Authentication

This week was Authentication and YelpCamp: Adding Authentication from Colt Steele The Web Developer Bootcamp.

 -Introduction to Authentication
 -Secret Page Code Along Pt. 1
 -Secret Page Code Along Pt. 2
 -Secret Page Code Along Pt. 3
 -Secret Page Code Along Pt. 4
 -Secret Page Code Along Pt. 5
 -YelpCamp: Adding Auth Pt. 1 
 -YelpCamp: Adding Auth Pt. 2 
 -YelpCamp: Adding Auth Pt. 3 
 -YelpCamp: Adding Auth Pt. 4 
Enter fullscreen mode Exit fullscreen mode

Introduction to Authentication

We are going to be using passport.js for authentication.
Passport.js is very commonly used.
http://www.passportjs.org/

Passport-local
https://github.com/jaredhanson/passport-local

Passport is about providing a entry point into all different kinds of locations.

Passport-local-mongoose

https://github.com/saintedlama/passport-local-mongoose
Authentication is made possible with sessions.
HTTP is a stateless protocol which means when you send request, those requests are one-time things they do not contain information about history or previous request they are not linked together. A request does not have a state, it is just a one-time transaction Sessions are do keep the history and previous request it is more than just a one-time transaction. Sessions are just a way to make HTTP not stateless.

Secret Page Code Along Pt. 1

Getting started installing the necessary packages

npm install express mongoose -save
npm install passport passport-local -save
npm install passport-local-mongoose save
npm install body-parser express-session save
npm install ejs --save
Enter fullscreen mode Exit fullscreen mode

Inside the app.js file

var express = require(express); 
var mongoose = require(mongoose); 
mongoose.connect(mongodb://localhost/auth_demo_app”); 
var app = express(); 
app.set(view engine, ejs);
app.get(/, function(req, res) {
res.render(home); 
});
app.get(/secret, function(req,res) {
res.render(secret)
app.listen(process.env.PORT, process.env.IP, function() {
console.log(server started);
Enter fullscreen mode Exit fullscreen mode

Secret Page Code Along Pt. 2

In the user.js file

Var mongoose = require(mongoose); 
Var UserSchema = new mongoose.Schema({
    username: String, 
    password: String
}); 
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model(User, UserSchema);
Enter fullscreen mode Exit fullscreen mode

Back in app.js

 app.use(require(express-session) ({
secret: Rusty is the best and cutest dog in the world, 
resave: false, 
saveUnitialized: false
})); 
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.serializeUser());
Enter fullscreen mode Exit fullscreen mode

Secret Page Code Along Pt. 3

<h1> Sign Up Form </h1> 
<form action=”/register” method=”POST”> 
<input type=”text” name=”username” placeholder=”username”>
<input type=”password” name=”password” placeholder=”password”>
<button>Submit</button> 
</form>
Enter fullscreen mode Exit fullscreen mode

In app.js

app.get(/register, function(req,res) {
res.render(register); 
res.send(Register Post Route); 
Enter fullscreen mode Exit fullscreen mode

Secret Page Code Along Pt. 4

Creating the log-in functionality
Add login routes and login form
Inside app.js we need to add two routes a POST and GET request.

Alt Text

This is the form

Alt Text

Middleware

Alt Text###Secret

This is code that is supposed to run before the final route callback. It is possible to have multiple middleware stacks within each other. The idea is that middleware sits between the beginning of the route and at the end of the route

Page Code Along Pt. 5
Add in the logout functionality and add isLoggedIn middleware to check and see if the user is actually logged in.
A simple link is enough to logout a given user.

Alt Text

In app.js time to add the logout functionality.

Alt Text

Creating the middle ware that will check to make sure that the user is logged in to view the secret page, otherwise the user will be redirected to the log in page.

Alt Text

Alt Text

YelpCamp: Adding Auth Pt. 1

Time to add user authentication into our Yelp Camp Application.
We are going to take the authentication concepts from the last lesson and apply them into this lesson.
First we will need to install the packages.

npm install passport passport-local passport-local-mongoose express-session save
Enter fullscreen mode Exit fullscreen mode

In app.js
We must make sure all the important require statements are applied.

Alt Text

In user.js

Alt Text

This sets up the user model with mongoose.

YelpCamp: Adding Auth Pt. 2

Configure Passport and add register routes and templates.
In app.js

Alt Text

Time to add the routes and templates

Here are the authorization routes
The first route is to show the register form

app.get(/register, function(req, res) {
res.render(register); 
}); 
Enter fullscreen mode Exit fullscreen mode

Alt Text

Now time to create the login form

Alt Text

Alt Text

Now to create the route

app.post(/register, function(req, res) {
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user) {
If(err) {
Console.log(err); 
Return res.render(register)
} 
passport.authenticate(local)(req, res, function(){
res.redirect(/campgrounds); 
}); 
});
Enter fullscreen mode Exit fullscreen mode

YelpCamp: Adding Auth Pt. 3

Login needs to have two routes. GET request to show the form.

Alt Text

And then a POST request to actually do the logging in.

Alt Text

This is to show the login form


app.get(/login, function(req, res) {
res.render(login);
}); 
Enter fullscreen mode Exit fullscreen mode

YelpCamp: Adding Auth Pt. 4

Creating the logout logic and prevent anyone from accessing the new comment form without being signed in.

First we must add the logout route

app.get(/logout, function(req, res) {
req.logout();
res.redirect(/campgrounds);
Enter fullscreen mode Exit fullscreen mode

Now we need to make the links go to the right route.

Alt Text

Now to prevent users that are not logged in from seeing the comment section.

Function isLoggedIn(req, res, next) {
If(req.isAuthenticated() {
Return next(); 
}
res.redirect(/login); 
}

Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

There has been a lot of hands-on coding for these lessons.
I still have a lot to learn but I feel like the information is starting to make sense, because I can see how each piece of code impacts the overall project.

Top comments (0)