Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req.isAuthenticated() always gives false and redirects to login() route. #12

Open
ASHISH-GITHUB2495 opened this issue Sep 30, 2020 · 3 comments

Comments

@ASHISH-GITHUB2495
Copy link

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");

}
else
{
	passport.authenticate("local")(req,res,function(err){
		if(!err)
			console.log("hey");
		res.redirect("/secrets");
	});
}

});

});

app.post("/login", function(req, res) {

});

app.listen(3000,function(){
console.log("Server started at port 3000");
});

@NoelCov
Copy link

NoelCov commented Jan 29, 2021

Same for me!

Found the solution, so apparently we need this line: passport.use(User.createStrategy());

@jabrahamdev
Copy link

jabrahamdev commented Apr 7, 2021

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
}));

Make sure is exactly like that.

@bharatsbsingh
Copy link

Same for me!

Found the solution, so apparently we need this line: passport.use(User.createStrategy());

Bro how does this code works
can uh please elobrote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants