hey i am using passport.js with react ,ejs and express even i am using same origin but req.isAuthenticated is returning false when i refreshes the page
const express=require('express');
const app=express();
const apiRoutes=require('./routes/apiRoutes');
const passport=require('passport');
const path=require('path');
var bodyParser=require('body-parser');
const session=require('express-session');
const helmet=require('helmet');
const normalRoutes=require('./routes/normalRoutes');
const setuppassport=require('./passport/setuppassport');
var mongoose=require('mongoose');
var cookieParser=require('cookie-parser');
const flash=require('connect-flash');
mongoose.Promise=global.Promise;
setuppassport();
var promise=mongoose.connect('mongodb://localhost:27017/test');
app.use(helmet());
app.use(express.static("public"));
app.set('view engine',"ejs");
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(flash());
app.use(cookieParser());
app.use(session({
secret:'my-secret',
saveUninitialized:true,
resave:true
}));
app.use(passport.initialize());
app.use(passport.session());
const protected=function(req,res,next){
if(req.isAuthenticated()){
next();
}
else{
res.status(401).send('Un authorized');
}}
app.use('/api',apiRoutes);
app.use('/api',protected);
app.use(normalRoutes);
app.listen(3000,()=>{
console.log('Server is runing on port 3000');
})
here is my code
hey i am using passport.js with react ,ejs and express even i am using same origin but req.isAuthenticated is returning false when i refreshes the page const express=require('express'); const app=express(); const apiRoutes=require('./routes/apiRoutes'); const passport=require('passport'); const path=require('path'); var bodyParser=require('body-parser'); const session=require('express-session'); const helmet=require('helmet'); const normalRoutes=require('./routes/normalRoutes'); const setuppassport=require('./passport/setuppassport'); var mongoose=require('mongoose'); var cookieParser=require('cookie-parser'); const flash=require('connect-flash'); mongoose.Promise=global.Promise; setuppassport(); var promise=mongoose.connect('mongodb://localhost:27017/test'); app.use(helmet()); app.use(express.static("public")); app.set('view engine',"ejs");
app.use(bodyParser.urlencoded({extended:false})); app.use(bodyParser.json()); app.use(flash()); app.use(cookieParser()); app.use(session({ secret:'my-secret', saveUninitialized:true, resave:true })); app.use(passport.initialize()); app.use(passport.session()); const protected=function(req,res,next){ if(req.isAuthenticated()){ next(); } else{ res.status(401).send('Un authorized'); }} app.use('/api',apiRoutes); app.use('/api',protected); app.use(normalRoutes); app.listen(3000,()=>{ console.log('Server is runing on port 3000'); })
here is my code