Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js login and registration

var express = require("express"),
    mongoose = require("mongoose"),
    passport = require("passport"),
    bodyParser = require("body-parser"),
    LocalStrategy = require("passport-local"),
    passportLocalMongoose = 
        require("passport-local-mongoose"),
    User = require("./models/user");
  
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect("mongodb://localhost/auth_demo_app");
  
var app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
  
app.use(require("express-session")({
    secret: "Rusty is a dog",
    resave: false,
    saveUninitialized: false
}));
  
app.use(passport.initialize());
app.use(passport.session());
  
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
  
//=====================
// ROUTES
//=====================
  
// Showing home page
app.get("/", function (req, res) {
    res.render("home");
});
  
// Showing secret page
app.get("/secret", isLoggedIn, function (req, res) {
    res.render("secret");
});
  
// Showing register form
app.get("/register", function (req, res) {
    res.render("register");
});
  
// Handling user signup
app.post("/register", function (req, res) {
    var username = req.body.username
    var password = req.body.password
    User.register(new User({ username: username }),
            password, function (err, user) {
        if (err) {
            console.log(err);
            return res.render("register");
        }
  
        passport.authenticate("local")(
            req, res, function () {
            res.render("secret");
        });
    });
});
  
//Showing login form
app.get("/login", function (req, res) {
    res.render("login");
});
  
//Handling user login
app.post("/login", passport.authenticate("local", {
    successRedirect: "/secret",
    failureRedirect: "/login"
}), function (req, res) {
});
  
//Handling user logout 
app.get("/logout", function (req, res) {
    req.logout();
    res.redirect("/");
});
  
function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) return next();
    res.redirect("/login");
}
  
var port = process.env.PORT || 3000;
app.listen(port, function () {
    console.log("Server Has Started!");
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: save item in array javascript 
Javascript :: Program to find GCD or HCF of two numbers javascript 
Javascript :: javascript get the last item in an array 
Javascript :: React Native drawer navigation screen header title and buttons 
Javascript :: handle bar 
Javascript :: array validation in jquery 
Javascript :: find second largest number in array javascript 
Javascript :: regex forms 
Javascript :: Discord.client on 
Javascript :: spread and rest operator javascript 
Javascript :: js setinterval run immediately 
Javascript :: react computed example 
Javascript :: invertir un array javascript 
Javascript :: prototype chain in javascript 
Javascript :: angular component 
Javascript :: angular $http abort request 
Javascript :: javascript extract from object and array 
Javascript :: change height of div with scroll in javascript 
Javascript :: merge two singly linked lists 
Javascript :: javascript create string of given length 
Javascript :: update nested formgroup angular 
Javascript :: convert rgb to hex 
Javascript :: documentelement javascript 
Javascript :: dropdown hide 
Javascript :: js callback hell 
Javascript :: how to do something once in javascript 
Javascript :: how to get time zone difference date-fns 
Javascript :: image react native base64 
Javascript :: jQuery Method Chaining 
Javascript :: what does json.parse do 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =