Advertisement
hills12

Untitled

Dec 26th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //create the folder like this.
  2. /*- views
  3. ----- layouts
  4. ---------- template.handlebars
  5. ----- dashboard.handlebars
  6. ----- home.handlebars
  7. ----- login.handlebars
  8. ----- register.handlebars
  9.  
  10. now check this links here to insert in the respective files
  11. http://pastebin.com/37yKKzUM
  12. http://pastebin.com/vjbLMfP1
  13. http://pastebin.com/vePMYBug
  14. http://pastebin.com/TLSgqwBm
  15. http://pastebin.com/jiHiAh0f
  16. */
  17.  
  18.  
  19. //This will be your app.js
  20.  
  21. var express = require("express");
  22. var app = express();
  23. var handlebars = require("express-handlebars");
  24. var hbs = handlebars.create({defaultLayout: "template"});
  25. var mongoose = require("mongoose");
  26. var session = require("express-session");
  27. var cookieParser = require('cookie-parser');
  28. var bodyParser   = require('body-parser');
  29. var passport = require("passport");
  30. var LocalStrategy = require("passport-local").Strategy
  31.  
  32. //for the mongoose orm connection with mongoDB
  33. var userSchema = mongoose.Schema({
  34.         username : String,
  35.         email : String,
  36.         password : String,
  37.         firstName: String,
  38.         lastName: String
  39.     });
  40.  
  41. User = mongoose.model("User", userSchema);
  42. mongoose.connect("mongodb://127.0.0.1/BimboAuthApp");
  43. mongoose.connection.on("connected", function(){
  44.     console.log("database responding");
  45. });
  46.  
  47. //for the middlewares
  48. app.use(cookieParser());
  49. app.use(bodyParser());
  50. app.use(session({ secret: 'thisIsTheRandomGeneratedCode' }));
  51. app.use(passport.initialize());
  52. app.use(passport.session());
  53.  
  54. //for the serialization of sessions by the passport module
  55. passport.serializeUser(function(user, done){
  56.     done(null, user.id);
  57. });
  58. passport.deserializeUser(function(id, done){
  59.     User.findById(id, function(err, user){
  60.         done(err, user);
  61.     });
  62. });
  63.  
  64. /*The the main the main
  65. **This is where the authentication is done by passport
  66. **This .use allows user to register into the mongoDB Database through the mongoose model,
  67. thus any input that is entered into the forms in your template will be parsed to the server with the bodyparser middleware
  68. and then save into the database through the ORM(mongoose)
  69.  
  70. LETS SEE HOW IT WORKS------------------------------
  71. */
  72. passport.use("local-register", new LocalStrategy({
  73.     passReqToCallback: true
  74. },//HERE THE "local-register" IS THE EVENT-NAME ASSIGNED TO PASSPORT TO REGISTER THE INPUT IN THE "/register" PAGE IN THE DATABASE
  75.   //THE  "new LocalStrategy()" IS AN INITIALIZATION OF NEW OBJECT IN JAVASCRIPT
  76.   //THE "passReqToCallback: true" IS USED TO CALL THE REQ PARAMETER FOR THE EVENT SO YOU CAN USE IT IN THE FUNCTION BELOW
  77. function(req, username, password, done){//"req" CALLED AS PARAMETER, "username" and "password" and "done" are constant passport
  78.                                         //functions parameter
  79.     User.findOne({username: username}, function(err, user){
  80.         if(err)
  81.             return done(err);
  82.         if(user){
  83.             return done(null, false);
  84.         }else{
  85.            
  86.             var newUser = new User({
  87.                 email: req.body.email,
  88.                 lastName: req.body.last,
  89.                 firstName: req.body.first
  90.             });
  91.             newUser.username = username;
  92.             newUser.password = password;
  93.  
  94.             newUser.save(function(err){
  95.                 if(err)
  96.                     throw err;
  97.                 return done(null, newUser);
  98.             });
  99.         }
  100.     });
  101. }));//every other things are the work of mongoose
  102.  
  103. //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.
  104. //everything is just mongoose work too
  105. passport.use("local-login", new LocalStrategy({
  106.     passReqToCallback : true
  107. },
  108. function(req, username, password, done){
  109.     User.findOne({username: username}, function(err, user){
  110.         if(err)
  111.             return done(err);
  112.         if(!user)
  113.             return done(null, false);
  114.         if(password !== user.password)
  115.             return done(null, false);
  116.         return done(null, user);
  117.     });
  118. }));
  119.  
  120. //less i forget, you need to do the work of your the template engine
  121. app.set("view engine", "handlebars");
  122. app.engine("handlebars", hbs.engine);
  123.  
  124. //the routers works here
  125. app.get("/", function(req, res){
  126.     res.render("home");
  127. });
  128. app.get("/login", function(req, res){
  129.     res.render("login");
  130. });
  131. app.post("/login", passport.authenticate("local-login",{
  132.     successRedirect: "/dashboard",
  133.     failureRedirect: "/login"
  134. }));//this now call the earlier "local-login" event that we created with the passport.use function... can you remember?
  135.     //sucessRedirect and its counterpart are passport constant object properties. req.flash should be here but i didnt install the
  136.     //connect flash middleware
  137.  
  138. app.get("/register", function(req, res){
  139.     res.render("register");
  140. });
  141. app.post("/register", passport.authenticate("local-register",{
  142.     successRedirect: "/dashboard",
  143.     failureRedirect: "/register"
  144. }));//this now call the earlier "local-login" event that we created with the passport.use function... can you remember?
  145.     //sucessRedirect and its counterpart are passport constant object properties. req.flash should be here but i didnt install the
  146.     //connect flash middleware
  147.  
  148. app.get("/dashboard", isLoggedIn, function(req, res){
  149.     res.render("dashboard");
  150. });// isLoging is an handling function which is created below
  151. app.get("/logout", function(req, res){
  152.     req.logout();//express req object property
  153.     res.redirect("/login");
  154. });
  155.  
  156. function isLoggedIn(req, res, next){
  157.     if(req.isAuthenticated()){
  158.         return next();
  159.     }else{
  160.         res.redirect("/login");
  161.     }
  162. }//the handling function to ensure only login users access the dashboard
  163. app.listen(2020, function(){
  164.     console.log("server fired-up on 2020");
  165. });//the end opin cinema.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement