这是我的用于在password.socketIO之前初始化通行证的代码。有时,passportSocketIo会引发错误,指出未初始化Passport。无论如何,我可以在app.use上使用async await来确保在套接字尝试使用通行证之前先初始化通行证吗?
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);
}
这是它创建的错误:
/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]