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 is returning false while using with react when we refreshes the pages #169

Open
shoaibnoor95 opened this issue Apr 6, 2018 · 0 comments

Comments

@shoaibnoor95
Copy link

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

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

1 participant