1

Here's my code for initializing passport before passport.socketIO. Occasionally, passportSocketIo throws an error saying that Passport was not initialized. Is there anyway I can use async await on app.use to ensure that passport gets initialized first before the socket attempts to use passport?

app.use(passport.initialize())
app.use(passport.session())

var io = socketIO(server)
io.use(passportSocketIo.authorize({
  cookieParser: cookieParser,       
  key:          'connect.sid',       
  secret:       'secret',    
  store:        sessionStore,        
  success:      onAuthorizeSuccess,  
  fail:         onAuthorizeFail,     
}));

function onAuthorizeSuccess(data, accept){
  accept(null, true);
}

function onAuthorizeFail(data, message, error, accept){
  if (error)
    throw new Error(message);
  accept(null, false);
}

Here's the error it creates:

/Users/user/reactProjects/app/server.js:90
[0]     throw new Error(message);
[0]     ^
[0] 
[0] Error: Passport was not initialized
[0]     at Object.onAuthorizeFail [as fail]
YUSHUF
  • 11
  • 1
  • 2

0 Answers0