diff --git a/packages/user/src/server.js b/packages/user/src/server.js index de0cea5..3d7577a 100644 --- a/packages/user/src/server.js +++ b/packages/user/src/server.js @@ -22,12 +22,16 @@ export default { }); }, '@latus/db/models': () => models, - '@latus/http/request': () => (req, res, next) => { + '@latus/http/request': (latus) => (req, res, next) => { debug('@latus/http/request: passport.initialize()'); passport.initialize()(req, res, () => { debug('@latus/http/request: passport.session()'); passport.session()(req, res, () => { - req.userId = req.user ? req.user.id : 0; + if (!req.user) { + const {User} = ModelMap(latus); + req.user = new User(); + req.user.id = 0; + } next(); }); }); @@ -47,25 +51,29 @@ export default { const {connected} = io.of('/'); const here = sids.filter((sid) => !!connected[sid]); // eslint-disable-next-line max-len - const reduced = here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.userId}), {}); + const reduced = here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.user.id}), {}); return reduced; }, }), - '@latus/socket/authenticate': () => (socket, next) => { + '@latus/socket/authenticate': (latus) => (socket, next) => { debug('@latus/socket/authenticate: passport.initialize()'); passport.initialize()(socket.handshake, undefined, () => { debug('@latus/socket/authenticate: passport.session()'); passport.session()(socket.handshake, undefined, async () => { /* eslint-disable no-param-reassign */ + if (!socket.handshake.user) { + const {User} = ModelMap(latus); + socket.handshake.user = new User(); + socket.handshake.user.id = 0; + } socket.handshake.login = LogOps.logIn; socket.handshake.logIn = LogOps.logIn; socket.handshake.logout = LogOps.logOut; socket.handshake.logOut = LogOps.logOut; socket.handshake.isAuthenticated = LogOps.isAuthenticated; socket.handshake.isUnauthenticated = LogOps.isUnauthenticated; - socket.handshake.userId = socket.handshake.user ? socket.handshake.user.id : 0; /* eslint-enable no-param-reassign */ - await socket.join(`/u/${socket.handshake.userId}`); + await socket.join(`/u/${socket.handshake.user.id}`); next(); }); });