Angular and local-Passportjs authentication

24 Views Asked by At

Good Evening. Trying to create Authentication to my angular project , using passport-js local strategy req.user is defined in '/login' route but when i am trying to request a different route, req.user is undefined..

when i see the session collection in atlas first of all weirdly it creates 2 sessions and seems that it indeed save the req.user but a new session is created each request

   // express-session-usage
    const store = MongoStore.create({
      mongoUrl: MongoUrl,
      secret: 'khkjh',
      touchAfter: 24 * 60 * 60,
      autoRemove: 'native'
    })
    

const sessionConfig = {
  store,
  name: 'Banana',
  secret: 'sskdjskdjfskj',
  resave: false,
  saveUninitialized: true,
  cookie: {
    httpsOnly: true,
    secure: true,
    expires: Date.now() + 18000000, //7200000 //1000 * 60 * 60 * 24 * 7, // session expires every 2 hours
    maxAge: 18000000 //7200000 1000 * 60 * 60 * 24 * 7 // // Max session Age is 2 hours
  }
}
app.use(session(sessionConfig))
app.use(passport.initialize());
app.use(passport.session());
here are the mongoose models created

passport.use(new LocalStrategy(Doctor.authenticate()));
passport.serializeUser(Doctor.serializeUser());
passport.deserializeUser(Doctor.deserializeUser());

app.post('/login',passport.authenticate('local', { session: true,failureRedirect: '/isAuth' }),async(req,res)=> {
  if (req.user === false) {
    console.log(req.user)
    res.json(false);
    res.end()
  } else {
    console.log(req.user)
    res.json(true);
    res.end()
  }
})
0

There are 0 best solutions below