You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I don't know if you worked out. I was in the same situation, my problem was that I did not configure the session like Angela, I just pasted from docs and did not check if it was the same as hers.
Check this part in your code:
app.use(session({
secret: 'Our little secret.',
resave: false,
saveUninitialized: false
}));
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
//const bcrypt = require("bcrypt");
//const md5 = require("md5");
//const encrypt = require("mongoose-encryption");
const app = express();
app.use(express.static("public"));
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect("mongodb://localhost:27017/userDB",{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, });
//mongoose.set("useCrateIndex", true);
const userSchema =new mongoose. Schema({
email: String ,
password: String
});
userSchema.plugin(passportLocalMongoose);
// for encryption
//userSchema.plugin(encrypt,{secret: process.env.SECRET , encryptedFields: ["password"]});
//const saltRounds =10;
const User = new mongoose.model("User",userSchema);
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get("/",function(req,res){
res.render("home");
});
app.get("/login",function(req,res){
res.render("login");
});
app.get("/register",function(req,res){
res.render("register");
});
app.get("/secrets", function(req, res) {
if(req.isAuthenticated()){
res.render("secrets");
}
else
{res.redirect("/login");}
});
app.post("/register",function(req,res){
User.register({username: req.body.username},req.body.password,function(err,user){
if(err){
console.log(err);
res.redirect("/register");
});
});
app.post("/login", function(req, res) {
});
app.listen(3000,function(){
console.log("Server started at port 3000");
});
The text was updated successfully, but these errors were encountered: