jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.74k stars 498 forks source link

req.isAuthenticated is returning false while using with react when we refreshes the pages #169

Open shoaibnoor95 opened 6 years ago

shoaibnoor95 commented 6 years ago

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