Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //create the folder like this.
- /*- views
- ----- layouts
- ---------- template.handlebars
- ----- dashboard.handlebars
- ----- home.handlebars
- ----- login.handlebars
- ----- register.handlebars
- now check this links here to insert in the respective files
- http://pastebin.com/37yKKzUM
- http://pastebin.com/vjbLMfP1
- http://pastebin.com/vePMYBug
- http://pastebin.com/TLSgqwBm
- http://pastebin.com/jiHiAh0f
- */
- //This will be your app.js
- var express = require("express");
- var app = express();
- var handlebars = require("express-handlebars");
- var hbs = handlebars.create({defaultLayout: "template"});
- var mongoose = require("mongoose");
- var session = require("express-session");
- var cookieParser = require('cookie-parser');
- var bodyParser = require('body-parser');
- var passport = require("passport");
- var LocalStrategy = require("passport-local").Strategy
- //for the mongoose orm connection with mongoDB
- var userSchema = mongoose.Schema({
- username : String,
- email : String,
- password : String,
- firstName: String,
- lastName: String
- });
- User = mongoose.model("User", userSchema);
- mongoose.connect("mongodb://127.0.0.1/BimboAuthApp");
- mongoose.connection.on("connected", function(){
- console.log("database responding");
- });
- //for the middlewares
- app.use(cookieParser());
- app.use(bodyParser());
- app.use(session({ secret: 'thisIsTheRandomGeneratedCode' }));
- app.use(passport.initialize());
- app.use(passport.session());
- //for the serialization of sessions by the passport module
- passport.serializeUser(function(user, done){
- done(null, user.id);
- });
- passport.deserializeUser(function(id, done){
- User.findById(id, function(err, user){
- done(err, user);
- });
- });
- /*The the main the main
- **This is where the authentication is done by passport
- **This .use allows user to register into the mongoDB Database through the mongoose model,
- thus any input that is entered into the forms in your template will be parsed to the server with the bodyparser middleware
- and then save into the database through the ORM(mongoose)
- LETS SEE HOW IT WORKS------------------------------
- */
- passport.use("local-register", new LocalStrategy({
- passReqToCallback: true
- },//HERE THE "local-register" IS THE EVENT-NAME ASSIGNED TO PASSPORT TO REGISTER THE INPUT IN THE "/register" PAGE IN THE DATABASE
- //THE "new LocalStrategy()" IS AN INITIALIZATION OF NEW OBJECT IN JAVASCRIPT
- //THE "passReqToCallback: true" IS USED TO CALL THE REQ PARAMETER FOR THE EVENT SO YOU CAN USE IT IN THE FUNCTION BELOW
- function(req, username, password, done){//"req" CALLED AS PARAMETER, "username" and "password" and "done" are constant passport
- //functions parameter
- User.findOne({username: username}, function(err, user){
- if(err)
- return done(err);
- if(user){
- return done(null, false);
- }else{
- var newUser = new User({
- email: req.body.email,
- lastName: req.body.last,
- firstName: req.body.first
- });
- newUser.username = username;
- newUser.password = password;
- newUser.save(function(err){
- if(err)
- throw err;
- return done(null, newUser);
- });
- }
- });
- }));//every other things are the work of mongoose
- //just like the previous one but this is use to check if user exist and compare the existing user parameters to make them access the //auth pages.
- //everything is just mongoose work too
- passport.use("local-login", new LocalStrategy({
- passReqToCallback : true
- },
- function(req, username, password, done){
- User.findOne({username: username}, function(err, user){
- if(err)
- return done(err);
- if(!user)
- return done(null, false);
- if(password !== user.password)
- return done(null, false);
- return done(null, user);
- });
- }));
- //less i forget, you need to do the work of your the template engine
- app.set("view engine", "handlebars");
- app.engine("handlebars", hbs.engine);
- //the routers works here
- app.get("/", function(req, res){
- res.render("home");
- });
- app.get("/login", function(req, res){
- res.render("login");
- });
- app.post("/login", passport.authenticate("local-login",{
- successRedirect: "/dashboard",
- failureRedirect: "/login"
- }));//this now call the earlier "local-login" event that we created with the passport.use function... can you remember?
- //sucessRedirect and its counterpart are passport constant object properties. req.flash should be here but i didnt install the
- //connect flash middleware
- app.get("/register", function(req, res){
- res.render("register");
- });
- app.post("/register", passport.authenticate("local-register",{
- successRedirect: "/dashboard",
- failureRedirect: "/register"
- }));//this now call the earlier "local-login" event that we created with the passport.use function... can you remember?
- //sucessRedirect and its counterpart are passport constant object properties. req.flash should be here but i didnt install the
- //connect flash middleware
- app.get("/dashboard", isLoggedIn, function(req, res){
- res.render("dashboard");
- });// isLoging is an handling function which is created below
- app.get("/logout", function(req, res){
- req.logout();//express req object property
- res.redirect("/login");
- });
- function isLoggedIn(req, res, next){
- if(req.isAuthenticated()){
- return next();
- }else{
- res.redirect("/login");
- }
- }//the handling function to ensure only login users access the dashboard
- app.listen(2020, function(){
- console.log("server fired-up on 2020");
- });//the end opin cinema.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement